#!/bin/bash

PROGDIR=$(dirname $(readlink -f $0))
PATH=/bin:/usr/bin:/usr/local/bin:$PROGDIR

SNAPDIR=/home/steve/www/mkgmap.org.uk/docroot/download

while getopts "d:" c
do
	case $c in
	d) SNAPDIR=$OPTARG;;
	esac
done
shift $[OPTIND-1]

PRODUCT=$1
USEFILE=/tmp/$PRODUCT-used
SVN=http://svn.mkgmap.org.uk/$PRODUCT

if [ $PRODUCT = "" ]; then
	echo Usage: $0 [-d snapshot-dir] product
	exit 1
fi

source mksnapfuncs

# Check file exists and has some lines in it
if [ ! -f $USEFILE ]; then
	echo No usage file $USEFILE
	exit 1
fi
if [ $(wc -l < $USEFILE) -lt 1 ]; then
	echo Not enough lines in usage file
	exit 1
fi

# Check that we are not working with old data
if (( $(stat -c %Y $USEFILE) < $(date +%s) - 1060 )); then
	echo The $USEFILE was generated too long ago
	exit 1
fi

cd $SNAPDIR
for f in $(ls ${PRODUCT}-r*.zip)
do
	base=${f%.zip}
	echo -n $base ' '
	grep -q $f $USEFILE

	case $? in
	0)
		echo 'ok'
		version=${base#*-r}
		mksnap_setdate $version ${base}.tar.gz
		;;
	1) rm ${base}[.-]*
		echo 'removed'
		;;
	esac
done

mksnapindex -d $SNAPDIR $PRODUCT
