#!/bin/bash
#
# TeamCall CA NetTSPI
# linux-installer.sh
#
# Copyright (c) 2006, 2009, 2017, 2022-2023 by ilink Kommunikationssysteme GmbH.  All Rights Reserved.
#
# This script will call the Java based command line installer for CA NetTSPI
#
# Requirements:
# - The calling user must have root permissions.
# - java needs to be on the search path.
#


PATH=$PATH:/usr/bin:/usr/sbin:/sbin


# Check directory
if [ ! -f "linux-installer.sh" ]; then
	echo ""
	echo "You are not in the correct directory."
	echo "Please change to the installer directory."
	echo "Installer cancelled"
	echo ""
	exit 1
fi

# Check for root permissions
perm=`id -u`
if [ "$perm" != "0" ]; then
	echo ""
	echo "You must be root to start the installer"
	echo "Installer cancelled"
	echo ""
	exit 1
fi


# Default values of CA config parameters
NETTSPI_PORT="20000"
NETTSPI_HOSTNAME="localhost"
SERVICE_PORT="26001"
SERVICE_SUFFIX="1"

INSTALLCONFIG="auto-install.xml"        # File name of the install config file
INSTALLERFILE="ca_nettspi_install*.jar" # File name of the Izpack install pkg (this variable may not be used within quotes)
INSTALLLOGFILE="CANetTSPI-install.log"  # File name of the install log file
INSTALLPATH="/usr/local/ilink/CA NetTSPI"


# Variables install script
#DEBUGOPTION="-DDEBUG=true -DTRACE=true -DSTACKTRACE=true"
DEBUGOPTION="-DTRACE=true -DSTACKTRACE=true"
checkParameter="0"


echo "Starting CA NetTSPI Linux install script"


#================================================================
# Read and confirm all parameters
#================================================================

while [ "$checkParameter" != "OK" ]
do
	echo ""

	echo -e "LinkTSP host [$NETTSPI_HOSTNAME]:\c "
	read NETTSPI_HOSTNAME_TMP

	if [ "$NETTSPI_HOSTNAME_TMP" != "" ]; then
		NETTSPI_HOSTNAME="$NETTSPI_HOSTNAME_TMP"
	fi

	echo -e "LinkTSP port [$NETTSPI_PORT]:\c "
	read NETTSPI_PORT_TMP

	if [ "$NETTSPI_PORT_TMP" != "" ]; then
		NETTSPI_PORT="$NETTSPI_PORT_TMP"
	fi

	echo -e "CA service port [$SERVICE_PORT]:\c "
	read SERVICE_PORT_TMP

	if [ "$SERVICE_PORT_TMP" != "" ]; then
		SERVICE_PORT="$SERVICE_PORT_TMP"
	fi

	echo -e "CA node suffix [$SERVICE_SUFFIX]:\c "
	read SERVICE_SUFFIX_TMP

	if [ "$SERVICE_SUFFIX_TMP" != "" ]; then
		SERVICE_SUFFIX="$SERVICE_SUFFIX_TMP"
	fi

	echo -e "CA installation path [$INSTALLPATH]:\c "
	read INSTALLPATH_TMP

	if [ "$INSTALLPATH_TMP" != "" ]; then
		INSTALLPATH="$INSTALLPATH_TMP"
	fi

	echo ""
	echo "---------------------------------------------"
	echo "          Check our settings!"
	echo ""
	echo " LinkTSP host:          $NETTSPI_HOSTNAME"
	echo " LinkTSP port:          $NETTSPI_PORT"
	echo " CA service port:       $SERVICE_PORT"
	echo " CA node suffix:        $SERVICE_SUFFIX"
	echo " CA installation path:  $INSTALLPATH"
	echo ""
	echo "---------------------------------------------"

	checker="0"
	echo -e "Are these settings correct? Then type OK: \c"
	read checker

	if [ "$checker" == "OK" ]; then
		checkParameter="OK"
	fi
done


# Create a configuration file for automatic installation
echo; echo "Creating configuration file $INSTALLCONFIG"
cat > "$INSTALLCONFIG" << EOF
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<AutomatedInstallation langpack="eng">
	<com.izforge.izpack.panels.hello.HelloPanel id="HelloPanel_0" />
	<com.izforge.izpack.panels.userinput.UserInputPanel id="UserInput.panel">
		<entry key="SERVER_PORT" value="$SERVICE_PORT" />
		<entry key="NETTSPI_PORT" value="$NETTSPI_PORT" />
		<entry key="NETTSPI_HOSTNAME" value="$NETTSPI_HOSTNAME" />
		<entry key="SERVICE_SUFFIX" value="$SERVICE_SUFFIX" />
	</com.izforge.izpack.panels.userinput.UserInputPanel>
	<com.izforge.izpack.panels.target.TargetPanel id="TargetPanel_2">
		<installpath>$INSTALLPATH</installpath>
	</com.izforge.izpack.panels.target.TargetPanel>
	<com.izforge.izpack.panels.install.InstallPanel id="InstallPanel_3" />
	<com.izforge.izpack.panels.finish.FinishPanel id="FinishPanel_4" />
</AutomatedInstallation>

EOF


# Start the IzPack installer, using the automatic installation configuration data generated above
# - java needs to be on the search path.
echo; echo "Starting CA NetTSPI installer"

echo "> java $DEBUGOPTION -jar $INSTALLERFILE -defaults-file \"$INSTALLCONFIG\""
java $DEBUGOPTION -jar $INSTALLERFILE "$INSTALLCONFIG" | tee "$INSTALLLOGFILE" | egrep '\(./.\)'


# Remove created configuration file
rm $INSTALLCONFIG


# Clean rubbish from logfile
mv "$INSTALLLOGFILE" logfile_tmp
cat logfile_tmp | grep -v "current OS" > "$INSTALLLOGFILE"
rm logfile_tmp

echo; echo "Generated installer log file:"
echo "> ls -l \"$INSTALLLOGFILE\""
ls -l "$INSTALLLOGFILE"
echo

