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


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


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

# Check for root permissions
perm=$(id -u)
if [ "$perm" != "0" ]; then
	echo ""
	echo "You must have root permissions to start the installer."
	echo "Installation cancelled"
	echo ""
	exit 1
fi


# Default values of CA config parameters
NFON_API_USERNAME="NFON API Username"
NFON_API_PWD="NFON API Password"
SERVICE_PORT="26001"

INSTALLCONFIG="auto-install.xml"     # File name of the install config file
INSTALLERFILE="ca_nfon_install*.jar" # File name of the Izpack install pkg (this variable may not be used within quotes)
INSTALLLOGFILE="CANFON-install.log"  # File name of the install log file
INSTALLPATH="/Applications/TeamCall/CA NFON" # Default installation path


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



  echo; echo "Starting CA NFON macOS install script"
( echo; echo "Starting CA NFON macOS install script" ) > "$INSTALLLOGFILE"


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

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

	echo -e "NFON API username [$NFON_API_USERNAME]:\c "
	read NFON_API_USERNAME_TMP

	if [ "$NFON_API_USERNAME_TMP" != "" ]; then
		NFON_API_USERNAME="$NFON_API_USERNAME_TMP"
	fi

	echo -e "NFON API password [$NFON_API_PWD]:\c "
	read NFON_API_PWD_TMP

	if [ "$NFON_API_PWD_TMP" != "" ]; then
		NFON_API_PWD="$NFON_API_PWD_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 installation path [$INSTALLPATH]:\c "
	read INSTALLPATH_TMP

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

	echo ""
	echo "---------------------------------------------"
	echo "          Check our settings!"
	echo ""
	echo " NFON API username:    $NFON_API_USERNAME"
	echo " NFON API password:    $NFON_API_PWD"
	echo " CA service port:      $SERVICE_PORT"
	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

(
	echo ""
	echo "----------------------------------------------------------------"
	echo " Using these settings:"
	echo ""
	echo " NFON API username:    $NFON_API_USERNAME"
	echo " NFON API password:    $NFON_API_PWD"
	echo " CA service port:      $SERVICE_PORT"
	echo " CA installation path: $INSTALLPATH"
	echo ""
	echo "----------------------------------------------------------------"
) >> "$INSTALLLOGFILE"



# Create a configuration file for automatic installation
  echo; echo; echo "Creating configuration file $INSTALLCONFIG:"
( echo; echo; echo "Creating configuration file $INSTALLCONFIG" ) >> "$INSTALLLOGFILE"

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="NFON_API_USERNAME" value="$NFON_API_USERNAME" />
		<entry key="NFON_API_PWD" value="$NFON_API_PWD" />
	</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

(
	echo "----------------------------------------------------------------"
	cat $INSTALLCONFIG
	echo "----------------------------------------------------------------"
) >> "$INSTALLLOGFILE"



# Delete previous installation
  echo; echo; echo "Deleting previous installation"
( echo; echo; echo "Deleting previous installation" ) >> "$INSTALLLOGFILE"

rm -rf "$INSTALLPATH"
rm -f /Library/LaunchDaemons/de.ilink.TeamCall.CA-NFON.plist



# Start the IzPack installer, using the automatic installation configuration data generated above
# - java needs to be on the search path.
  echo; echo; echo "Starting CA NFON installer"
( echo; echo; echo "Starting CA NFON installer" ) >> "$INSTALLLOGFILE"

JAVA=java
  echo; echo "> $JAVA $DEBUGOPTION -jar $INSTALLERFILE -defaults-file \"$INSTALLCONFIG\""
( echo; echo "> $JAVA $DEBUGOPTION -jar $INSTALLERFILE -defaults-file \"$INSTALLCONFIG\"" ) >> "$INSTALLLOGFILE"
$JAVA $DEBUGOPTION -jar $INSTALLERFILE "$INSTALLCONFIG" 2>&1 | tee "${INSTALLLOGFILE}2" | egrep '\(./.\)'
RETVAL1=$?
if [ $RETVAL1 -ne 0 ]; then
	which $JAVA  > /dev/null
	RETVAL2=$?
	echo ""
	if [ $RETVAL2 -ne 0 ]; then
		echo "$JAVA not found."
		echo "The Java runtime does not seem to be installed or $JAVA is not on the search path."
		echo "$JAVA not found." >> "$INSTALLLOGFILE"
		echo "The Java runtime does not seem to be installed or $JAVA is not on the search path." >> "$INSTALLLOGFILE"
	else
		echo "The installer failed (code $RETVAL1)."
		echo "Check log file $INSTALLLOGFILE for details."
		echo "The installer failed (code $RETVAL1)." >> "$INSTALLLOGFILE"
	fi
	echo "Installation cancelled"
	echo ""
	echo "Installation cancelled" >> "$INSTALLLOGFILE"
	echo "" >> "$INSTALLLOGFILE"
	exit 1
fi


# Create log folder

echo
echo "" >> "$INSTALLLOGFILE"
echo "> mkdir -p \"$INSTALLPATH/log\""
echo "> mkdir -p \"$INSTALLPATH/log\"" >> "$INSTALLLOGFILE"
        mkdir -p  "$INSTALLPATH/log"
echo "> chmod 755 \"$INSTALLPATH/log\""
echo "> chmod 755 \"$INSTALLPATH/log\"" >> "$INSTALLLOGFILE"
        chmod 755  "$INSTALLPATH/log"


# Consolidate individual logs into $INSTALLLOGFILE and remove individual logs
(
	echo ""
	echo ""
	echo "Installer log:"
	echo "----------------------------------------------------------------"
	cat ${INSTALLLOGFILE}2
	echo "----------------------------------------------------------------"

	echo ""
	echo ""
	echo "Installer warnings and errors:"
	echo "----------------------------------------------------------------"
	cat izpack.log
	echo "----------------------------------------------------------------"

	echo ""
	echo ""
	echo "setup-CANFON log:"
	echo "----------------------------------------------------------------"
	cat setup-CANFON.log
	echo "----------------------------------------------------------------"
	echo ""
	echo ""
) >> "$INSTALLLOGFILE"

rm -f ${INSTALLLOGFILE}2
rm -f izpack.log
rm -f setup-CANFON.log


# Remove installer configuration file that was generated above
echo; echo; echo "Removing configuration file $INSTALLCONFIG"
( echo; echo; echo "Removing configuration file $INSTALLCONFIG" ) >> "$INSTALLLOGFILE"
rm -f $INSTALLCONFIG


echo; echo; echo "NOTE: The service has been installed but has not yet been started"
( echo; echo; echo "NOTE: The service has been installed but has not yet been started"; echo; echo ) >> "$INSTALLLOGFILE"


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

