#!/bin/sh
# @(#)postinstall_template	1.17 07 Feb 1995 Copyright 1993 SMI
# Copyright (c) 1993 Sun Microsystems, Inc.  All Rights Reserved.
# Sun considers its source code as an unpublished, proprietary trade
# secret, and it is available only under strict license provisions.
# This copyright notice is placed here only to protect Sun in the event
# the source is deemed a published work.
#
# RESTRICTED RIGHTS LEGEND: Use, duplication, or disclosure by the
# Government is subject to restrictions as set forth in subparagraph
# (c)(1)(ii) of the Rights in Technical Data and Computer Software
# clause at DFARS 52.227-7013 and in similar clauses in the FAR and
# NASA FAR Supplement.
#
# Post-install template for unbundled products using FLEXlm
# Copies all licenses over from the standard directory - $licdir -

###########################################
# The following lines should be modified for use by individual products
###########################################
# Identifies the basename of the license file and license
# location file, before the .loc or.lic are attached to it
# e.g., if license_file=featfile
# the license file will then be called featfile.lic
#
# For Software Developer Products, the default is "sunpro"
license_file=sunpro

# Identifies the path to the license once it is installed
# in the product, excluding the basedir and the license file name
# e.g., if dirloc=SUNWspro/lib, the license file would be placed in
# $BASEDIR/SUNWspro/lib
#
# For Software Developer Products, the default is "SUNWspro/license_dir"
dirloc=SUNWspro/license_dir

# Identifies the path to the router file once it is installed
# in the product  - ALL INCLUSIVE -
# INCLUDING the basedir and the router file name.
# There can be more than 1, numbered consecutively - routerloc.1, routerloc.2,
# etc.
#routerloc1=FILL_THIS_IN
#

####################################################
# Subsequent lines should not be modified by individual products
#####################################################
licdir="/etc/opt/licenses"
licfile=${license_file}.lic
locfile=${license_file}.loc

PLATFORM=`uname`

if [ "${PLATFORM}" = "SunOS" ]; then
	if [ -f /vmunix ]
	then
	# 	Solaris 1.x systen
		thishost=`hostname`
		opt="-ax"
		LICPS="ps"
	else 
		thishost=`uname -n`
		LICPS="/usr/bin/ps"
		opt="-ef"
	fi

	CHGRP_CMD="chgrp -f"

elif [ "${PLATFORM}" = "HP-UX" ]; then
	thishost=`uname -n`
	LICPS="/bin/ps"
	opt="-ef"
	CHGRP_CMD="chgrp"
else
	echo "The ${PLATFORM} platform is not supported by the postinstall"
	echo "script for this package."
	exit 1
fi

sunpro_router_rel_prefix="/net"
#sunpro_router_rel_path="${sunpro_router_rel_prefix}/${thishost}"
${LICPS} ${opt} | grep automount > /dev/null 2>&1

if [ $? -ne 0 ]
then
	sunpro_router_rel_path=""
else
	sunpro_router_rel_path="${sunpro_router_rel_prefix}/${thishost}"
fi



node_suffix="node"
		
#
# Adds path to router file
fix_router_files ()
{
	licfilename=$1
	i=1
	while [ $i -gt 0 ]
	do
		routerfile=routerloc$i
		eval rfile=\$$routerfile
		if [ "$rfile" = "" ]
		then
			break
		fi
		if [ -f $rfile ]
		then
			awk 'BEGIN	{
						found_path = 0
					}
				{	if ($0 ~ /^#/)
						printf("%s\n", $0)
				  	else
					{
						if ($0 == "")
							printf("%s\n", $0)
				  		else
						{
							if (index($0, licfilename))
							{
								printf("%s\n", $0)
							}
							else
							{
								if(found_path == 0)
									printf("%s:%s\n", licfilename, $0)
								else
									printf("%s\n", $0)
							}
							found_path = 1
						}
					}
				}
END		{
					if (found_path == 0)
						printf("%s\n", licfilename)
		} ' licfilename="$licfilename" ${rfile} > /tmp/routerout

			cp $rfile $rfile.BAK
			cp /tmp/routerout  $rfile 
		else
			dirtouse=`dirname $rfile`
			if [ "$dirtouse" != "" ]
			then
				mkdir -p "$dirtouse" > /dev/null 2>&1
			fi
			echo $licfilename >> $rfile
		fi
		i=`expr $i + 1`
	done
}

