#!/bin/bash
#
# Fist make a directory called check in your linbpq directory and made a copy of the linbpq program file to it.
#
    # Lets get the Modify date from linbpq local en pilinbpq remote
local=$(date -d "$( stat /home/pd9q/linbpq/check/linbpq | awk '/Modify/{gsub( /^[^:]+: +/,"",$0); print}' )"   "+%Y-%m-%d")
remote=$(date -d "$( curl -sI http://www.cantab.net/users/john.wiseman/Downloads/Beta/pilinbpq  | awk '/Last-Modified/{gsub( /^[^:]+: +/,"",$0); print}' )"   "+%Y-%m-%d")
    # Compare the Modified date`s
if (( ${local//-/} < ${remote//-/} ));
then
    #
    echo The local file is from $local and de remote file is from $remote
    echo Let get the new Linbpq version from $remote and downloading it with a timestamp.
    #
    wget -N http://www.cantab.net/users/john.wiseman/Downloads/Beta/pilinbpq -P /home/pd9q/linbpq
    #
    echo Let give the file a date.
    echo We are going to copy the file with the same Last_Modified as on the webserver of GM8BPQ.
    echo And we give it the timestamp of downloading. Dont forget to copy it with the -p option.
    #
    now=$(date "+%Y-%m-%d")
    cp -p /home/pd9q/linbpq/pilinbpq /home/pd9q/linbpq/linbpq-$now
    yes | cp -rfp /home/pd9q/linbpq/pilinbpq /home/pd9q/linbpq/check/linbpq
    # Remove pilinbpq
    rm -f /home/pd9q/linbpq/pilinbpq
    chmod +x /home/pd9q/linbpq/linbpq-$now
    # We are going to use the sed command to alter the linbpq start file.
    sed -i "s,sudo -u pd9q ./.*$,sudo -u pd9q ./linbpq-"$now"," /home/pd9q/linbpq/linbpq.start
    echo Lets check "if" its was okay.
    #
    grep linbpq-  /home/pd9q/linbpq/linbpq.start
    #
    echo Now lets start Linbpq with the new version.
    #
    #sudo systemctl restart linbpq
else
    echo "Your version has the same timestamp so your version is up2date"
    echo The local linbpq in the /home/pd9q/linbpq/check directory has $local as timestamp.
    echo The remote pilinbpq has $remote as timestamp
fi
