#!/usr/bin/bash

# 04/26/2024:WB4FAY:    Modified to check for AlmaLinux
# 04/01/2024:WB4FAY:	Modified to log the install script
# 03/31/2024:WB4FAY:	Modified to support AlmaLinux9
# 01/24/2017:WB4FAY:	Combined dplusreport and monlink install scripts into
#			 a single package.
# 02/10/2017:WB4FAY:	Modified script to hangle Centos 7 and ICOM G3
# 02/15/2017:WB4FAY:	Modified to rename rpmforge.repo if it exists
# 03/17/2024:WB4FAY:	Updated to for G3.2 on AlmaLinux9


echo "MONLINK and DPLUSREPORT Installer - Version 3.2 - 04/26/2024"

FAY_SOURCE=https://wb4fay.com/.g32_util
FAY_SOURCE3=http://wb4fay.com/.g3_util
SYSTEM_DIR=/usr/lib/systemd/system
ROTATE_DIR=/etc/logrotate.d
UTIL_DIR=/dstar/util
DATE=`date "+%y%m%d-%H%M%S"`
LOGF=/tmp/ML-DPR-Installation-$DATE.log

if [ $EUID != 0 ]; then
	echo " installer must be run as root" | tee -a $LOGF
	exit
fi

if [ ! -f /dstar/tools/dplus ]; then
	echo " - DPLUS is not installed" | tee -a $LOGF
        echo " - AA4RC's DPLUS must be installed first" | tee -a $LOGF
	echo " - aborting MONLINK and DPLUSREPORT installation." | tee -a $LOGF
	exit
fi

read RELEASENAME MISC < /etc/redhat-release
if [ $RELEASENAME == "AlmaLinux" ]; then
    echo "Installing on $RELEASENAME based system" | tee -a $LOGF
elif [ $RELEASENAME == "CentOS" ]; then
    echo "This is a $RELEASENAME based system" | tee -a $LOGF
#    echo "  wb4fay.com/util/g3-ml-dpr-install.sh must be used" | tee -a $LOGF
#    echo "   instead of this g32-ml-dpr-installer" | tee -a $LOGF
#    exit
else
    echo "unknown system release $RELEASENAME"
    exit
fi

echo "Please be patient.  This install may take several minutes." | tee -a $LOGF

TAILDIR=/usr/local/share/perl5/File

if [ $RELEASENAME == "AlmaLinux" ]; then
    CMDRSLT=`dnf -q makecache | tee -a $LOGF`
else
    CMDRSLT=`yum -q makecache fast | tee -a $LOGF`
fi

echo " - checking for PERL installation" | tee -a $LOGF
if [ $RELEASENAME == "AlmaLinux" ]; then
    CMDRSLT=`dnf -y install perl perl-libwww-perl perl-XML-Parser | tee -a $LOGF`
else
    CMDRSLT=`yum -y install perl perl-libwww-perl perl-XML-Parser | tee -a $LOGF`
fi

#echo " - checking for perl-libwww-perl installation" | tee -a $LOGF
#CMDRSLT=`dnf -y install perl-libwww-perl | tee -a $LOGF`

#echo " - checking for per-XML-Parser installation" | tee -a $LOGF
#CMDRSLT=`dnf -y install perl-XML-Parser | tee -a $LOGF`

echo " - checking for Tail.pm installation" | tee -a $LOGF
if [ $RELEASENAME == "AlmaLinux" ]; then
    CMDRSLT=`dnf -y install epel-release | tee -a $LOGF`
    CMDRSLT=`dnf -y install perl-File-Tail perl-Env | tee -a $LOGF`
else
    CMDRSLT=`find /usr/local/share/perl5/File -name "Tail.pm" -print`
    if [ ! $CMDRSLT ]; then
        mkdir -p /usr/local/share/perl5/File
        cd /usr/local/share/perl5/File
        curl -O http://wb4fay.com/.g3_util/Tail.pm
        chmod 444 Tail.pm
    fi
fi




function install_dplusreport() {
	echo "" | tee -a $LOGF
	echo " - installing DPLUSREPORT" | tee -a $LOGF
	mkdir -p $UTIL_DIR
	cd $UTIL_DIR

	if [ -f dplusreport.last ]; then
		rm -f dplusreport.last
	fi
	if [ -f dplusreport ]; then
		mv dplusreport dplusreport.last
	fi
        rm -f dplusreport.md5sum
        if [ -f dplusreport-check.sh ]; then
            rm -f dplusreport-check.sh
        fi


        if [ $RELEASENAME == "AlmaLinux" ]; then
	    curl -O $FAY_SOURCE/dplusreport | tee -a $LOGF
	    chmod 755 dplusreport
            curl -O $FAY_SOURCE/dplusreport.md5sum | tee -a $LOGF
            curl -O $FAY_SOURCE/dplusreport-check.sh | tee -a $LOGF
	    chmod 755 dplusreport-check.sh
        else
            curl -O $FAY_SOURCE3//dplusreport | tee -a $LOGF 
            chmod 755 dplusreport
            curl -O $FAY_SOURCE3/dplusreport.md5sum | tee -a $LOGF
            curl -O $FAY_SOURCE3/dplusreport-check.sh | tee -a $LOGF
            chmod 755 dplusreport-check.sh
        fi

	DPRVER=`$UTIL_DIR/dplusreport -v`
	echo " - DPLUSREPORT $DPRVER installed" | tee -a $LOGF
}




