#!/bin/bash
# Script to launch the LicenseManager installer

getMajorMinorVersion()
{
	# given aa.bb.cc, strip .cc, return aa.bb
	echo $1 | sed 's/\([0-9][0-9]*\)\.\([0-9][0-9]*\)\.[0-9][0-9]*$/\1\.\2/'
}

# Product Info
SimpleVersion=2023.0
ProductVersion="${SimpleVersion}"
BaseVersion=` getMajorMinorVersion ${SimpleVersion} `
PreviousVersions="2022.1 2022.0 2021.1 2021.0 2020.1 2020.0 2019.2 2019.1 2019.0 2018.1 2018.0 10.6 10.5.1 10.5 10.4.1 10.4 10.3.1 10.3 10.2.2 10.2.1 10.2"
ProductName="ArcGIS License Manager $ProductVersion"
# PreviousProductName initialized later
product=LicenseManager
shortname="license"
InstallName="licensemanager"
Esri_Hostname=` hostname `; export Esri_Hostname
platform=Linux

SILENT_INFO_TEXT="For information on the silent setup UI mode, please run this script with the -h or --help option."

# Installer configuration
locale -a | grep "^en_US.utf8$" >/dev/null 2>&1
if [ "$?" = "0" ]
then
    LANG=en_US.UTF-8; export LANG
    LC_CTYPE=en_US.UTF-8; export LC_CTYPE
fi

# Setup functions
error_header() {
    echo "[$ProductName Setup Error]" >&2
}

warning_header() {
    echo "[$ProductName Setup Warning]"
}

# Usage & Examples text
usage () {
    echo "Usage: Setup [OPTION]..."
    echo "  -m, --mode MODE            Optional. Defaults to GUI mode."
    echo "                             MODE is either silent or gui:"
    echo "                               silent:  Performs a silent install."
    echo "                                        See Silent Options section below."
    echo "                               gui:     X display is required."
    echo
    echo "  -v, --verbose              Installer runs in verbose mode."
    echo
    echo "  -h, --help                 Display this help and exit."
    echo
    echo "  -e, --examples             Display usage examples of these options and exit."
    echo
    echo
    echo "Silent Options - the following options are only used for silent mode installs:"
    echo "  -l, --license-agreement CHOICE"
    echo "                             Required for silent mode. CHOICE is one of Yes or No."
    echo "                             Yes indicates that you have read and agreed to the"
    echo "                             Esri Master Agreement (E204, E300). Please visit"
    echo "                             http://www.esri.com/legal/licensing-translations"
    echo "                             to read the agreement."
    echo
    echo "  -d, --directory DIRECTORY  Optional. By default, $ProductName will be"
    echo "                             installed to your \$HOME directory. DIRECTORY"
    echo "                             specifies a different installation directory."
    echo "                             Note: The path /arcgis/${InstallName} will be"
    echo "                             appended to the installation directory."
    echo
}

usage_examples() {
    echo "$ProductName Setup script EXAMPLES:"
    echo
    echo "This will run the installer in GUI mode, allowing the user to make all"
    echo "selections at install time."
    echo "  Setup"
    echo 
    echo "These will run the installer in Silent mode, using the default installation"
    echo "directory \$HOME/arcgis/${InstallName}."
    echo "Note that these two commands perform the same actions."
    echo "  Setup -m silent -l yes"
    echo "  Setup --mode silent --license-agreement yes"
    echo 
    echo "This will run the installer in Silent mode, using the installation directory"
    echo "/opt/arcgis/${InstallName}."
    echo "  Setup -m silent -l yes -d /opt/arcgis/${InstallName}"
    echo
}