copylicfiles()
{

	file_dir=${1}
	file_prefix=${2}
	for infile in `ls ${file_dir}/${file_prefix}*`
	do

# Handle nodelocked license files specially - place them in a ",node" file
		basefile=`basename ${infile}`
		if [ $basefile = ${licfile},${node_suffix} ]
		then
			outputfile=$BASEDIR/$dirloc/$licfile,${node_suffix}
			cat ${file_dir}/${basefile} >> $outputfile
			if [ $? -ne 0 ]
			then
				echo "Cannot execute command -  cat $infile >> $outputfile - Exiting"
				exit 1
			fi
		else
			i=1
			notdone=0
			while [ $notdone -eq 0 ]
			do
				outputfile=$BASEDIR/$dirloc/$licfile,$i
				if [ ! -f $outputfile ]
				then
					notdone=1
				else
					i=`expr $i + 1`
				fi
			done
		
# Get the basename, just in case we don't know the formate of this name
			basefile=`basename ${infile}`
			cp ${file_dir}/${basefile} $outputfile
			if [ $? -ne 0 ]
			then
				echo "Cannot execute command -  cp ${file_dir}/${basefile} $outputfile - Exiting"
				exit 1
			fi
		fi
		fix_router_files  "${sunpro_router_rel_path}${outputfile}"
		if [ ${file_dir} = ${licdir} ]
		then
			rm ${licdir}/${basefile}
		fi
	done
	echo Licenses copied from ${file_dir} to $BASEDIR/$dirloc
	
}
#
# The main body
#
if [ ! -d ${BASEDIR}/${dirloc} ]
then
	echo Cannot Access license location directory ${BASEDIR}/${dirloc}  - Exiting
	exit 1
fi

mkdir -p /etc/opt/licenses > /dev/null 2>&1
chmod 775 /etc/opt /etc/opt/licenses
${CHGRP_CMD} sys /etc/opt /etc/opt/licenses
#
#
# Find licenses placed in other basedirs
if [ -f ${licdir}/${locfile} ]
then
	grep ${BASEDIR}/${dirloc} ${licdir}/${locfile} > /dev/null 2>&1
	if [ $? -ne 0 ]
	then
	# If We're already listed in  the loc file. No need to look for
	# licenses from other location directories
	#
		
		while read tempdir
		do
			ls ${tempdir}/${licfile}* 2>/dev/null | grep ${licfile} > /dev/null 2>&1
			if [ $? -eq 0 ]
			then
			#
			# Only need to copy from one directory. All others should be same
			#
				copylicfiles ${tempdir} ${licfile}
				break
			fi
		done < ${licdir}/${locfile}
		echo $BASEDIR/${dirloc} >> ${licdir}/${locfile}
	fi
else
	echo $BASEDIR/${dirloc} >> ${licdir}/${locfile}
fi
		
#
# Find new names for existing new license files in licdir
#
ls ${licdir}/${licfile}* 2>/dev/null | grep ${licfile} > /dev/null 2>&1
if [ $? -eq 0 ]
then 
	copylicfiles ${licdir} ${licfile}
fi
#
# Make sure we get router files fixed in case where licenses
# were installed before router files were set up
#
ls ${BASEDIR}/${dirloc}/${licfile}* 2>/dev/null | grep ${licfile} > /dev/null 2>&1
if [ $? -eq 0 ]
then
	for usefile in `ls ${BASEDIR}/${dirloc}/${licfile}*`
	do
		tfile=`basename ${usefile}`
		fix_router_files  "${sunpro_router_rel_path}${BASEDIR}/${dirloc}/${tfile}"
	done
fi

echo Make sure to run the license installation tool or the license \
configuration script on this machine to install additional licenses.

i=1
while [ $i -gt 0 ]
do
	routerfile=routerloc$i
	rlocfile=${licdir}/${locfile}.router
	eval rfile=\$$routerfile
	if [ "${rfile}" = "" ]
	then
		break
	fi
	grep "${rfile}" ${rlocfile} > /dev/null 2>&1
	if [ $? -ne 0 ]
	then
		echo "${rfile}" >> ${rlocfile}
		if [ $? -ne 0 ]
		then
			echo Cannot Access license location file ${licdir}/${locfile}  - Exiting
			exit 1
		fi
	fi
	i=`expr $i + 1`
done

exit 0
