#!/bin/sh

#
#ident "@(#)postinstall 1.7 15 Jul 1993 SMI"
#
# Copyright 1993 Sun Microsystems, Inc. All Rights Reserved
#
#  Postinstall script for package: <to be defined>
#

# Exit codes for installation scripts
e_ok=0
e_fatal=1      # stop installation on this exit
e_warning=2    # Installation will go on.
e_int=3        # Interrupted. Stop installation
e_reboot=10    # User must reboot after installation of all selected packages
e_rebootnow=20 # User must reboot right after installation of current package
               # To be added to one of the single-digit exit code above

# Trap interrupt
trap `exit $e_int` 15
#
# Who set the path ??? (security issue)
# PATH="/bin:/etc:/usr/sbin"

#
# For kernel package we just need to add_drv the different modules
#       - Check first if its not already done, on /etc/name_to_major
#       - We don't run add_drv if the driver are already installed
#

DRVS="oopi otpi oclt otk6 otmr clns llc"
CONTROL=/etc/rc2.d/S90osinet
REM_DRV="/usr/sbin/rem_drv"
SPECIFIC="/dev/oopi0 /dev/oopi1"
NETDTMP="/tmp/osinetd.status"

grep rk6tr /etc/name_to_major > /dev/null
RK6TR_LOADED=$?

if [ $RK6TR_LOADED = 1 ]
then
#rk6tr is a new driver delivered since patch -04... need to be added via add_drv
    echo ""
    echo "adding rk6tr driver"
    /usr/sbin/add_drv -m '* 0666 root sys' rk6tr
    if [ $? -ne 0 ]; then
	exit $e_fatal
    fi
fi

if [ -f /bin/isainfo ] && [ -x /bin/isainfo ]
then
echo 
else
#this is not a Solaris 7 or +  .... no need to continue.
exit 0
fi

SPARC_VERS=`/bin/isainfo | /bin/grep sparcv9`

if [ x"$SPARC_VERS" = x ]
then
echo 
else
echo You cannot install this patch on a system running 64bit Solaris
echo Please backout this patch / reboot in 32bit / reinstall the patch
exit $e_fatal
fi

# NP CTE fix for bug 4400983 <GS> 17/01/2001
# From S8 onward, netstat -ai output differs from netstat -i and confuses 8.1.1 FCS package install,
# creating an incorrect llcdev0 line in osinetd.conf; we attempt to fix that during patch install.
FILE=/etc/SUNWconn/osinet/osinetd.conf
BAK=/var/SUNWconn/osinet/osinetd.bak
TMP=/tmp/osinetd.conf
# NP CTE fix for bug 4498657 <GS> 24/09/2001
# Remove any domain and higher qualifiers
HOSTNAME=`uname -n | cut -d "." -f1`
# try to obtain correct default interface info, compare with non-qualified hostname
# note that field 4 from netstat -i and netstat -ia also contains non-qualified hostname
DEVICE=`/bin/netstat -ia |
                awk '{ 
                        if ( $4 == hostname )
                                print $1
                }' hostname=$HOSTNAME `
DRIVER=`/bin/echo $DEVICE | /bin/sed -e 's/^\([a-zA-Z]*\)\([0-9]*\)/\1/'`
DEVICE8=`/bin/netstat -i |
                awk '{ 
                        if ( $4 == hostname )
                                print $1
                }' hostname=$HOSTNAME `
DRIVER8=`/bin/echo $DEVICE8 | /bin/sed -e 's/^\([a-zA-Z]*\)\([0-9]*\)/\1/'`

# Bugs 4400983 and 4406728 are S8 specific, only need to modify osinetd.conf or clean up drivers if S8
if [ `uname -r | cut -d "." -f2` = 8 ]; then 	# S8
   # The llcdev0 line is actually corrupt, (more than one word inside quotes in field 9, where ppa 
   # type should be), if the last character extracted by a cut for f9 is not a quote char, and the
   # PPA_FIELD_END variable is set; the line exists and is corrupt.
   PPA_FIELD_END=`/bin/grep '^llcdev0' $FILE | cut -d " " -f 9 | sed 's/^"\([a-zA-Z]*\)\(.\)$/\2/'`
   
   # PPA_FIELD_END, DEVICE or DEVICE8 were not set to sane values, can't proceed but postinstall must not fail
   if [ `echo $DEVICE8 | wc -w` != 1 ] || [ `echo x$DEVICE8` = x ] || [ `echo x$PPA_FIELD_END` = x ] ; then
	echo " Patch postinstall script could not fix the llcdev0 line in the OSI config file"
	echo " $FILE."
	echo " The postinstall should exit normally but you may need to manually fix the llcdev0"
	echo " line (bug 4400983) and perform the workaround for bug 4406728, (see Sunsolve)"
	echo " before the default OSI config will work."
	exit 0
   fi
   if [ "${PPA_FIELD_END}" = '"' ] || [ `ps -elf | grep osinetd | egrep -v grep | wc -w` != 0 ] ; then
   	# No corruption, (probably manually corrected already), or stack is running, do nothing !
   	exit 0
   fi
   # End NP CTE fix for bug 4498657

   # Perform actual fix on llcdev0 line in osinetd.conf
   cp $FILE $BAK
   cp $FILE $TMP
   cat $TMP | sed -e "s/$DRIVER/$DRIVER8/g" -e "s/${DRIVER8}. fla/fla/" > $FILE
   rm $TMP

   # NP CTE fix for bug 4406728, pb installing patch S8 unless reboot after the 
   # FCS pkg installation, <GS> 22/Jan/2001.
   RDRVS="otpi oclt otk6 otmr clns llc oopi"
   ADRVS="oopi otpi oclt otk6 otmr clns llc"
   /bin/diff $FILE $BAK >/dev/null 2>&1
   if [ $? = 1 ] # we really did fix the conf line, so stopping osinetd will not disturb a good install
   then
     # NP CTE fix for bug 4400983 <GS> 17/01/2001
     echo "located an erroneous llcdev0 line in etc/SUNWconn/osinet/osinetd.conf file"
     echo " Repaired !"
     # END NP CTE fix for bug 4400983 <GS> 17/01/2001
     echo ""
     echo Installation of the SUNWcosia FCS package on Solaris 5.8 seems to have 
     echo failed to create all devices correctly. Please ignore any errors  resulting 
     echo from the pkgadd above.
     echo ""
     echo Ensuring correct SUNWcosia installation by deleting and re-creating devices.
     $CONTROL stop
     for i in `echo $RDRVS` ; do
 	echo "rem_drv $i"
 	/usr/sbin/rem_drv $i
     done
     echo "      =      "
     # NP CTE fix for bug 4442594, <GS> 12/April/2001 permissions on drivers not explicitly set
     for i in `echo $ADRVS` ; do 
 	echo "add_drv $i"
 	/usr/sbin/add_drv -m '* 0666 root sys' $i
   done
   echo " Done !"
   echo ""
 fi # script fixed conf file
 # No evidence of a real need to do this after add_drv -m, but other postinstall code paths do, it causes no harm
 /bin/chmod 666 /dev/oopi /dev/otpi /dev/oclt /dev/otk6 /dev/clns /dev/llc 2>&1 > /dev/null
 # END NP CTE fix for bug 4442594
 exit 0
 # End NP CTE fix for bug 4406728
