#!/bin/sh
##
## Copyright (c) 2023, HCL Development Corporation.
## All Rights Reserved.
##
## Change:
##
##  #!/bin/sh
##
## to
##
##  #!/bin/sh -x
##
## for debug output
##
## This is 'install' from /iris/dev/tools/support/nui/installer/install11
##

# Set version
V=11
export V

CdPath() {

	# presumes CdPathRoot is set to the real absolute path of the 
	# kit (dist) root; the dir containing this file 
	# (not necessarily the root of the cd)

	# in:		$1: relative (to CdPathRoot) representative path of 
	#				target file 
	#			$2: (optional) if "noescape", a backslash char will not be used
	#				to escape a semicolon in the filename
	# returns:	absolute real path of target, or 
	#			${CdPathRoot}/${1} if not found

    targpath=$1

	# try as-is
	testpath=${CdPathRoot}/${targpath}
    if [ -r $testpath ]; then
        $echo $testpath
        return
    fi
	
	# try upper-case
    testpath=${CdPathRoot}/`$echo $targpath | $tr [a-z] [A-Z]`
    if [ -r $testpath ]; then
        $echo $testpath
        return
    fi

	# try lower-case
    testpath=${CdPathRoot}/`$echo $targpath | $tr [A-Z] [a-z]`
    if [ -r $testpath ]; then
        $echo $testpath
        return
    fi

	# try upper-case with version ";1" appended
    if [ -r ${testpath}\;1 ]; then
		if [ "$2" = "noescape" ]; then
			$echo "${testpath};1"
		else 
	        $echo ${testpath}\\\;1
		fi
        return
    fi

	# didn't find it
    $echo ${CdPathRoot}/${targpath}
}


#
# BEGIN script
#

PATH=/usr/bin:/bin:/usr/sbin:${PATH}
export PATH

#Language Pack "Replace" language option and Japanese language always
#LP_REPLACE=1
#export LP_REPLACE
#Japanese language only
#LP_JP=1
#export LP_JP

tr=tr
basename=basename
sed=sed
pwd=pwd
uname=uname
dirname=dirname
echo=echo
umask=umask


# determine NUI_ARCH
#
if [ `$uname` = AIX ]; then
	NUI_ARCH=aix
elif [ `$uname` = Linux ]; then
    	NUI_ARCH=linux
        export LINUX_PACK16=1
elif [ `$uname` = OS400 ]; then
	# This is a virtuall install done at kitting time for os400.  Customer never runs
	# this script, so set desired values for kitting.
	NUI_ARCH=os400
	NUI_NOTROOT=1 # Don't have to be root to run
	NUI_DEV=1     # Dump output to stdout instead of error log
	export NUI_NOOPTLOTUS=1  # Don't try to update anything in /opt/hcl/domino (done in NIC.pm)
	export NUI_DBG_BE=1     # debug mode
fi
export NUI_ARCH


# change sys cmd defs as necessary per-platform
#
if [ "$NUI_ARCH" = linux -o "$NUI_ARCH" = linux64 ]  ; then
	echo="echo -e"
fi


userdir=`$pwd`

cd `$dirname $0`
NUI_DISTROOT=`$pwd`
export NUI_DISTROOT
CdPathRoot=$NUI_DISTROOT

# Uncomment the following lines if you know that you want to install over the existing hotfixes
# If you enable checksum checking by commenting out these lines then cannot install NP fp because the
# checksum data is always from prod objects at this time 
NUI_NOCHECKSUM=1
export NUI_NOCHECKSUM

if [ -z "$NUI_CFGFILE" ]; then 
NUI_CFGFILE=/opt/hcl/domino/.install.dat
export NUI_CFGFILE
fi

thisfile=`$basename $0`
thisfile=`$echo $thisfile | $sed -e 's/;1$//'`

if [ "$1" = "extract"  -o  "$1" = "contents" ]; then 	
	torun="tools/lib/extract_from_tar.pl"
else
	torun="tools/lib/${thisfile}.pl"
fi

# Read in our 'txt' strings
eval ". `CdPath tools/nls/${thisfile}.sh`"

# "Install Program" message
if [ "$1" != "extract" -a  "$1" != "contents" ]; then 
	if [ -r "hotfix_license.txt" ]; then
		if [ -z "$NUI_NOTESDIR" ]; then

			defaultInstallDir="/opt/hcl/domino"

			if [ -r "$defaultInstallDir/.install.dat" ]; then
				NUI_CFGFILE="$defaultInstallDir/.install.dat"
				export NUI_CFGFILE
            else
				$echo $txt6
				exit 1
			fi
		elif [ -r "$NUI_NOTESDIR/.install.dat" ]; then
			NUI_CFGFILE=$NUI_NOTESDIR/.install.dat
			export NUI_CFGFILE
        else
			$echo $NUI_NOTESDIR $txt7
			echo $txt6
			exit 1
		fi

		kitArchType=`grep kitArch $NUI_CFGFILE |   sed -e 's/.*kitArch = //'`
	fi

	# 10/6/20 - completely remove remnants of 32/64 bit check since always 64 bit now - kenbo

	# "Install Program" message
	$echo $txt3
	$echo $txt31
	$echo $txt32

	if [ -z "$NUI_ARCH" ]; then
		$echo "${txt1}`$uname`${txt2}" # Unknown platform
		exit 1
	fi

	# Check root is running this install script
	if [ -z "$NUI_NOTROOT" -a `id | sed -e 's/^[^(]*(//' -e 's/).*$//'` != "root" ]; then
		$echo "You must be root to run the install program.\n"
		exit 1;
	fi
fi

umaskvalue=`$umask`;

if [ $umaskvalue != 0022 ]; then
	cmd="umask 22"
	eval $cmd	
fi

while [ "$1" = "-perl" ]; do
	shift
	perlswitch=$1
	shift
done

PERL5LIB="`CdPath tools/lib`:`CdPath tools/lib/perl5`:`CdPath tools/nls`"
export PERL5LIB

PERL=`which perl| grep perl`
if [ $? -ne 0 ]; then
	PERL=`which Perl | grep Perl`
	if [ $? -ne 0 ]; then
		$echo $txt5 # Can't find Perl
		exit 1
	fi
fi

export PERL

# ensure current dir is same as users for relative path args
cd $userdir

# do it
#
if [ -z "$NUI_DEV" ]; then
	StdErrRedirect="2>./nuish.err"
fi


cmd="$PERL $perlswitch  `CdPath $torun` $* $StdErrRedirect"
eval $cmd

ret_code=$?

if [ "$NUI_AUTO" ]; then
	echo $ret_code > ./.install.txt
fi

# For automation, let them know how things went
exit $ret_code