function install_monlink() {
	echo "" | tee -a $LOGF
	echo " - installing MONLINK" | tee -a $LOGF

	mkdir -p $UTIL_DIR/dv
	mkdir -p $UTIL_DIR/tmp
	cd $UTIL_DIR

	if [ -f monlink.last ]; then
		rm -f monlink.last
	fi
	if [ -f monlink ]; then
	    /usr/bin/systemctl -q stop monlink.service
	    mv monlink monlink.last
	fi
        if [ -f monlink.md5sum ]; then
            rm -f monlink.md5sum
        fi
        if [ -f monlink-check.sh ]; then
            rm -f monlink-check.sh
        fi
        if [ -f monlink.conf.new ]; then
            rm -f monlink.conf.new
        fi

        if [ $RELEASENAME == "AlmaLinux" ]; then	
            curl -O $FAY_SOURCE/monlink | tee -a $LOGF
 	    chmod 755 monlink
            curl -O $FAY_SOURCE/monlink.md5sum | tee -a $LOGF
            curl -O $FAY_SOURCE/monlink-check.sh | tee -a $LOGF
	    chmod 755 monlink-check.sh
            curl -O $FAY_SOURCE/monlink.conf.new | tee -a $LOGF
        else
            curl -O $FAY_SOURCE3/monlink | tee -a $LOGF
            chmod 755 monlink
            curl -O $FAY_SOURCE3/monlink.md5sum | tee -a $LOGF
            curl -O $FAY_SOURCE3/monlink-check.sh | tee -a $LOGF
            chmod 755 monlink-check.sh
            curl -O $FAY_SOURCE3/monlink.conf.new | tee -a $LOGF
        fi

	if [ ! -f monlink.conf ]; then
		mv monlink.conf.new monlink.conf
	else
		echo " - *********************" | tee -a $LOGF
		echo " - A new config file is available at /dstar/util/monlink.conf.new" | tee -a $LOGF
		echo " - Review and edit the /dstar/util/monlink.conf file as desired" | tee -a $LOGF
		echo "    to include any new features parameters from the monlink.conf.new file." | tee -a $LOGF
		echo " - Monlink will automatically pick up and config changes within 15" | tee -a $LOGF
		echo "    minutes of the monlink.conf file being saved." | tee -a $LOGF
		echo " - *********************" | tee -a $LOGF
	fi

	cd /tmp
        if [ $RELEASENAME == "AlmaLinux" ]; then
	    curl -O $FAY_SOURCE/dstar | tee -a $LOGF
        else
            curl -O $FAY_SOURCE3/dstar | tee -a $LOGF
        fi
	chmod 644 dstar
        mv dstar $ROTATE_DIR/dstar

	cd $UTIL_DIR
	curl -O $FAY_SOURCE/monlink-dv.tgz | tee -a $LOGF
	tar -xPzkf monlink-dv.tgz 2>/dev/null
	if [ -f monlink-dv.md5sum ]; then
		rm -f monlink-dv.md5sum
	fi
	curl -O $FAY_SOURCE/monlink-dv.md5sum

        if [ $RELEASENAME == "AlmaLinux" ]; then
            curl -Os $FAY_SOURCE/monlink.service
	    chmod 644 monlink.service
	    mv monlink.service $SYSTEM_DIR/monlink.service
 	    /usr/bin/systemctl --now enable monlink.service
	    /usr/bin/systemctl daemon-reload
        else
            cd /tmp
            curl -O $FAY_SOURCE3/monlink_init
            chmod 755 monlink_init
            mv monlink_init /etc/rc.d/init.d/monlink
            /sbin/chkconfig --add monlink
            /sbin/chkconfig monlink on
        fi

	/usr/bin/systemctl start monlink.service

        MLINKVER=`$UTIL_DIR/monlink -v`
        echo " - MONLINK $MLINKVER installed" | tee -a $LOGF
}



# See how we were called.

case "$1" in
	dpr)
		install_dplusreport
		exit
		;;
	ml)
		install_monlink
		exit
		;;
	*)
		install_dplusreport
		install_monlink
		;;
esac

exit
