#!/bin/bash
# Up2bpq Version v2.0.1 10-11-2019 by Niels PD9Q
# 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.
#
# Add first check, to do something about the errors.
# Add email option, now you get the version number en changelog mailed to you.
# Try to meet the posix standards
#
# Meet the standard of posix
set -o posix
# Enable debugging
set -xv
#
# Global variables.
# Please change the variables to your own needs.
user=pd9q                                                      # This is the user who ownes/runs Pilinbpq/Linbpq
mail=pd9q@packet-radio.net                                     # Set your e-mail adres
home=/home/pd9q/linbpq/                                        # This is the main directory of linbpq
work=/home/pd9q/linbpq/up2bpq                                  # This is the working directory for up2bpq
changelog=/home/pd9q/linbpq/up2bpq/changelog                   # This is the changelog directory
archive=/home/pd9q/linbpq/up2bpq/archive                       # This is the archive directory to save older versions of pilinbpq/linbpq
logfile=/home/pd9q/linbpq/up2bpq/log-up2bpq.txt                # Logfile (not in use yet)
#
# Start of the main script
#
# Fist check if pilinbpq exist
if [ ! -f "$home"/pilinbpq_* ]; then
        nowdat=$(date "+%Y-%m-%d")
        wget -N http://www.cantab.net/users/john.wiseman/Downloads/pilinbpq.old -P "$home" &>/dev/null
        mv "$home"/pilinbpq.old "$home"/pilinbpq ; chmod u+x "$home"/pilinbpq
        oldlin=$("$home"/pilinbpq -V | grep Version | cut -d' ' -f7)
        cp -p "$home"/pilinbpq "$home"/pilinbpq_"$oldlin"_"$nowdat" ; rm -f "$home"/pilinbpq
        echo "# Pilinbpq Start File v1.1" > "$home"/linbpq.start
        echo "cd "$home"" >> "$home"/linbpq.start
        echo "sudo -u $user ./pilinbpq_"$oldlin"_"$nowdat"" >> "$home"/linbpq.start
        chmod u+x "$home"/linbpq.start
fi
# Check if directory exist
if [ ! -d "$home" ]; then
        mkdir -p -v "$home"
fi

if [ ! -d "$work" ]; then
        mkdir -p -v "$work"
fi

if [ ! -d "$archive" ]; then
        mkdir -p -v "$archive"
fi

if [ ! -d "$changelog" ]; then
        mkdir -p -v "$changelog"
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" &>/dev/null

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

# Chmod the file
chmod u+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 -f "$work"/pilinbpq
        exit 0 &>/dev/null

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
        # Email the new version number and the changlog
        wget https://www.cantab.net/users/john.wiseman/Documents/NodeChangeLog.html -P "$changelog" &>/dev/null
        sed -e '1,20d' -e 's/<[^>]*>//g' "$changelog"/NodeChangeLog.html > "$changelog"/changelog.txt
        sed -e "3s/Version/Version-new/" -n -e '/Version-new/,/Version/ p' "$changelog"/changelog.txt  | mail -s "Changelog of BPQ32" "$mail"
        echo "Pilinbpq is replaced by "$newvertxt" old version was "$oldvertxt"" | mail -s "Update BPQ32 new version" "$mail"
        rm -f "$changelog"/NodeChangeLog.* ; rm -f "$changelog"/changelog.txt
        # No reason to exist any more.
        rm -f "$home"/pilinbpq_"$oldvertxt"_*
        rm -f "$work"/pilinbpq
        sleep 5
        # Change to your own needs
        sudo systemctl restart linbpq
        #sudo reboot
        #sudo shutdown -r now
fi
exit 0
