#!/bin/sh

target_dir="/etc/opt/licenses"
lit_sw="/etc/opt/licenses/lit_tty"

user=`id | sed -e 's/[^(]*(\([^)]*\).*/\1/'`

if [ "$user" != "root" ]
then
	echo
	echo "You must be superuser to run installpatch."
	echo
	exit 1
fi

prog_dir=`dirname $0`
first_char=`echo $prog_dir |  sed -e 's/^\(.\).*$/\1/'`

if [ "$first_char" != "/" ]
then
	prog_dir=`pwd`/$prog_dir
fi

if [ ! -f $prog_dir/patch_tarfile -o ! -f $prog_dir/components_list ]
then
	echo
	echo "$prog_dir does not have all the information to install the patch"
	echo
	exit 1
fi
echo



if [ ! -d "/etc/opt/licenses" -a ! -f "/etc/opt/licenses/lit_tty" ] 
then
	echo
	echo "Licensing software has not yet been installed"
	echo
	exit 1
fi

echo
echo "Extracting the patch components"
echo

#
# Can't cd to progdir because an attempt to extract the tarfile
# onto the CD will fail.  Instead, use a temporary directory
# and remove it later. 
#
#cd $prog_dir
tmp_dir=/usr/tmp/104217.`date +%y%m%d%H%M%S`
if [ -f $tmp_dir ]; then
  rm -rf $tmp_dir
fi
mkdir $tmp_dir
cd $tmp_dir

tar xf $prog_dir/patch_tarfile

cp $tmp_dir/components/adjust_flexlm_owner $target_dir
chmod 544 $target_dir/adjust_flexlm_owner

cp $tmp_dir/components/lic_mgr $target_dir
cp $tmp_dir/components/lic_srvr_start $target_dir


if [ -h $lit_sw ]; then
   lpath=`/usr/bin/ls -l $lit_sw | sed 's/^.*-> //'`
   ldir=`dirname $lpath`
else
        echo
        echo "Licensing software must first be installed properly before"
        echo "patch is installed"
        echo
        exit 1
fi

mv $tmp_dir/components/config_template $ldir
chmod 644 $ldir/config_template
chown root $ldir/config_template
chgrp sys $ldir/config_template


if [ -x $target_dir/adjust_flexlm_owner ]; then
   $target_dir/adjust_flexlm_owner
else
        echo
        echo "Installation of patch failed."
        echo
        exit 1
fi


rm -rf $tmp_dir/components

echo
echo "Patch installation done."
echo
exit 0