# create temporary file or directory, even if mktemp is not available
mktemp_custom() {
	mktemp_loc=` which mktemp 2>/dev/null | grep -v "no mktemp" `

	if [ "x$1" = "x-d" ] # create temp directory
	then
		if [ "x${mktemp_loc}" != "x" ]
		then	
    		new_temp_dir="` mktemp -d `"
		else
			if [ "x$TMPDIR" != "x" ] && [ -d $TMPDIR ] && [ -w $TMPDIR ]
			then
				new_temp_dir="/$TMPDIR/lm$$` perl -e 'print time()' `"
			else
				new_temp_dir="/tmp/lm$$` perl -e 'print time()' `"
			fi

			mkdir "$new_temp_dir"
			chmod 700 "$new_temp_dir"
		fi
		echo "${new_temp_dir}"
	else # create temp file
		if [ "x${mktemp_loc}" != "x" ]
		then	
    		new_temp_file="` mktemp `"
		else
			if [ "x$TMPDIR" != "x" ] && [ -d $TMPDIR ] && [ -w $TMPDIR ]
			then
				new_temp_file="/$TMPDIR/tmp$$` perl -e 'print time()' `"
			else
				new_temp_file="/tmp/tmp$$` perl -e 'print time()' `"
			fi

			touch "$new_temp_file"
			chmod 700 "$new_temp_file"
		fi
		echo "${new_temp_file}"
	fi
}

create_prop_file() {
    # Create the temp file using mktemp
    Esri_Silent_Property_File="`mktemp_custom`"

    # Set the file to be removed on exiting the script
    trap 'rm -f $Esri_Silent_Property_File' EXIT

    # Check that file exists
    if [ ! -f $Esri_Silent_Property_File ]
    then
        error_header
        echo "Could not create temporary file for silent install." >&2
        echo "Please check that your user id has file creation permissions for the mktemp utility." >&2
        echo "Exiting." >&2
        exit 1;
    fi

    # Check that file is writeable
    if [ -w $Esri_Silent_Property_File ]
    then
        # Create our temp prop file to pass in to installer
cat << EOF >> $Esri_Silent_Property_File
# Esri $ProductName Silent Install Property File
USER_INSTALL_DIR=$Esri_Install_Directory
INSTALL_TYPE=$Esri_Install_Type
EOF
    else
        error_header
        echo "Could not write to temporary file $Esri_Silent_Property_File for silent install." >&2
        echo "Exiting." >&2
        exit 1;
    fi
}

append_to_install_dir() {
    # Strip any / from end of string
    Esri_Install_Directory=`echo $Esri_Install_Directory | sed "s;/$;;"`

    # Check if it ends with /arcgis
    echo $Esri_Install_Directory | grep "\/arcgis$" >/dev/null 2>&1

    # If so, append /${InstallName}
    if [ "$?" = "0" ]
    then
        Esri_Install_Directory=${Esri_Install_Directory}/${InstallName}
    fi

    # Check now if it ends with /arcgis/${InstallName}
    echo $Esri_Install_Directory | grep "\/arcgis\/${InstallName}$" >/dev/null 2>&1

    # If not, append /arcgis/${InstallName}
    if [ "$?" != "0" ]
    then
        Esri_Install_Directory=${Esri_Install_Directory}/arcgis/${InstallName}
    fi
}

check_valid_install_dir() {
    # Check for / at start of string
    if [ ` echo $Esri_Install_Directory | cut -c1 ` != "/" ]
    then
        error_header
        echo "Invalid argument to -d or --directory. The install directory" >&2
        echo "must be an absolute path. It should start with the / character." >&2
        echo "Exiting." >&2
        exit 1
    fi

    Check_Dir=$Esri_Install_Directory
    # Strip any / from end of string
    Check_Dir=`echo $Check_Dir | sed "s;/$;;"`

    # Check that this user can write to the directory
    if [ -d $Check_Dir ]
    then
        if [ ! -w $Check_Dir ]
        then
            error_header
            echo "This user does not have write permissions to the install directory provided." >&2
            echo "Please change the permissions or choose a different install directory." >&2
            echo >&2
            echo "User................`id | cut -d\( -f2 | cut -d\) -f1 | head -1`" >&2
            echo "Install Directory...$Check_Dir" >&2
            exit 1
        fi
    else
        # /${InstallName} dir doesn't exist, check next level up
        Check_Dir=`echo $Check_Dir | sed "s;/${InstallName}$;;"`
        if [ -d $Check_Dir ]
        then
            if [ ! -w $Check_Dir ]
            then
                error_header
                echo "This user does not have write permissions to the install directory provided." >&2
                echo "Please change the permissions or choose a different install directory." >&2
                echo >&2
                echo "User................`id | cut -d\( -f2 | cut -d\) -f1 | head -1`" >&2
                echo "Install Directory...$Check_Dir" >&2
                exit 1
            fi
        else
            # /arcgis dir doesn't exist, check next level up
            Check_Dir=`echo $Check_Dir | sed "s;/arcgis$;;"`
            if [ -d $Check_Dir ]
            then
                if [ ! -w $Check_Dir ]
                then
                    error_header
                    echo "This user does not have write permissions to the install directory provided." >&2
                    echo "Please change the permissions or choose a different install directory." >&2
                    echo >&2
                    echo "User................`id | cut -d\( -f2 | cut -d\) -f1 | head -1`" >&2
                    echo "Install Directory...$Check_Dir" >&2
                    exit 1
                fi
            else
                # Appended /arcgis & /${InstallName} dir don't exist, and their parent doesn't exist. This directory must exist before the setup is run.
                error_header
                echo "Install directory [$Check_Dir] does not exist. Please create this directory first." >&2
                echo "Exiting." >&2
                exit 1
            fi
        fi
    fi
}

