#!/bin/zsh -feu

SWAP_FILE=/swapfile

usage()
{
    cat <<EOF
usage: ${0##*/} [+-h] [--] [file]

-h help
have to be root!
EOF
}
while getopts :h OPT; do
    case $OPT in
        h|+h)
            usage
            exit 0
            ;;
        *)
            usage >&2
            exit 2
    esac
done
shift OPTIND-1
OPTIND=1

if [[ $# -gt 0 ]]
then
    SWAP_FILE=$1
fi

set -x
# with SSD I want to allocate the space? or sparse is ok?
dd conv=fdatasync iflag=fullblock if=/dev/zero of=$SWAP_FILE bs=1M count=8192 || rm -f $SWAP_FILE
chmod 0600 $SWAP_FILE
mkswap $SWAP_FILE
swapon  $SWAP_FILE

# maybe check if it's there already?
echo "$SWAP_FILE none swap defaults" >> /etc/fstab
