#!/bin/bash
# #################################################################################
# Exceed TurboX Connection Node
# © Rocket Software, Inc. or its affiliates. All Rights Reserved.
# ROCKET SOFTWARE, INC. CONFIDENTIAL
# #################################################################################
# installetxCUPS
# ##################################################################################

test_ECHO()
{
	${ECHO} -e "test\c" | grep "\-e" > /dev/null
	if [ $? -gt 0 ] ; then
		# -e is supported so add it
		ECHO="${ECHO} -e"
	fi
}

find_good_ECHO()
{
	ECHO=`which echo`
	test_ECHO
	export ECHO
}

silentEcho()
{
	if [ ! "${bSilent}" = "1" ] ; then
		${ECHO} "$*"
	fi
}

getLinuxPlatforms()
{
	isLinux="1"
	
	if [ -x "/etc/rc.d/init.d" ]; then
		isRedHat="1"
	elif [ -x "/etc/rc0.d/" ]; then
		isDebianUbuntu="1"
	else
		isSUSE="1"
	fi
}

getThisPlatform()
{
	osAix=aix
	osLinux=linux
	osSolaris=solaris
	osHPUX=hpux
	
	archPpc=ppc
	archi586=i586
	archx86_64=x86_64
	archSparc=sparc
	archParisc=parisc
	
	unknown=unknown
	
	thisOS=${unknown}
	thisArch=${unknown}
	
	thisUname=`uname`
	
	fgrepOptions="-c -i"
	optionL="-L"
	
	hasi86=`uname -a | grep ${fgrepOptions} i386`
	
	if [ ${hasi86} = "0" ] ; then
		hasi86=`uname -a | grep ${fgrepOptions} i686`
	fi
	
	isx86_64=`uname -a | grep ${fgrepOptions} x86_64`
	
	if [ ${hasi86} = "0" ] ; then
		if [ ! ${isx86_64} = "0" ] ; then
			thisArch=${archx86_64}
		fi
	else
		thisArch=${archi586}
	fi
	
	case ${thisUname} in
		AIX )
			thisOS=${osAix}
			thisArch=${archPpc}
			;;
		Linux )
			thisOS=${osLinux}
			getLinuxPlatforms
			;;
		SunOS )
			thisOS=${osSolaris}
			if [ ${hasi86}=0 ] ; then
			thisArch=${archSparc}
				optionL="-h"
			fi
			;;
		HP-UX )
			thisOS=${osHPUX}
			thisArch=${archParisc}
			;;
		* )
			;;
	esac
}

getThisSysLog()
{
	thisLogName=${thisScript}.log
	sysLogDir="/var/log"
	
	if [ -d "${sysLogDir}" ] ; then
		thisSysLog="${sysLogDir}/${thisLogName}"
	fi
}

writeToThisSysLog()
{
	if [ ! -z "${thisSysLog}" ] ; then 
		${ECHO} "$*" >> "${thisSysLog}"
	fi
}

writeToThisSysLogAndEcho()
{
	writeToThisSysLog $*
	silentEcho $*
}

getDirs()
{
	# resolve links - $0 may be a softlink
	thisPRG="$0"
	
	while [ -h "$thisPRG" ] ; do
		ls_ld=`ls -ld "$thisPRG"`
		this_link=`expr "${ls_ld}" : '.*-> \(.*\)$'`
		
		if expr "${this_link}" : '/.*' > /dev/null; then
			thisPRG="${this_link}"
		else
			thisPRG=`dirname "${thisPRG}"`/"${this_link}"
		fi
	done
	
	CDir=`pwd`
	
	thisPrgDirName="`dirname ${thisPRG}`"
	
	cd "${thisPrgDirName}" > /dev/null 2>&1
	
	elprDir=`pwd`
	
	cd ${CDir} > /dev/null 2>&1 
}

rootOnlyPlease()
{
	${ECHO} "The ${thisScript} script requires root privileges."
	exit 1
}

getCUPSBackendDir()
{
	USRLIB="/usr/lib"
	CUPSBACKEND="cups/backend"
	
	LIBCUPSBACKEND="${USRLIB}/${CUPSBACKEND}"
	LIB64CUPSBACKEND="${USRLIB}64/${CUPSBACKEND}"
	
	if [ -d "${LIBCUPSBACKEND}" ] ; then
		cupsBackendDir="${LIBCUPSBACKEND}"
	elif [ -d "${LIB64CUPSBACKEND}" ] ; then
		cupsBackendDir="${LIB64CUPSBACKEND}"
	else
		${ECHO} "${LIBCUPSBACKEND} and ${LIB64CUPSBACKEND} do not exist!"
		exit 1
	fi
}