# Pre-requisite checks

# OS must be Linux or Solaris
case `uname` in
    Linux)   platform=Linux;;
    SunOS)   platform=Solaris;;
    Solaris) platform=Solaris;;
    *)       error_header
             echo "The $ProductName setup is only supported on Linux or Solaris." >&2
             echo "Exiting." >&2
             exit 1;;
esac

# User must not be root
if [ "$platform" = "Solaris" ]
then
	USER_ID=` id | sed -e 's/uid=//' -e 's/(.*$//' `
else
	USER_ID=` id -u `
fi
if [ $USER_ID -eq 0 ]
then
    error_header
    echo "The $ProductName setup cannot run as the root user. Please switch to another user and run this setup again." >&2
    echo "Exiting." >&2
    exit 1
fi

# check for incompatible RH6.0 kernel version
if [ ` grep "Red Hat Enterprise Linux Server release 6.0" /etc/redhat-release >/dev/null 2>&1; echo $? ` -eq 0 ]
then
    KERNEL_VERSION=` uname -r `
    if [ ` echo ${KERNEL_VERSION} | grep 2.6.32-71. >/dev/null 2>&1; echo $? ` -eq 0 ]
    then
        KERNEL_SUBVERSION=` echo ${KERNEL_VERSION} | sed 's;2\.6\.32-71\.;;' `
        if [ ` echo ${KERNEL_SUBVERSION} | cut -f1 -d. ` = "el6" ]
        then
            error_header
            echo "The $ProductName setup cannot run on kernel version ${KERNEL_VERSION}. Please upgrade to kernel 2.6.32-71.14.1 or greater." >&2
            echo "Exiting." >&2
            exit 1
        elif [ ` echo $KERNEL_SUBVERSION | cut -f1 -d. | grep [^0-9] >/dev/null 2>&1; echo $? ` -eq 0 ] || [ ` echo $KERNEL_SUBVERSION | cut -f2 -d. | grep [^0-9] >/dev/null 2>&1; echo $? ` -eq 0 ]
        then
            error_header
            echo "The $ProductName setup needs to run on kernel version 2.6.32-71.14.1 or greater. $ProductName setup cannot determine if detected kernel version ${KERNEL_VERSION} is sufficient." >&2
        else
            if [ ` echo $KERNEL_SUBVERSION | cut -f1 -d. ` -eq 14 ]
            then
                if [ ` echo $KERNEL_SUBVERSION | cut -f2 -d. ` -lt 1 ]
                then
                    error_header
                    echo "The $ProductName setup cannot run on kernel version ${KERNEL_VERSION}. Please upgrade to kernel 2.6.32-71.14.1 or greater." >&2
                    echo "Exiting." >&2
                    exit 1
                fi
            elif [ ` echo $KERNEL_SUBVERSION | cut -f1 -d. ` -lt 14 ]
            then
                error_header
                echo "The $ProductName setup cannot run on kernel version ${KERNEL_VERSION}. Please upgrade to kernel 2.6.32-71.14.1 or greater." >&2
                echo "Exiting." >&2
                exit 1
            fi
        fi
    fi
