#!/bin/bash
# Revision of up2bpq Version v2 01-11-2019 (thanks to Mark KD0QYN and Tadd KA2DEW)
# Check version numbers of Pilinbpq and Linbpq against version numbers on the local machine.
# Hold the timestamp. Add version number and timestamp to the file and copy to archive.
# Copy the new version to the Linbpq directory and rerun Linbpq with the new version.
#
# Changelog
# find=`ls /home/pd9q/linbpq/pilinbpq_* | cut -c 19-` to find=`ls "$home"/pilinbpq_*
# oldvertxt=`$./find -V | grep Version | cut -d' ' -f7` to oldvertxt=`$find -V | grep Version | cut -d' ' -f7`
#
# Qoute all the variables
# https://nickjanetakis.com/blog/here-is-why-you-should-quote-your-variables-in-bash
#
# End changelog
#
# The fist time you run the script you wil get some error.
# ls: cannot access '/home/pd9q/linbpq/pilinbpq_*': No such file or directory
# ./up2bpq-v2: line 78: -V: command not found
# ./up2bpq-v2: line 85: [: : integer expression expected
# Run it a second time and you will see that there are no more errors. 
# This is because the script searches for a pilinbpq file with version number and date. 
# This is not present during the first run.
#
# Please change the variables to your own needs.
home=/home/pd9q/linbpq                        # This is the main directory of linbpq
work=/home/pd9q/linbpq/up2bpq-pilinbpq        # This is the working directory for up2bpq
archive=/home/pd9q/linbpq/archive             # This is the archive directory to save older versions of pilinbpq/linbpq
user=pd9q                                     # This is the user who runs Pilinbpq/Linbpq
#
# Start of the main script
#
# Check if directory exist
if [ ! -d "$work" ]; then
        mkdir "$work"
fi

if [ ! -d "$archive" ]; then
        mkdir "$archive"
fi
# Find the old version number
#oldlinver=`ls "$home"/linbpq`

#oldlin=`$oldlinver -V | grep Version | cut -d' ' -f7`

#nowdat=$(date "+%Y-%m-%d")

#if [ -f "$home"/linbpq ]; then
#        cp -p "$home"/linbpq "$home"/pilinbpq_6.0.0.0_2019-01-01
#        rm -f "$home"/linbpq
#fi

if [ ! -f "$home"/linbpq.start ]; then
        echo "# Pilinbpq Start File v1.1" > "$home"/linbpq.start
        echo "cd $home" >> "$home"/linbpq.start
        echo "sudo -u $user ./pilinbpq_6.0.0.0_2019-01-01" >> "$home"/linbpq.start
else
        echo "Checking if there is a linbpq.start file.....File exists! Yeah :)"
fi


# Find the recent version number (pilinbpq_6.0.19.1_2019-10-5?)
find=`ls "$home"/pilinbpq_*`

# Getting the files from BPQ32
wget -N http://www.cantab.net/users/john.wiseman/Downloads/pilinbpq -P "$work"

if [ $? -ne  0 ]; then
        echo "Download FAILED...Try again later"
        exit 1
fi

# Chmod the file
chmod +x "$work"/pilinbpq

# Compare the version numbers
oldvertxt=`$find -V | grep Version | cut -d' ' -f7`
newvertxt=`"$work"/pilinbpq -V | grep Version | cut -d' ' -f7`

# Convert to numbers for comparision
#
oldver=`echo $oldvertxt | sed s'/[.]//g'` newver=`echo $newvertxt | sed s'/[.]//g'`

if [ "$newver" -le "$oldver" ]; then
        echo Version "$oldvertxt" is current. No update available
        echo Nothing to do, so sit down and relax
        rm -rf "$work"
        exit 0
else
        echo Version "$newvertxt" available. Upgrading now
        now=$(date "+%Y-%m-%d")
        cp -p "$work"/pilinbpq "$home"/pilinbpq_"$newvertxt"_"$now"
        cp -p "$work"/pilinbpq "$archive"/pilinbpq_"$newvertxt"_"$now"
        sed -i "s,sudo -u $user ./.*$,sudo -u $user ./pilinbpq_"$newvertxt"_"$now"," "$home"/linbpq.start
        grep linbpq_  "$home"/linbpq.start
        # No reason to exist any more.
        rm -f "$home"/pilinbpq_"$oldvertxt"_*
        rm -rf "$work"
        sleep 5
        sudo reboot
fi
exit 0
