#!/bin/sh 
# CA License install script for Linux - wrapper around ca-lic rpm.
#
# Usage: 
# install [installpath]
#
# Where: 
# [installpath] is optional, and will be ignored if CASHCOMP is already defined.
#
# Functionality:
# The ca-lic package is relocatable based on the value of CASHCOMP.
# The value of CASHCOMP is reliably obtained via the liccaprofile common script.
# If CASHCOMP is defined the package must continue to be installed to that path.
# If CASHCOMP is not defined, the user may choose the path to install to or use the default.
# 
# It is required that certain arguments be passed to rpm when installing this package.
# It is required that this package never be uninstalled.
#

##### Taken from caprofile to reliably obtain the CASHCOMP value ####
Chk_Dir_Perms()
{

        [ "$Query" = "1" ] && return 0

        qvar="$1"
        eval q_var_val="$"`$LIC_ECHO $qvar`""

        if [ ! -d "${q_var_val}" ];then
                mkdir -p "${q_var_val}" 2>/dev/null
                if [ "$?" != "0" ];then
                        #cannot create the directory
                        return 10
                fi

                create_dir="1"
        fi

        touch "${q_var_val}/.tmp" 2>/dev/null
        if [ "$?" != "0" ];then
                #cannot create files in the directory
                return 11
        else

                rm -f "${q_var_val}/.tmp" 2>/dev/null

                # try to remove what we created
                [ "$create_dir" = "1" ] && rmdir -p "${q_var_val}" 2>/dev/null


                return 0
        fi
}
Chk_Env_in_Profile()
{
qvariable="$1"
qvar_value=""

 for file in /etc/profile /etc/profile.CA
 do
	if [ -f "$file" ];then
		vals_qvar=`grep "${qvariable}=" "$file" |grep -v "[a-zA-Z0-9_-]${qvariable}=" |\
			 grep -v "\#[ \t]*${qvariable}" | awk -F= '{print $2}'|awk -F\; '{print $1}'`
		
		for qvar_val in $vals_qvar
		do
			qvar_value="$qvar_val"	
		done
	fi
 done

 if [ "$qvar_value" = "" ];then
	return 12
 else
	eval "$qvariable"="$qvar_value"
	export "$qvariable"

	return 0

 fi
}

Chk_RegularVar()
{
	qvar="$1"

	Chk_Env_in_Profile "$qvar"
	rc1=$?
	if [ "$rc1" != "0" ];then
		return $rc1
	fi

	case $qvar in
	"PATH" | "LD_LIBRARY_PATH" | "LIBPATH" | "SHLIB_PATH" | "MANPATH" ) ;;
	* )
		Chk_Dir_Perms "$qvar"
		rc=$?
		if [ "$rc" != "0" ];then
			return "$rc"
		fi;;
	esac

	eval q_var_val="$"`$LIC_ECHO $qvar`""

	if [ "$q_var_val" != "" ];then
		$LIC_ECHO "$q_var_val"
		return 0
	else
		return 9
	fi
}

QueryVariable()
{
        qvar="$1"

        case "$qvar" in
        "CASHCOMP" )
                        Chk_Env_in_Profile CASHCOMP
                        CASH_rc="$?"

                        if [ "$CASH_rc" != "" ];then
                                $LIC_ECHO "$CASHCOMP"
                        fi

                        return "$CASH_rc"
                        ;;
        *)              Chk_RegularVar "$qvar"
                        return $?;;
        esac
return 0
}
#### End - Taken from caprofile to obtain CASHCOMP value ####

if [ "$CA_LIC_DEBUG" = "1" ];then
   set -xv
fi

version="01.0063-3"
rpmname="ca-lic-$version"
arch="i386"
required_args="-U --replacepkgs --replacefiles "
LIC_ECHO="echo -e"
export LIC_ECHO

ARCH=`uname -m 2>/dev/null`
case "$ARCH" in
  i[2-8]86) arch="i386"
            ;;    
  ia64)     arch="ia64"
            version="01.0063-0000"	
            rpmname="ca-lic-$version"	
            ;;
  s390)     arch="s390"
            ;;
  s390x)    arch="s390"
            ;;
esac      

if [ "$ARCH" = "i?86" ];then
  required_args="$required_args --ignorearch"
fi
 
# rpm 4.0.2 workaround - never use prefix
ver=`rpm --version | tail -c6`
if [ "$ver" = "4.0.2" ];then
   `rpm $required_args ./$rpmname.$arch.rpm`
   exit $?
fi

# get the correct prefix value to pass to the rpm
CASHCOMP=`QueryVariable CASHCOMP`
if [ "$CASHCOMP" != "" ];then
    # If CASHCOMP is defined it MUST be passed as the prefix
    # This normally expands to:
    # "rpm -U --replacefiles --replacepkgs --prefix /opt/CA/SharedComponents ca-lic-xxxxxxxxx.rpm
    `rpm $required_args --prefix "$CASHCOMP" ./$rpmname.$arch.rpm`
else 
    # if CASHCOMP is not yet defined 
    CALIB="`QueryVariable CALIB`"
    if [ "$CALIB" != "" ];then
        # CALIB is set and CASHCOMP is not - backward compatable case
        CASHCOMP="`dirname $CALIB`"
        `rpm $required_args --prefix "$CASHCOMP" ./$rpmname.$arch.rpm`
    else
       # Nothing is defined - new system
       if [ "$1" = "" ];then
          # use the default as the prefix - /opt/CA/SharedComponents
          `rpm $required_args ./$rpmname.$arch.rpm`
       else
          # use a configurable directory
          `rpm $required_args --prefix "$1" ./$rpmname.$arch.rpm`
       fi  
    fi
fi  

rc=$?

if [ $rc -eq 2 ];then
  exit 0
else
  exit $rc
fi