fi


# Parse command line arguments
# Set defaults
Esri_UI_Mode=gui
Esri_Install_Directory=$HOME
Esri_Install_Type=Complete
Esri_Verbose=false
Esri_License_Agree=no

# Get args
while [ "x${1}X" != "xX" ]; do
  case "$1" in
    -m|--mode)
      Esri_UI_Mode=`echo $2 | tr [A-Z] [a-z]`
      if [ -z "$2" ] || [ "$Esri_UI_Mode" != "gui" -a "$Esri_UI_Mode" != "silent" ]
      then
        error_header
        echo "Empty or invalid argument to -m or --mode detected. See --help option for more details." >&2
        echo "Exiting." >&2
        exit 1
      else
        shift 2
      fi
      ;;
    -l|--license-agreement)
      Esri_License_Agree=`echo $2 | tr [A-Z] [a-z]`
      if [ -z "$2" ] || [ "$Esri_License_Agree" != "yes" -a "$Esri_License_Agree" != "no" ]
      then
        error_header
        echo "Empty or invalid argument to -l or --license-agreement detected. See --help option for more details." >&2
        echo "Exiting." >&2
        exit 1
      else
        shift 2
      fi
      ;;
    -d|--directory)
      if [ -z "$2" ] || [ `echo $2 | cut -c1 ` = "-" ]
      then
        error_header
        echo "Empty or invalid argument to -d or --directory detected. See --help option for more details." >&2
        echo "Exiting." >&2
        exit 1
      else
        Esri_Install_Directory="$2"
        append_to_install_dir
        check_valid_install_dir # This call should be after append_to_install_dir so we know that /arcgis/${InstallName} is at end of string
        shift 2
      fi
      ;;
    -v|--verbose)
      Esri_Verbose=true
      shift 1
      # Turns on InstallAnywhere debug statements
      LAX_DEBUG=true; export LAX_DEBUG
      ;;
    -h|--help)
      usage
      exit
      ;;
    -e|--examples)
      usage_examples
      exit
      ;;
    *)
      error_header
      echo "Error: Unknown option: $1" >&2
      exit 1
      ;;
  esac
done

# File Information
# Determine the path to the CD root directory.
ScriptDir=`dirname "$0"`
ScriptName=`basename "$0"`
if echo ${ScriptDir} | grep '^./' >/dev/null 2>&1
then
    ScriptPath=`pwd``echo ${ScriptDir} | cut -d '.' -f2-`
elif echo ${ScriptDir} | grep '^/' >/dev/null 2>&1
then
    ScriptPath=${ScriptDir}
elif [ "${ScriptDir}" = "." ]
then
    ScriptPath=`pwd`
else
    ScriptPath=`pwd`/${ScriptDir}
fi

# detect previous version(s)
PreviousVersion_Detected=false
for PreviousVersion in ${PreviousVersions}
do
    PreviousProductName="ArcGIS License Manager $PreviousVersion"
    PreviousVersion_Prop_File="$HOME/.ESRI.properties.` uname -n `.${PreviousVersion}"
    ProductInstallDir=""
    if [ -f ${PreviousVersion_Prop_File} ]
    then
        ProductInstallDir=` grep Z_${product}_INSTALL_DIR ${PreviousVersion_Prop_File} | cut -f2 -d= `

        DetectedSPLevel=""
        if [ "x${ProductInstallDir}" != "x" ]
        then
            if [ -d ${ProductInstallDir} ]
            then
                PreviousVersion_Detected=true
                ProductPatchLog="${ProductInstallDir}/.ESRI_LM_PATCH_LOG"
                if [ -f ${ProductPatchLog} ]
                then
                    GetSPNumber="False"
                    while read line
                    do
                        if [ "` echo ${line} | grep ^QFE_TYPE | sed 's/^QFE_TYPE: //' `" = "Service Pack" ]
                        then
                            GetSPNumber="True"
                            continue
                        fi

                        if [ "${GetSPNumber}" = "True" ] && [ "` echo ${line} | grep ^QFE_TITLE `x" != "x" ]
                        then
                            DetectedSPLevel=` echo ${line} | sed 's/^QFE_TITLE: //' `
                            break
                        fi
                    done < ${ProductPatchLog}
                fi
                break
            fi
        else
            InstalledProductDirs=` grep Z_[A-Za-z]*_INSTALL_DIR ${PreviousVersion_Prop_File} | cut -f2 -d= | sort | uniq `
            if [ "x${InstalledProductDirs}" != "x" ]
            then
                echo "Other ${PreviousVersion} product(s) were detected at:"
                echo
                echo "${InstalledProductDirs}"
                echo
                echo "Please uninstall or upgrade these product(s) before installing"
                echo "${ProductName}."
                exit 1
            fi
        fi
    fi