init_vars()
{
	thisScript=`basename ${0}`
	
	getCUPSBackendDir
	
	THIS_ELPR="${elprDir}/${ELPR_NAME}"
	USRBINELPR="/usr/bin/${ELPR_NAME}"
	
	usr_bin_elpr_cmd_line="${USRBINELPR} ${ELPR_OPTIONS}"
	
	etxpsDir="${elprDir}/sys"
	ETXPS_NAME="etxps"
	THIS_ETXPS="${etxpsDir}/${ETXPS_NAME}"
	BACKEND_ETXPS="${cupsBackendDir}/${ETXPS_NAME}"
	
	bashEtcProfNames="bashrc bash.bashrc"
	shEtcProfNames="profile"
	csh_tcshEtcProfNames="csh.cshrc"
	kshEtcProfNames="kshrc ksh.kshrc"
	zshEtcProfNames="zshrc"
	
	allSysProfileNames="${bashEtcProfNames} ${shEtcProfNames} ${csh_tcshEtcProfNames} ${kshEtcProfNames} ${zshEtcProfNames}"
	modificationByThisScript="modification by ${thisScript} - Please do NOT edit"
	
	startingSignPost="Starting ${modificationByThisScript}"
	endingSignPost="Ending ${modificationByThisScript}" 
	
	if [ "`whoami`" = "root" ] ; then
		iamroot="1"
	else
		rootOnlyPlease
	fi
}

getThisProfileStatus()
{
	if [ ! -e "${thisProfile}" ] ; then
		${ECHO} "${thisProfile} not found."
		return
	fi
	
	startingSignPostCount="`grep -c "${startingSignPost}" ${thisProfile}`" 
	endingSignPostCount="`grep -c "${endingSignPost}" ${thisProfile}`" 
	
	thisProfileWasModified=""
	
	if [ "${startingSignPostCount}" -gt "0" -a "${endingSignPostCount}" -gt "0" ] ; then
		thisProfileWasModified="1"
	fi
}

noSupportsForNonLinux()
{
	${ECHO} "The ${ELPR_CUPS_SUPPORTS} is available for Linux only."
	
	exit 1
}

set_elpr_vars()
{
	ELPR_NAME="elpr"
	ELPR_OPTIONS="-q -a 1"  
	ELPR_CUPS_SUPPORTS="${ELPR_NAME} CUPS support"
}

init()
{
	find_good_ECHO
	set_elpr_vars
	
	getThisPlatform
	
	if [ ! "${isLinux}" = "1" ] ; then   
		noSupportsForNonLinux
	fi
	
	getDirs
	init_vars
	
	getThisSysLog
}

followThisProfileLink()
{
	while [ -h "${thisProfile}" ]; do
		ls_ld=`ls -ld "${thisProfile}"`    
		
		this_link=`expr "${ls_ld}" : '.*-> \(.*\)$'`
		
		if expr "${this_link}" : '/.*' > /dev/null; then
			thisProfile="${this_link}"
		else
			thisProfile=`dirname "${thisProfile}"`/"${this_link}"
		fi
	done
}

thisProfileHasThisSource()
{
	etcDir="/etc"
	etcSource="${etcDir}/${1}"  
	
	if [ -e "${etcSource}" ] ; then
		thisSrcPat=`echo ${1} | sed -e "s|\.|\\\.|"`
		thisGrepPat="^ *source * \\${etcDir}\/${thisSrcPat}"
		thisCount=`grep -c "${thisGrepPat}" "${thisProfile}"`
		
		if [ "${thisCount}" -gt "0" ] ; then
			return 0
		fi
	fi
	
	return 1
}

thisProfileHasSource()
{
	theseSources="bashrc bash.bashrc csh.cshrc kshrc ksh.kshrc profile"
	
	for thisSource in ${theseSources} ; do
		if thisProfileHasThisSource "${thisSource}" ; then
			return 0
		fi
	done
	
	return 1
}

handleSystemProfiles()
{
	thisOp=${1}
	
	for thisProfileName in ${allSysProfileNames} ; do
		thisProfile="/etc/${thisProfileName}"
		
		if [ -e "${thisProfile}" ] ; then
			if [ -L "${thisProfile}" ] ; then
				followThisProfileLink
			fi
			if thisProfileHasSource ; then
				continue
			fi
			getThisProfileStatus
			${thisOp}
		fi
	done
}

