#! /bin/bash

LOCK=/var/tmp/update-vincennes-template
TMPFILE=$(tempfile) || exit
DST=/var/lib/wcs-au-quotidien/demarches.vincennes.fr/themes/vincennes/template.ezt

if mkdir $LOCK &> /dev/null; then
        trap "rm -rf $TMPFILE $LOCK" EXIT
        curl --silent https://compte-citoyen.vincennes.fr/__template.ezt > $TMPFILE
	if [ "$?" != "0" ]; then
		exit 1
	fi
	file_size=$(stat -c%s $TMPFILE)
	if [ $file_size -le 4000 ]
	then
		exit 1
	fi
        chown wcs-au-quotidien:wcs-au-quotidien $TMPFILE
        diff $TMPFILE $DST > /dev/null
        if [ "$?" != "0" ]
        then
                cp $DST $DST.backup.$(date +'%Y%m%d-%H%M%S')
                cp $TMPFILE $DST
        fi
fi
