#!/bin/sh

if [[ ! -d ~/prints/ ]] ; then
    echo "~/prints/ does not exist as a directory." >&2
    exit 1
fi

if [[ ! -e ~/prints/default ]] ; then
    echo "~/prints/default does not exist." >&2
    exit 1
fi

if [[ $# -lt 1 ]] ; then
    echo "$0 fileName options*" >&2
    exit 1
fi

FILENAME=$1
shift

COUNT=1
DIRNAME=$(printf "${HOME}/prints/p%04d/" ${COUNT})

until [[ ! -e $DIRNAME ]]
do
    DIRNAME=$(printf "${HOME}/prints/p%04d/" ${COUNT})
    COUNT=$(( COUNT + 1 ))
done

echo ${DIRNAME}
mkdir -p ${DIRNAME}

cp "${FILENAME}" "${DIRNAME}/$(basename "${FILENAME}")"
basename "${FILENAME}" > "${DIRNAME}/file"

if [[ $# -gt 0 ]] ; then
    echo $@ > "${DIRNAME}/options"
fi
