#!/bin/sh

DATE=$(date +'%Y/%m/%d')
COUNT=1
FILENAME=~/Pictures/Screenshots/${DATE}/${COUNT}.png
mkdir -p "$(dirname "${FILENAME}")"

until [[ ! -e $FILENAME ]]
do
    FILENAME=~/Pictures/Screenshots/${DATE}/${COUNT}.png
    COUNT=$(( COUNT + 1 ))
done

TYPE=root

while getopts rwf:? opt
do
    case "$opt" in
        r)
            TYPE=root
            ;;
        w)
            TYPE=window
            ;;
        f)
            FILENAME="$OPTARG"
            ;;
        [?])
            echo "screenshot [ -? ] [ -r | -w ] [ -f filename.png ]" >&2
            exit 1
            ;;
    esac
done

if [[ $TYPE == "root" ]] ; then
    import -window root "${FILENAME}"
else
    import "${FILENAME}"
fi
