#! /bin/sh

if [ "$1" = "-d" ]
then
    destdir=$2
    zipfile=$3
    debug_state="-d"
else
    destdir=$1
    zipfile=$2
    debug_state=""
fi

if [ "${destdir}" = "" ]
then
    echo "Usage: zipfiles [-d] destdir destfile.zip"
    exit 1
fi

if [ -d ${destdir} ]
then
    echo "Removing ${destdir}"
    echo "ERROR: destdir must not already exist"
    exit 1
fi

if [ -f printfiles ]
then
    echo "Found printfiles"
else
    echo "Cannot find printfiles script. Run zipfiles in the same directory as printfiles"
    exit 1
fi

if mkdir ${destdir}
then
    echo "Created destdir"
    if copyfiles ${debug_state} ${destdir} ./printfiles
    then
	if [ -f ${zipfile} ]
	then
	    rm ${zipfile}
	fi
	zip -r ${zipfile} ${destdir}
	rm -rf ${destdir}
	echo "Done creating ${zipfile}"
    fi
else
    echo "ERROR: Could not mkdir ${destdir}"
fi
