#!/bin/bash
# ircDDB Install Script for Standard Icom-based DStar Gateways
#
# Hans-J. Barthen, DL5DI
# Franz-Josef-Str. 20
# D 56642 Kruft
#
# 2010/05/28 Version 0.8

# Installation information
IRCDDBUSER="ircddb"
IRCDDBHOME="/opt/ircDDB"
IRCDDBINST="/opt/ircDDB/install"
#
# Download-URL
CURL="/usr/bin/curl -4 --connect-timeout 30 --max-time 240"
DOWNLOAD_URL="http://download.prgm.org/dl5di-soft/ircddb"
IRCDDBINIT="install-ircddb-for-icom.tgz"
#
# Database settings
IRCSRV="group1-irc.ircddb.mdx.de"
IRCPORT="9007"
DATABASE="//group1-postgres.ircddb.mdx.de:5432/ircddb"
#
# IRC settings
IRCCHAN="#dstar"
IRCJARUPD="http://group1-update.ircddb.mdx.de/ircDDB.jar"
APPJARUPD="http://group1-update.ircddb.mdx.de/app.jar"
#
# Other setups
JAVPGCONN="postgresql-8.4-701.jdbc3.jar"
DSICONF=/opt/products/dstar/dstar_gw/dsipsvd/dsipsvd.conf
#
#####################################################################

if [ $EUID != 0 ] ; then
    echo "Sorry, the install script must be run as root!"
    echo "Aborting."
    echo " "
    exit
fi

IRCPWD=$1
AUTO=0

if [ $IRCPWD ]; then
    AUTO=1
fi

echo " "
echo "ircDDB Install Script for Standard Icom-based DStar Gateways"
echo " "
echo "Copyright (c) 2010"
echo "Michael Dirska - DL1BFF, Jann Traschewski - DG8NGN, Hans-J. Barthen - DL5DI"
echo " "
echo "This program is free software; you can redistribute it and/or modify"
echo "it under the terms of the GNU General Public License as published by"
echo "the Free Software Foundation; either version 2 of the License, or"
echo "(at your option) any later version."
echo "  "
echo "This program is distributed in the hope that it will be useful,"
echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the"
echo "GNU General Public License for more details."
echo "	"
echo "You should have received a copy of the GNU General Public License"
echo "along with this program; if not, write to the Free Software"
echo "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA"
echo " "
echo "DG8NGN, DL1BFF, DL5DI  28.05.2010"
echo " "

if [ $AUTO -eq 0 ]; then
# ask for password, it was not supplied in the command line

    while [ ! $IRCPWD ]; do
	echo " "
	echo "Please enter the password provided by the ircDDB-team to enter the system:"
	echo " "
	read IRCPWD
    done
fi

echo " "
echo "OK, Password will be set to '$IRCPWD'"

# check if GW_CALL could be found

echo " "
echo "Check requirements:"
echo " "
#
GW_CALL=`egrep -e '^ZR_CALLSIGN' $DSICONF | sed -e 's/ //g' | awk -F"=" '{print $2}' | tr '[A-Z]' '[a-z]' | sed -e 's/\r//'`


if [ ! $GW_CALL ]; then 
    echo "- Missing GW-Call!"
    echo "  This installscript is for a standard Icom Gateway-Installation only!"
    echo -n "You may try it by entering the call manually (lower case!):"
    read GW_CALL
    if [ ! $GW_CALL ]; then 
	exit
    fi
fi

echo "+ Gateway Callsign set to '$GW_CALL'"

# check if DSM is installed
DSMHOME=`find /opt -name dstarmon`

if [ ! $DSMHOME ]; then 
    echo "- Missing DStarMonitor!"
    exit
fi

echo "+ DStarMonitor found at $DSMHOME !"

# check for Java-directory
JAVABIN=`egrep -e '^javadir' $DSMHOME/monstart.sh | sed -e 's/ //g' | awk -F"=" '{print $2}' |sed -e 's/\r//'`

if [ ! $JAVABIN ]; then 
    echo "- Missing Java!"
    echo "  This installscript is for a standard Icom Gateway-Installation!"
    exit
fi

echo "+ Java found at $JAVABIN !"

# check where Postgres is installed
PGSQL=`find /opt -name psql`

if [ ! $PGSQL ]; then 
    echo "  Th installscript is for a standard Icom Gateway-Installation!"
    echo "  This system seems to be different. We will extend our search, it may take a while ...."
    PGSQL=`find / -name psql`
