#!/bin/bash

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

if [ ! -f /etc/redhat-release ]; then
	echo "dplus installer only runs on redhat based distributions (CentOS/AlmaLinux)"
	exit
fi
read releasename misc </etc/redhat-release
if [ $releasename == "AlmaLinux" ]; then
	echo "Installing dplus on $releasename based system"
elif [ $releasename == "CentOS" ]; then
	echo "Installing dplus on $releasename based system"
else 
	echo "unknown release $releasename"
	exit
fi

mkdir -p /dstar/tools /dstar/tmp /dstar/dv
cd /dstar/tools
rm -f dplus.last
if [ -f dplus ]; then
	service dplus stop
	mv dplus dplus.last
fi
if [ $releasename == "AlmaLinux" ]; then
	curl -s -O http://www.opendstar.org/.admins/dplus-al
	mv dplus-al dplus
else
	curl -s -O http://www.opendstar.org/.admins/dplus
fi
chmod 755 dplus
DPLUSVER=`./dplus -v`

if [ -f dplus-check.sh ]; then
	rm -f dplus-check.sh.last
	mv dplus-check.sh dplus-check.sh.last
fi
curl -s -O http://www.opendstar.org/.admins/dplus-check.sh
chmod 755 dplus-check.sh

if [ ! -f dplus-readdb ]; then
	if [ $releasename == "AlmaLinux" ]; then
		curl -s -O http://www.opendstar.org/.admins/dplus-readdb-al
		mv dplus-readdb-al dplus-readdb
	else
		curl -s -O http://www.opendstar.org/.admins/dplus-readdb
	fi
	chmod 755 dplus-readdb
fi

if [ ! -f dstarpasswd ]; then
	if [ $releasename == "AlmaLinux" ]; then
		curl -s -O http://www.opendstar.org/tools/G2/dstarpasswd-al
		mv dstarpasswd-al dstarpasswd
	else
		curl -s -O http://www.opendstar.org/tools/G2/dstarpasswd
	fi
	chmod 755 dstarpasswd
fi

if [ ! -f dplus.conf ]; then
	curl -s -O http://www.opendstar.org/.admins/dplus.conf
	mv dplus.conf dplus.conf.ORIG
	extnic=
	intnec=
	extip=
	intip=
	nics=`find /sys/class/net -type l |sed -e 's,.*/,,'`
	for nic in $nics
	do
		nicip=`ip -f inet addr show $nic | sed -En -e 's/.*inet ([0-9.]+).*/\1/p'`
		if [[ "$nicip" == *"10.0.0"* ]]; then
			extnic=$nic
			extip=$nicip
		elif [[ "$nicip" == *"172.16.0"* ]]; then
			intnic=$nic
			intip=$nicip
		fi
	done
	if [ x"$extnic" == x"" ] || [ x"$intnic" == x"" ]; then
		echo "Nonstandard IP addressing for NICs.  You will need to edit dplus.conf and change internal and external nics"
		cp dplus.conf.ORIG dplus.conf
	else
		echo "Using $extnic $extip for external NIC and $intnic $intip for internal NIC"
		sed -e "s/externalethernet=eth0/externalethernet=$extnic/" -e "s/internalethernet=eth1/internalethernet=$intnic/" <dplus.conf.ORIG >dplus.conf
	fi
fi

rm -f dsgw-cert-install.sh
curl -s -O http://www.opendstar.org/.admins/.certs/dsgw-cert-install.sh
chmod +x dsgw-cert-install.sh

if [ $releasename == "AlmaLinux" ]; then
	curl -s -O http://www.opendstar.org/.admins/dplus.sh
	chmod +x dplus.sh
	curl -s -O http://www.opendstar.org/.admins/dplus.service
	chmod +x dplus.service
	cp dplus.service /usr/lib/systemd/system
	systemctl enable dplus
else
	cd /tmp
	curl -s -O http://www.opendstar.org/.admins/dplus_init
	cmp dplus_init /etc/init.d/dplus >/dev/null 2>&1
	if [ ! -f /etc/init.d/dplus -o ! $? -eq 0 ]; then
		chmod 755 dplus_init
		mv dplus_init /etc/init.d/dplus
		chkconfig --add dplus
	fi
fi

cd /tmp
if [ $releasename == "AlmaLinux" ]; then
	if [ ! -d /var/www/html ]; then
		echo "/var/www/html missing on $releasename based system, have you installed dstar?"
		exit 1
	fi
	if [ ! -f /var/www/html/status.html ]; then
		curl -s -O http://www.opendstar.org/.admins/dplus-web-ulf.tgz
		cd /var/www/html
		tar xPzf /tmp/dplus-web-ulf.tgz
		if [ ! -f /var/www/html/index.html ]; then
			ln -s /dstar/tmp/status.html /var/www/html/index.html
		else
			echo "/var/www/html/index.html already exists, you may need to configure that for dplus dashboard"
		fi
	fi
else
	if [ -d /opt/products/dstar/apache/securesite ]; then
		if [ ! -d /opt/products/dstar/apache/securesite/dplus ]; then
			curl -s -O http://www.opendstar.org/.admins/dplus-web.tgz
			tar xPzf dplus-web.tgz
			if [ ! -f /opt/products/dstar/apache/securesite/index.html ]; then
				ln -s /dstar/tmp/status.html /opt/products/dstar/apache/securesite/index.html
			fi
		fi
	else
		curl -s -O http://www.opendstar.org/.admins/dplus-web-ulf.tgz
		cd /var/www/html
		tar xPzf /tmp/dplus-web-ulf.tgz
		if [ ! -f /var/www/html/index.html ]; then
			ln -s /dstar/tmp/status.html /var/www/html/index.html
		fi
	fi
fi

if [ ! -f /dstar/dv/remotesystemlinked.dvtool ]; then
	cd /dstar/dv
	curl -s -O http://www.opendstar.org/.admins/dplus-audio.tgz
	tar xzvf dplus-audio.tgz
fi

DSTARGW=/opt/products/dstar/dstar_gw
DSIPSVD=$DSTARGW/dsipsvd/dsipsvd.conf
GW_CALL=`egrep -e '^ZR_CALLSIGN' $DSIPSVD | sed -e 's/ //g' | awk -F"=" '{print $2}' |sed -e 's/\r//'`

echo ""
echo "dplus $DPLUSVER installed"
echo ""

if [ ! -f /dstar/tmp/dvdonglemessage ]; then
echo "Welcome to the $GW_CALL D-Star repeater" >/dstar/tmp/dvdonglemessage
echo "file /dstar/tmp/dvdonglemessage was created, you may wish to edit your custom changes"
fi

if [ $releasename == "AlmaLinux" ]; then
	systemctl start dplus
else
	service dplus start
fi