thisProfileAlreadyHandledELPR()
{
	countELPR="`grep -c \"\/usr\/bin\/${ELPR_NAME}\" ${thisProfile}`" 
	
	if [ "${countELPR}" -gt "0" ] ; then
		return 0
	fi
	
	return 1
}

modifyThisProfile()
{
	if [ ! -e "${thisProfile}" ] ; then
		${ECHO} "${thisProfile} not found."
		return
	fi
	
	if thisProfileAlreadyHandledELPR ; then
		return
	fi
	
	if [ "${thisProfileWasModified}" = "1" ] ; then
		thisMsg="${thisProfile} had already been modified!" 
		
		writeToThisSysLogAndEcho "${thisMsg}"
		
		return
	fi
	
	cp ${thisProfile} ${thisProfile}.bak
	
	cat >> ${thisProfile} << _MODIFY_THIS_PROFILE
# ${startingSignPost}
#

${usr_bin_elpr_cmd_line}

#
# ${endingSignPost}
_MODIFY_THIS_PROFILE

	writeToThisSysLogAndEcho "`date`: ${thisProfile} was modified:"
	writeToThisSysLogAndEcho "${usr_bin_elpr_cmd_line}" 
}

lnThisToThat()
{
	if [ -L "${2}" ] ; then
		rm -f "${2}"
	fi
	
	ln_cmd_line="ln -s \"${1}\" \"${2}\""
	
	ln -s "${1}" "${2}"
	
	writeToThisSysLogAndEcho "`date`: ${ln_cmd_line}"
}

do_root_install()
{
	lnThisToThat "${THIS_ELPR}" "${USRBINELPR}"
	lnThisToThat "${THIS_ETXPS}" "${BACKEND_ETXPS}"
}

usage()
{
	nspaces="    "
	thisScriptName=${thisScript}
	
	cat << _END_USAGE_

${thisScriptName} - Exceed TurboX Connection Node

Usage: ${thisScriptName} -i|I|u|U [-q|-Q|-s|-S]
  
${nspaces}-i|-I       : install ${ELPR_CUPS_SUPPORTS}
${nspaces}-u|-U       : uninstall ${ELPR_CUPS_SUPPORTS}
${nspaces}-q|-Q|-s|-S : do not print any messages

_END_USAGE_

	exit 1
}

do_install()
{
	writeToThisSysLog ""
	writeToThisSysLog "`date`: ${thisScript} starts."
	do_root_install
	handleSystemProfiles "modifyThisProfile"
	writeToThisSysLog "`date`: ${thisScript} ends."
	writeToThisSysLog ""
}

restoreThisProfile()
{
	if [ "${thisProfileWasModified}" = "1" ] ; then
		writeToThisSysLogAndEcho "`date`: Restoring ${thisProfile}"
		tmpProfile="${thisProfile}.tmp"
		mv "${thisProfile}" "${tmpProfile}"
		sed "/${startingSignPost}/, /${endingSignPost}/ d" < "${tmpProfile}" > "${thisProfile}"
		rm -f "${tmpProfile}"
	fi
}

removeSharedMemory()
{
	if [ ! -x "${USRBINELPR}" ] ; then
		return 
	fi
	
	rmSharedMem="${USRBINELPR} -q -a 0"
	
	writeToThisSysLogAndEcho "`date`: ${rmSharedMem}"  
	
	${rmSharedMem}
}

do_uninstall()
{
	removeSharedMemory
	handleSystemProfiles "restoreThisProfile"
	removingSoftLink="Removing soft link"
	
	if [ -L "${BACKEND_ETXPS}" ] ; then
		writeToThisSysLogAndEcho "`date`: ${removingSoftLink} ${BACKEND_ETXPS}"
		rm -f "${BACKEND_ETXPS}"
	fi
	
	if [ -L "${USRBINELPR}" ] ; then
		writeToThisSysLogAndEcho "`date`: ${removingSoftLink} ${USRBINELPR}"
		rm -f "${USRBINELPR}"
	fi
}

getParams()
{
	if [ -z "${1}" ] ; then
	usage
	fi
	
	while [ ! -z "${1}" ] 
	do
		
		case "${1}" in
			-q | -Q | -s | -S )
				bSilent="1"
				;;
			-i | -I )
				install_it="1"
				;;
			-u | -U )
				uninstall_it="1"
				;;
			* )
				usage
				;;
		esac
		
		shift
		done
}

do_it()
{
	init
	getParams $*
	
	if [ "${install_it}" = "1" ] ; then
		do_install $*
	elif [ "${uninstall_it}" = "1" ] ; then
		do_uninstall $*
	fi
}

do_it $*
