#! /sbin/sh
###############
# Product: UserLicense
# Fileset: *-USER
# postinstall
# @(#) $Revision: 1.4.98.2 $
################
#
# (c) Copyright Hewlett-Packard Company, 1993
#
########################################################################
#                                                                      #
# General requirements:                                                #
#  (1)  Alter libhp-ux.a to support the proper number of users.        #
#  (2)  Clean up afterwards.					       #
#       - Remove things that might allow illegitimate license updating.#
#       - Remove all other license filesets.                           #
#                                                                      #
########################################################################


# Variable declarations.

UTILS=/usr/lbin/sw/control_utils
if [[ ! -f $UTILS ]]
then
    print "ERROR:   Cannot find the sh functions library $UTILS."
    exit 1
fi
. $UTILS

exitval=$SUCCESS                # Anticipate success

BASE_LIB=/usr/conf/lib/libhp-ux.a
BASE_OBJ=/usr/lbin/sw/p
BASE_UPD=/usr/lbin/sw/pinstall
KERNEL_LIB=${SW_ROOT_DIRECTORY%/}${BASE_LIB}  # Library to update
LICENSE_OBJ=${SW_ROOT_DIRECTORY%/}${BASE_OBJ} 
EXEC_UPD=${SW_ROOT_DIRECTORY%/}${BASE_UPD} 


if [[ $SW_ROOT_DIRECTORY > "/" ]] 
then
  remote_opt="-r "
else
  remote_opt=""
fi

KERN_DEBUG=""		# Debugging Flag
PS4='    + ${0##*/}:$LINENO -> '

#########################################
# 					#
# Utility funtions.			#
# 					#
#########################################

#
# Clear - Remove all other license level products 
#

function Clear {
   [[ -n "$KERN_DEBUG" ]] && set -x

   #
   # Clean up other license products 
   #

   	# get the names of the other products.
		# 10.0 MR names
		# 10.0 pre-gsT names
		#  9.x names
       OtherLevels=`echo  " \
			UserLicense.02-USER \
   			UserLicense.08-USER \
   			UserLicense.16-USER \
   			UserLicense.32-USER \
   			UserLicense.64-USER \
   			UserLicense.UNL-USER \
			02UserLicense \
			08UserLicense \
			16UserLicense \
			32UserLicense \
			64UserLicense \
			UnlUserLicense \
			HPUX9.02-USER \
			HPUX9.08-USER \
			HPUX9.32-USER \
			HPUX9.64-USER \
			HPUX9.UNL-USER \
" | \
   		 sed -e "s/${PRODUCT}.${FILESET}//" ` 


       # Get list of old license filesets
       # 	Massage swlist output to be suitable for swremove input
       # 		Remove # lines
       # 		Remove blank lines
       # 		Replace tabs before revision fields with ,r=

       Obsolete="$(swlist -l fileset -a revision $remote_opt \
		$OtherLevels @ $SW_ROOT_DIRECTORY 2>/dev/null | \
		sed -e '/^#/ D' \
			     -e '/^$/ D' \
			     -e 's/		*/,r=/')"

       if [[ -n "$Obsolete" ]]
       then
          swremove -x verbose=0 $remote_opt $Obsolete @ $SW_ROOT_DIRECTORY
	  if [[ $? -eq $SUCCESS ]]
	  then
   	    print "NOTE:    Older license level fileset(s) removed."
	  else
   	     print "WARNING: Could not remove older license fileset(s)."
   	     [[ $exitval -ne $FAILURE ]] && exitval=$WARNING
	  fi	# swremove return -eq $SUCCESS
   	  print "         Filesets: $Obsolete"
       fi	# [[ -n "$Obsolete" ]]

   return 0
}

########################################################################
#                                                                      #
# Main line code.                                                      #
#                                                                      #
########################################################################

[[ -n "$KERN_DEBUG" ]] && set -x

########################################################################
# Error Checking
########################################################################
# A missing file usually means we are continuing with a previous
# installation failure.  We'll warn the user that that the license
# level was not installed, but won't force them to abort.  They
# can continue with the default license level and install the license
# level later.  They can't "fix" the problem, since the missing files
# have to be re-loaded from the media.  

for file in $LICENSE_OBJ $KERNEL_LIB $EXEC_UPD
do
  if [[ ! -r $file ]]
  then
    print "WARNING: Could not read ${file}."
    exitval=$WARNING
  fi
done


########################################################################
# Load in new license file
########################################################################

if [[ $exitval -ne $WARNING ]]
then
  $EXEC_UPD $LICENSE_OBJ $KERNEL_LIB || exitval=$FAILURE

	# Make sure install script didn't change permissions
  chmog 444 bin bin ${KERNEL_LIB}  

        # Update kernel library file attributes for swverify purposes
  swmodify $remote_opt -x loglevel=0 \
		-x files="${BASE_LIB}" \
		OS-Core.CORE-KRN @ $SW_ROOT_DIRECTORY

  # Remove these even on failure for security purposes.
  IPD_delfile $BASE_UPD
  IPD_delfile $BASE_OBJ
  rm -f $EXEC_UPD $LICENSE_OBJ

  if [[ $exitval -eq $FAILURE ]]
  then
   print "ERROR:   Install failed, possibly due to insufficient temporary disk space." 
  fi

else
  print "         You must re-install the fileset $PRODUCT.$FILESET to modify"
  print "         your license level."
  print "         The system license level is unchanged."
fi

########################################################################
#                                                                      #
# Fin.                                                                 #
#                                                                      #
########################################################################

if [[ $exitval -ne $SUCCESS ]]
then
   print "         ${PRODUCT}.${FILESET} license level was not installed." 
else
   Clear 
fi

exit $exitval