fi

if [ ! $PGSQL ]; then 
    echo "- Missing Postgres, sorry, no chance!"
    exit
fi

echo "+ Postgres found at $PGSQL !"
echo " "
echo "All requirements seem to be OK!"
echo " "

# check if ircDDB is already installed and should be updated

if [ -d $IRCDDBHOME ]; then 

    echo "- ircDDB is obviously already installed on your system!"
    echo " "
    echo "1. Refresh/Update installation"
    echo "2. Delete installation and quit"
    echo "3. Quit"
    echo " "
    echo -n "Please choose selection followed by ENTER: "
    choice=4
    
    while [ $choice -eq 4 ]; do

	if [ $AUTO -eq 0 ]; then
	    read choice
	else
	    choice=1
	fi

	if [ $choice -eq 1 ]; then
	    echo "+ Refreshing existing installation"
	    rm -f $IRCDDBHOME/*;
	else
	    if [ $choice -eq 2 ]; then
		echo "+ Removing existing installation and exiting"
		rm -Rf $IRCDDBHOME;
		exit
	    else
		if [ $choice -eq 3 ]; then
		    echo "+ Exiting without any changes"
    		    exit
		else
		    echo "- ircDDB is already installed on your system!"
		    echo " "
		    echo "1. Refresh/Update installation"
		    echo "2. Delete installation and quit"
		    echo "3. Quit"
		    echo " "
		    echo -n "Please choose selection followed by ENTER: "
		    choice=4
		fi
	    fi
	fi
    done
fi

echo " "
#
echo "Starting installation of ircDDB Init-version for Icom systems on $GW_CALL"
#
echo "+ Create new user '$IRCDDBUSER'"
useradd -c "ircDDB-User" -d $IRCDDBHOME $IRCDDBUSER
#
echo "+ Create home directory '$IRCDDBHOME'"
mkdir $IRCDDBHOME
#
echo "+ Create install directory '$IRCDDBINST'"
mkdir $IRCDDBINST
#
cd $IRCDDBINST
DL=1
if [ -f $IRCDDBINIT ]; then 
    choice=3
    echo " "
    echo "- The initial installation file already exists at $IRCDDBINST/$IRCDDBINIT"
    echo " "
    echo "1. Use the existing file"
    echo "2. Start new Download"
    echo " "
    echo -n "Please choose selection followed by ENTER: "

    while [ $choice -eq 3 ]; do

	if [ $AUTO -eq 0 ]; then
	    read choice
	else
	    choice=2
	fi

	if [ $choice -eq 1 ]; then
	    echo "+ Using the existing file "
	    DL=0
	else
	    if [ $choice -ne 2 ]; then
		echo " "
		echo "- The initial installation file already exists at $IRCDDBINST/$IRCDDBINIT"
	        echo " "
	        echo "1. Use the existing file"
	        echo "2. Start new Download"
	        echo " "
		echo -n "Please choose selection followed by ENTER: "
		choice=3
	    fi	
	fi
    done
fi

if [ $DL -eq 1 ]; then
    echo " "
    echo "+ Download initial installation file '$IRCDDBINIT' to '$IRCDDBINST'"
    echo "Please wait!!"
    echo " "
# delete really all before we start
    rm -f $IRCDDBINST/*
    $CURL -O $DOWNLOAD_URL/$IRCDDBINIT 2>/dev/null
fi

#
echo " "
echo "unpack tar-file"
echo " "
cd /
tar -zxvf $IRCDDBINST/$IRCDDBINIT
echo " "
#
echo "+ Stop DStarMonitor"
/etc/init.d/dsm stop
echo "+ Stop all processes run by user '$IRCDDBUSER'"
killall -u $IRCDDBUSER
#
# add ircDDB database server to DSM properties
echo "+ Saving dstarmonitor.properties"
cp $DSMHOME/dstarmonitor.properties $DSMHOME/dstarmonitor.properties.ircddb-bak
#
echo "+ Saving monstart.sh"
cp $DSMHOME/monstart.sh $DSMHOME/monstart.sh.ircddb-bak
echo "+ Adding Java-Postgres-Connector to class"
cat $DSMHOME/monstart.sh | grep $JAVPGCONN > /dev/null || sed -i "s/ Main/:$JAVPGCONN Main/g" $DSMHOME/monstart.sh
#
echo "+ Installing Java-Postgres-Connector"
cp $IRCDDBINST/$JAVPGCONN $DSMHOME
#
echo "+ Adding IRC-Database to dstarmonitor.properties."

# already installed?
LHPARM=`egrep -e "LHParameters1=user;$GW_CALL;password;$IRCPWD" $DSMHOME/dstarmonitor.properties | sed -e 's/\r//'`
if [ $LHPARM ]; then
    echo "+ Java-Postgres-Connector already installed."
    LHURL=`egrep -e "^LHURI1=jdbc:postgresql:$DATABASE" $DSMHOME/dstarmonitor.properties | sed -e 's/\r//'`
    if [ $LHURL ]; then
	echo "+ DSM-Settings are OK! We can skip this step."
    else
	echo "+ Set new server parameters in DSM-properties."
        cat $DSMHOME/dstarmonitor.properties.ircddb-bak | sed "/^LHURI1=/cLHURI1=jdbc:postgresql:$DATABASE" > $DSMHOME/dstarmonitor.properties
    fi
else
# not yet installed, check if other additional LHDrivers are installed
    LHD1=`egrep -e '^LHDriver1' $DSMHOME/dstarmonitor.properties | sed -e 's/ //g' | awk -F"=" '{print $2}' | sed -e 's/\r//'`
    if [ $LHD1 ]; then
	echo "- LHDriver1 already in use."
	echo "- Sorry, this is no default configuration, please try manual setup of the LHDriver!"
	echo "- Skipping DSM driver setup"
    else
	echo "+ Installing Java-Postgres-Connector"
        # insert driver setting as LHDriver1 after LHParameters of LHDriver(0)
	cat $DSMHOME/dstarmonitor.properties.ircddb-bak | sed "/^LHParameters=/a#\nLHDriver1=org.postgresql.Driver\nLHURI1=jdbc:postgresql:$DATABASE\nLHParameters1=user;$GW_CALL;password;$IRCPWD\n#" > $DSMHOME/dstarmonitor.properties
    fi
fi

echo " "
echo "Configure ircDDB.properties"
echo " "
sed -i "s|<USERNAME>|$GW_CALL|g" $IRCDDBHOME/ircDDB.properties
sed -i "s|<PASSWORD>|$IRCPWD|g" $IRCDDBHOME/ircDDB.properties
sed -i "s|<IRCSRV>|$IRCSRV|g" $IRCDDBHOME/ircDDB.properties
sed -i "s|<IRCPORT>|$IRCPORT|g" $IRCDDBHOME/ircDDB.properties
sed -i "s|<IRCCHAN>|$IRCCHAN|g" $IRCDDBHOME/ircDDB.properties
echo " "
echo "+ Configure ircDDB run-script"
sed -i "s|<DSMHOME>|$DSMHOME|g" $IRCDDBHOME/run.sh
sed -i "s|<JAVABIN>|$JAVABIN|g" $IRCDDBHOME/run.sh
sed -i "s|<IRCSRV>|$IRCSRV|g" $IRCDDBHOME/run.sh
sed -i "s|<IRCPORT>|$IRCPORT|g" $IRCDDBHOME/run.sh
sed -i "s|<IRCCHAN>|$IRCCHAN|g" $IRCDDBHOME/run.sh
sed -i "s|<IRCJARUPD>|$IRCJARUPD|g" $IRCDDBHOME/run.sh
sed -i "s|<APPJARUPD>|$APPJARUPD|g" $IRCDDBHOME/run.sh
sed -i "s|<JAVPGCONN>|$JAVPGCONN|g" $IRCDDBHOME/run.sh
#
echo " "
echo "+ Configure ircDDB.policy"
sed -i "s|<DSMHOME>|$DSMHOME|g" $IRCDDBHOME/ircDDB.policy
sed -i "s|<JAVPGCONN>|$JAVPGCONN|g" $IRCDDBHOME/ircDDB.policy
#
chown -R $IRCDDBUSER $IRCDDBHOME
echo " "
echo "+ Install database trigger"
echo " "
su postgres -c "$PGSQL -U dstar dstar_global < $IRCDDBINST/trigger.txt"
echo " "
echo "Installation complete!"
echo " "
echo "Ready to go ...."
echo " "
echo "+ Start DStarMonitor"
/etc/init.d/dsm start
echo " "
echo "+ Start ircDDB-Client"
cd $IRCDDBHOME
su ircddb -c "nohup ./run.sh >> /tmp/ircdbb.log 2>&1 &"
echo " "
echo " * * * Welcome to the DStar-ircDDB network ! * * *"
exit
