#!/bin/bash
# postinst script for #PACKAGE#
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see https://www.debian.org/doc/debian-policy/ or
# the debian-policy package

SRC_FILE=/usr/share/gnsstk__VER__/gnsstk_enable.sh
TGT_FILE=/etc/profile.d/gnsstk_enable.sh

# Set current major version that is being installed. 
SRC_GNSSTK_VER=__VER__

#Set echo font colors
BIRed='\033[1;91m'
BIBlue='\033[1;94m'
BIWhite='\033[1;97m'
Color_Off='\033[0m'

case "$1" in
    configure)
        # Attempting to copy gnsstk_enable.sh script to /etc/profile.d
        # It will copy for these two use cases:
        #   1) target file doesn't exist in /etc/profile.d
        #   2) target file already exists but target gnsstk major version is older
        #         than source file gnsstk major version
        if [[ -f "$SRC_FILE" && -f "$TGT_FILE" ]]; then
            TGT_GNSSTK_VER=$(sed -n -E -e 's/^GNSSTK_VER=([0-9]+).*/\1/p' $TGT_FILE)
            if [ $TGT_GNSSTK_VER -lt $SRC_GNSSTK_VER ]; then
                echo -e "\nCopying $SRC_FILE to $TGT_FILE"
                cp -f $SRC_FILE $TGT_FILE
            else
                echo -e "\nTarget gnsstk major version $TGT_GNSSTK_VER is greater or equal to source gnsstk major version $SRC_GNSSTK_VER"
                echo -e "Therefore, not copying $SRC_FILE to $TGT_FILE\n"
            fi
        else
            if [[ -f "$SRC_FILE" && ! -f "$TGT_FILE" ]]; then
                echo -e "\nCopying $SRC_FILE to $TGT_FILE"
                cp $SRC_FILE $TGT_FILE
            fi
        fi
        # Let's post how to enable $TGT_FILE
        echo -e "${BIBlue}#########################################################"
        echo -e "${BIBlue}##  ${BIWhite}To enable the toolkit:                             ${BIBlue}##"
        echo -e "${BIBlue}##    ${BIWhite}Either start a new login session.                ${BIBlue}##"
        echo -e "${BIBlue}##         ${BIRed}Ex. su - or relogin                         ${BIBlue}##"
        echo -e "${BIBlue}##    ${BIWhite}or source the enable script.                     ${BIBlue}##"
        echo -e "${BIBlue}##         ${BIRed}Ex. source ${TGT_FILE}  ${BIBlue}##"
        echo -e "${BIBlue}#########################################################${Color_Off}\n"
        ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0