#! /bin/bash

INSTALL_DIR="`dirname \"$0\"`"
cd "$INSTALL_DIR"

. .includes/installer.h


#Obtain PID of core server executables to determine if they are running.
FCSD_PID=`find_pid "fcsd"`
FCISD_PID=`find_pid "fcisd"`


#Obtain CPU type
get_cpu_type
get_brand_info


#Display Installer Header text to the user.
displayHeader


cd "$MY_FILES"
FCSD_TAR="$MY_FILES/`echo $FCSD_NAME*.tar.gz`"
FCISD_TAR="$MY_FILES/`echo $FCISD_NAME*.tar.gz`"
cd ..


#Define the Installer Type based on included components
defineInstallerType


#Display Installer Name to the User
displayInstallerName


#Perform installer Prerequisite Checks 
doPrerequisiteChecks



#If addon script included as part of the package, include the addon installer script functions
if [ -e "$ADDON_TAR" ]; then
	includeAddonScript
fi


#Show what is included in the Installer
displayIncludedFiles



# Assume no FCNS upgrade by default...
UPGRADE_FCNS=0



#Request User input do determine what needs to be installed.
getInstallOptions



#Warn user if installer is a fresh installer, but an existing Network store was found to be installed.
if [ "$INSTALL_CS" -eq "1" ] ; then
	if [ -e "$PRIMARY_FCNS" ]; then
		if [ ! -e "$UPGRADE_TAR" ]; then
			if [ -e "$FCNSBUS_TAR" ] || [ -e "$FCNSEDU_TAR" ] || [ -e "$FCNSOTSM_TAR" ] || [ -e "$FCNSARCH_TAR" ]; then
				warnThisIsNotUpgradeInstaller
			fi
		fi
	fi
fi



# If installing FCS or IS ensure destination service folder exists and set flag to set file permissions and update desktop icons.
if [ "$INSTALL_CS" = "1" ] || [ "$INSTALL_IS" = "1" ]; then
        SET_APP_PERMISSIONS="1"
        UPDATE_DESKTOP="1"
        createServiceFolder
else
	SET_APP_PERMISSIONS="0"
        UPDATE_DESKTOP="0"
fi




#If core FCS server included, run core server fresh/upgrade installer  script.
if [ "$INSTALL_CS" -eq "1" ] ; then
	installCoreServer
fi




#If FCIS included, run FCIS fresh/upgrade installer script.
if [ "$INSTALL_IS" -eq "1" ] ; then
	runFCIS_Installer

fi




#If addon installer included AND no other fcns upgrade scripts, then run addon Installer script
if [ -e "$ADDON_TAR" ]; then
        if [ ! -e "$UPGRADE_TAR" ]; then
               # tar xz -C "$MY_FILES" -f "$ADDON_TAR" $ADDON_SCRIPT
               runAddonInstaller
        fi
fi




#Install End User License Agreement
installEULA



#If FCNS upgrade scripts were included as part of the core installer then install them.
if [ "$UPGRADE_FCNS" -eq "1" ] ; then
	installUpgradeBatchFiles
fi




#Set proper permissions on installed apps if required
if [ "$SET_APP_PERMISSIONS" -eq "1" ]; then
	setAppPermissions
fi




#Set proper permissions on Volumes or Batch folder contents if required.
if [ "$VERIFY_PO" -eq "1" ] ; then
	setVolumePermissions
elif [ "$VERIFY_PO" -eq "2" ] ; then
	setBatchPermissions
fi



#Updates Desktop if Required
if [ "$UPDATE_DESKTOP" -eq "1" ] ; then
	updateDesktop
fi



#Display Final Instructions to user
echo "_____"
echo ""
if [ "$UPGRADE_FCNS" -eq "1" ] ; then
    display_UpgradePhaseComplete
	echo ""
	display_BatchOnStartup_Instructions
elif [ "$BATCH_FILES_INSTALLED" -eq "1" ] ; then
	if [ "$INSTALL_ADDON_EXECUTABLES" -eq "1" ]; then
		if [ "$ADDON_INSTRUCTIONS" = "DEFINED" ] ; then
			addon_Display_Instructions
		fi
	else
		display_UpgradePhaseComplete
	fi
	
	echo ""
	if [ -e "$FCSD_TAR" ]; then
		display_BatchOnStartup_Instructions
	else	
	    display_BatchUpgrade_Instructions
	fi
elif [ "$ADDON_INSTRUCTIONS" = "DEFINED" ] ; then
	addon_Display_Instructions
else	
    displayInstallComplete
fi

#Display footer text to user
displayFooter
