#!/bin/bash
SCRIPT_FILE=`realpath $0`
SCRIPT_DIR=`dirname $SCRIPT_FILE`
APT_DIR=`dirname $SCRIPT_DIR`
INCOMING_DIR=$APT_DIR/incoming
timestamp() {
    date +'%s'
}

TMP_FILE=/tmp/raspisms-$(timestamp)
echo "Download latest.deb"
wget --no-cache --quiet https://github.com/RaspbianFrance/raspisms-deb/raw/master/packages/latest.deb -O $TMP_FILE
test -f $INCOMING_DIR/raspisms.deb
DEBFILE_EXISTS=$?
if [ "$DEBFILE_EXISTS" != "0" ]
then
    echo "Debfile not exist, start update"
    mv $TMP_FILE $INCOMING_DIR/raspisms.deb
    exit 0
fi

echo "Debfile exists, check for changes"

cmp $TMP_FILE $INCOMING_DIR/raspisms.deb
SAME_FILE=$?

if [ "$SAME_FILE" != "0" ]
then
    echo "Debfile have change, start update"
    mv $TMP_FILE $INCOMING_DIR/raspisms.deb
    exit 0
fi

rm -f $TMP_FILE
exit 1