done

if [ ${PreviousVersion_Detected} = "true" ] # begin upgrade
then
	PreviousBaseVersion=` getMajorMinorVersion ${PreviousVersion} `
    Esri_Install_Directory=` echo ${ProductInstallDir} | sed "s;/${shortname}${PreviousBaseVersion}$;;" | sed "s;/${InstallName}$;;" `
    if [ "x${DetectedSPLevel}" != "x" ]
    then
        DetectedPreviousProductName="${PreviousProductName} SP${DetectedSPLevel}"
    else
        DetectedPreviousProductName="${PreviousProductName}"
    fi

    echo "==================================================================="
    echo "${ProductName} ($platform)"
    echo "==================================================================="
    echo ""
    echo "Your ${DetectedPreviousProductName} is installed at:"
    echo ""
    echo " ${Esri_Install_Directory}"
    echo ""
    echo "Confirm Settings"
    echo "==================================================================="
    echo "Product to upgrade:        ${DetectedPreviousProductName} ($platform)"
    echo "Location to upgrade:       ${Esri_Install_Directory}"
    echo ""
    echo "Your ${DetectedPreviousProductName} will be stopped when performing the upgrade"
    echo "and ${ProductName} will be started after the upgrade completes."
    echo ""
    if [ "$Esri_UI_Mode" = "silent" ]
    then
	echo "Upgrade will automatically continue in 10 seconds..."
        sleep 10
    else
        echo " 'y' to continue with these settings"
        echo " 'q' to exit without upgrading this product"
        echo ""
        echo "Enter choice [y,q] (y): "
        read upgrade_choice

        if [ "$upgrade_choice" = "q" ] || [ "$upgrade_choice" = "Q" ]
        then
            echo "Exiting script..."
            exit 1
        fi
    fi

    echo "Applying ${ProductName} to ${Esri_Install_Directory}."
    echo "Please wait..."

    # Back up config
    PreviousVersion_Backup_Dir=`mktemp_custom -d`
    trap 'rm -rf $PreviousVersion_Backup_Dir' EXIT

    # backup user files
    mkdir $PreviousVersion_Backup_Dir/bin
    if [ -d ${Esri_Install_Directory}/${shortname}${PreviousBaseVersion} ]
    then
        cp -p ${Esri_Install_Directory}/${shortname}${PreviousBaseVersion}/bin/service.txt $PreviousVersion_Backup_Dir/bin/ 2>/dev/null
        cp -p ${Esri_Install_Directory}/${shortname}${PreviousBaseVersion}/bin/ARCGIS.opt $PreviousVersion_Backup_Dir/bin/ 2>/dev/null
        cp -p ${Esri_Install_Directory}/${shortname}${PreviousBaseVersion}/bin/options.cfg $PreviousVersion_Backup_Dir/bin/ 2>/dev/null
    else
        cp -p ${Esri_Install_Directory}/${InstallName}/bin/service.txt $PreviousVersion_Backup_Dir/bin/ 2>/dev/null
        cp -p ${Esri_Install_Directory}/${InstallName}/bin/ARCGIS.opt $PreviousVersion_Backup_Dir/bin/ 2>/dev/null
        cp -p ${Esri_Install_Directory}/${InstallName}/bin/options.cfg $PreviousVersion_Backup_Dir/bin/ 2>/dev/null
    fi

    if [ ` echo "${PreviousBaseVersion}<2020.1" | bc ` -gt 0 ]
    then
        # export authorization reg entries in subshell
        ( . ${ProductInstallDir}/.Setup/init_mwrt.sh; regedit -c -e $PreviousVersion_Backup_Dir/LicenseManager${PreviousVersion}.reg "HKEY_LOCAL_MACHINE\Software\ESRI\ArcGIS ${PreviousBaseVersion} License Manager"; regedit -c -e $PreviousVersion_Backup_Dir/FLEXlm.reg "HKEY_LOCAL_MACHINE\Software\FLEXlm License Manager\ArcGIS License Manager" )

        if [ ! -f $PreviousVersion_Backup_Dir/LicenseManager${PreviousVersion}.reg ]
        then
            ( . ${ProductInstallDir}/.Setup/init_mwrt.sh; regedit -c -e $PreviousVersion_Backup_Dir/LicenseManager${PreviousVersion}.reg "HKEY_LOCAL_MACHINE\Software\ESRI\ArcGIS License Manager" )
        fi
    fi

    # Remove previous Product
    echo "Uninstalling previous version..."
    if [ -d ${Esri_Install_Directory}/${shortname}${PreviousBaseVersion} ]
    then
        sh ${Esri_Install_Directory}/${shortname}${PreviousBaseVersion}/uninstall${product} -s >/dev/null 2>&1
    else
        sh ${Esri_Install_Directory}/${InstallName}/uninstall${product} -s >/dev/null 2>&1
    fi

    # rename previous product folder, if it exists
    if [ -d ${Esri_Install_Directory}/${shortname}${PreviousBaseVersion} ]
    then
        mv ${Esri_Install_Directory}/${shortname}${PreviousBaseVersion} ${Esri_Install_Directory}/${InstallName} >/dev/null 2>&1
    else
        mkdir -p ${Esri_Install_Directory}/${InstallName} >/dev/null 2>&1
    fi

    # convert reg entries
    if [ ` echo "${PreviousBaseVersion}<2020.1" | bc ` -gt 0 ]
    then
        mkdir -p $PreviousVersion_Backup_Dir/.Setup
        echo REGEDIT4 > $PreviousVersion_Backup_Dir/.Setup/LicenseManager${BaseVersion}.reg
        echo >> $PreviousVersion_Backup_Dir/.Setup/LicenseManager${BaseVersion}.reg
        grep HKEY_LOCAL_MACHINE $PreviousVersion_Backup_Dir/LicenseManager${PreviousVersion}.reg | sed "s/${PreviousBaseVersion} //g" >> $PreviousVersion_Backup_Dir/.Setup/LicenseManager${BaseVersion}.reg
        grep ACTIVATION_ENABLED $PreviousVersion_Backup_Dir/LicenseManager${PreviousVersion}.reg >> $PreviousVersion_Backup_Dir/.Setup/LicenseManager${BaseVersion}.reg
        grep BORROW_ENABLED $PreviousVersion_Backup_Dir/LicenseManager${PreviousVersion}.reg >> $PreviousVersion_Backup_Dir/.Setup/LicenseManager${BaseVersion}.reg
        grep MAX_BORROW_TIMEOUT $PreviousVersion_Backup_Dir/LicenseManager${PreviousVersion}.reg >> $PreviousVersion_Backup_Dir/.Setup/LicenseManager${BaseVersion}.reg
        grep TIMEOUTWARNING $PreviousVersion_Backup_Dir/LicenseManager${PreviousVersion}.reg >> $PreviousVersion_Backup_Dir/.Setup/LicenseManager${BaseVersion}.reg
        cat $PreviousVersion_Backup_Dir/FLEXlm.reg | sed "s/${PreviousBaseVersion}/${BaseVersion}/g" | sed "s/license${BaseVersion}/licensemanager/g" > $PreviousVersion_Backup_Dir/.Setup/FLEXlm.reg
        rm -f $PreviousVersion_Backup_Dir/*.reg
    fi

    # restore user files
    cp -pR ${PreviousVersion_Backup_Dir}/.Setup ${Esri_Install_Directory}/${InstallName}/ 2>/dev/null
    cp -pR ${PreviousVersion_Backup_Dir}/* ${Esri_Install_Directory}/${InstallName}/ 2>/dev/null

    # Install current Product
    Esri_UI_Mode=silent
    Esri_License_Agree=yes
    echo "Installing upgrade..."
    PreviousVersion_UPGRADE=true; export PreviousVersion_UPGRADE
fi

if [ "$Esri_UI_Mode" = "silent" ]
then
    append_to_install_dir
    check_valid_install_dir # This call should be after append_to_install_dir so we know that /arcgis/${InstallName} is at end of string

    if [ ${PreviousVersion_Detected} != "true" ]
    then
        echo "[$ProductName Installation Details]"
        echo "UI Mode..................$Esri_UI_Mode"
        echo "Agreed to Esri License...$Esri_License_Agree"
        echo "Installation Directory...$Esri_Install_Directory"
        echo
        echo "Starting installation of $ProductName..."
    fi
    sleep 2
else
    # validate the current DISPLAY
    if [ -z "$DISPLAY" ]
    then
        echo >&2
        echo "WARNING:  This shell's DISPLAY variable has not been set." >&2
        echo "This setup requires a valid display for GUI install. Please set the DISPLAY" >&2
	   	echo "variable to your local UNIX host, or execute this setup from a UNIX host" >&2
	   	echo "which has the DISPLAY variable set." >&2
        echo "${SILENT_INFO_TEXT}" >&2
        exit 1
    fi

    echo
    echo "Starting installation of $ProductName..."
fi

# Installer configuration
CD_ROOT="${ScriptPath}"; export CD_ROOT
if [ $platform = "Linux" ]
then
    InstallerLoc="${ScriptPath}/${product}/${platform}/Disk1/InstData/VM/install.bin"
else
    InstallerLoc="${ScriptPath}/${product}/${platform}/Disk1/InstData/NoVM/install.bin"
fi

if [ -f "$InstallerLoc" ]
then
{
    # Launch the setup
    if [ "$Esri_UI_Mode" = "silent" ]
    then
        if [ "$Esri_License_Agree" = "yes" ]
        then
            create_prop_file
            sh "$InstallerLoc" -i silent -f "$Esri_Silent_Property_File"
            Esri_IA_Exit_Code=$?
            if [ "$Esri_IA_Exit_Code" = "0" ]
            then
                echo "...$ProductName installation is complete."
                if [ ${platform} = "Linux" ]
                then
                    echo "You will need to run the license service installer (${Esri_Install_Directory}/licensingservice/install_fnp.sh) as the root user or via the sudo command, before using ${ProductName}."
                fi
            else
                error_header
                echo "...$ProductName installation is complete, but some errors or warnings occurred. [Exit Code: ${Esri_IA_Exit_Code}]" >&2
                echo "Please check this file for more information:" >&2
                echo "${Esri_Install_Directory}/.Setup/${product}_InstallLog.log" >&2
                exit 1
            fi
        else
            error_header
            echo "Before the installer can run silently, you must read and agree to the Master Agreement." >&2
            echo "Please visit http://www.esri.com/legal/licensing-translations to read the agreement." >&2
            echo "Please see the --help option for further information." >&2
            echo >&2
            echo "Exiting." >&2
            exit 1;
        fi
    else
        echo
        echo "Note: ${SILENT_INFO_TEXT}"
        sleep 2
        echo
        sh "$InstallerLoc" -i gui
    fi
} | grep -v "^Custom code execution " | grep -v "Gtk-WARNING"
else
    error_header
    echo "Setup files missing. Please make sure the file ${InstallerLoc} is present and run the Setup script again." >&2
    echo "Exiting." >&2
    exit 1
fi

if [ ${PreviousVersion_Detected} = "true" ]
then
    # restore user files
    cp -pR ${PreviousVersion_Backup_Dir}/.Setup ${Esri_Install_Directory}/ 2>/dev/null
    cp -pR ${PreviousVersion_Backup_Dir}/* ${Esri_Install_Directory}/ 2>/dev/null

    rm -rf ${PreviousVersion_Backup_Dir} >/dev/null 2>&1
fi