fi #S8
# End of NP CTE fix for bug 4400983

OS_REL=`/bin/uname -r`
/bin/grep oopi /etc/name_to_major > /dev/null
OOPI_LOADED=$?
/bin/grep otpi /etc/name_to_major > /dev/null
OTPI_LOADED=$?
if [ $OOPI_LOADED = 0 ] && [ $OTPI_LOADED != 0 ]
then
  echo Your previous SUNWcosia installation failed
  echo Resuming SUNWcosia installation.
else
  exit 0
fi

NAME_DRV="/etc/name_to_major"

BASEDIR=/ ; export BASEDIR
#
# Drivers installation
#

echo "Installing the OSI drivers ..."

if [ ! -r "$NAME_DRV" ]; then
        echo "Cannot access $NAME_DRV"
        echo "Please contact your system administrator"
        exit $e_fatal
fi

for D in $DRVS; do
        if grep "^$D" $NAME_DRV 2>&1 >/dev/null ; then
                echo "Removing $D driver ..."
                $REM_DRV $D
                #
                # We should not have to do this manually. The system (rem_drv)
                # should remove these files automatically (OS BUG?).
                #
                rm -f /devices/pseudo/clone*:$D # for 1093 compatibility
                rm -f /dev/$D
        else
                echo "The $D driver is already removed"
        fi
done
#
# Remove Specific OSI Installation stuff
#
for I in $SPECIFIC; do
        if [ -c "$I" -o  -h $I ]; then
                rm -f $I
        fi
done

for D in $DRVS ; do
        if egrep "^$D\>" "$NAME_DRV" 2>&1 >/dev/null ; then
                echo "$D already defined on this system"
        else
                # Create driver with proper permissions!
                echo "adding $D driver"
                /usr/sbin/add_drv -m '* 0666 root sys' $D
                if [ $? -ne 0 ]; then
                        echo Problem detected on $D instalation REBOOT is mandatory after patch installation
                        #exit $e_fatal
                fi

        fi
done

chmod 666 /dev/oopi /dev/otpi /dev/oclt /dev/otk6 /dev/clns /dev/llc 2>&1 > /dev/null

#
# Adding specific OSI stuff
#       - Creating  /dev/oopi0 and /dev/oopi1
# Should be done by the ddi_create_minor on the
# Streams attach function
#

MAJOR=`grep oopi $NAME_DRV | awk '{ print $2 }'`
if [ ! -c /dev/oopi0 ]; then
        /etc/mknod /dev/oopi0 c $MAJOR 0
fi
if [ ! -c /dev/oopi1 ]; then
        /etc/mknod /dev/oopi1 c $MAJOR 1
fi

#
# Licensing
#

# @(#)postinstall_prod  1.6 11/19/92 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

license_file=osistk8.0

# 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

dirloc=/etc/opt/licenses

#
# Add (Osman.)
#
mkdir -p $dirloc 2>&1 >/dev/null

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

if [ ! -d ${BASEDIR}/${dirloc} ]
then
        echo Cannot Access license location directory ${BASEDIR}/${dirloc}  - Exiting
        exit 1
fi

#
# Find new names for existing new license files in licdir
#
if [ -f  ${licdir}/${licfile}* ]
then
        for infile in `ls ${licdir}/${licfile}*`
        do

                i=0
                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 ${licdir}/${basefile} $outputfile
                if [ $? -ne 0 ]
                then
                        echo Exiting
                        exit 1
                fi
                rm ${licdir}/${basefile}
        done
        echo Licenses copied to $BASEDIR/$dirloc
else
        echo No licenses available to insert.
        echo Make sure to run the license insertion tool or the license \
configuration script on this machine.
fi

echo $BASEDIR/${dirloc} > ${licdir}/${locfile}
if [ $? -ne 0 ]
then
        echo Cannot Access license location file ${licdir}/${locfile}  - Exiting
        exit 1
fi

#
# Happy End
exit $e_ok

