# This script creates the backout package for a patch package
#
# directory format options.
#
#       @(#)postinstall 1.4 95/10/10 SMI
#
# Copyright (c) 1995 by Sun Microsystems, Inc.
# All rights reserved
#

########################################################################
# SUNWpcnet specific section - start
########################################################################

#############################
#############################
## Display error messages
#############################
#############################
Display_error() {
        if [ "$#" -ne 0 ] ; then
                echo ""
                echo "FEHLER!!! $1"
                echo ""
        fi
}

# Check for valid run environment
if [ ! -d /usr/bin ] ; then
	exit 1
fi
PATH="/usr/sbin:/usr/bin:/bin"
SNPATH="/opt/SUNWpcnet/1.5/pc/pro/bin"
LICFILE="nfswlsn.exe"
export PATH
SUNW_PCW_LAN=":SNCpath=\"/opt/SUNWpcnet/%CLNT_VERS%/site\":SNCboot=\"pcnfspro\\\\boot.snc\":SNClogin=\"pcnfspro\\\\login.snc\":SNClgout=\"pcnfspro\\\\logout.snc\":"

SOLARNETDIR=/opt/SUNWpcnet
SNTABLE=${SOLARNETDIR}/bin/sntable
SNDHCPINIT=${SOLARNETDIR}/bin/sndhcpinit
SNDHCPADD=${SOLARNETDIR}/bin/sndhcpadd
SNCONFIG=${SOLARNETDIR}/bin/snconfig
SNSETUP=${SOLARNETDIR}/bin/snsetup

NEEDEDFILES="\
${SNTABLE} \
${SNDHCPINIT} \
${SNSETUP} \
${SNDHCPADD}"

SUCCESS=0
ERRRET=2

ERRNOFILE="Folgende erforderliche Datei wurde nicht gefunden: "
ERRNODHCPTAB="Die dhcptab-Tabelle ist nicht vorhanden und kann nicht korrigiert werden!"
ERRNORIGHTS="Benutzer ist nicht befugt, die dhcptab-Tabelle zu aktualisieren!"
ERRDHCPINIT="Das Programm ${SNDHCPINIT} konnte die dhcptab-Tabelle nicht ordnungsgem initialisieren!"
ERRDHCPADD="Das Programm ${SNDHCPADD} konnte das neue Client-Makro nicht erfolgreich hinzufgen."
ERRSNSETUP="The ${SNSETUP} program failed to determine the PC-Admin nameservice."
ERRSNCONFIG="
${SNCONFIG} mu ausgefhrt werden, nachdem die Korrektur installiert ist!"
ERRCP="Kopieren fehlgeschlagen: "

########################################################################
# SUNWpcnet specific section - end
########################################################################

PATH=/usr/sadm/bin:$PATH

if [ "$PKG_INSTALL_ROOT" = "/" ]; then
	PKG_INSTALL_ROOT=""
fi

if [ -n "$PATCH_BUILD_DIR" -a -d "$PATCH_BUILD_DIR" ]; then
	BUILD_DIR="$PATCH_BUILD_DIR/$SUNW_PATCHID.$PKGINST"
else
	BUILD_DIR="$PKG_INSTALL_ROOT/var/tmp/$SUNW_PATCHID.$PKGINST"
fi

FILE_DIR=$BUILD_DIR/files
RELOC_DIR=$FILE_DIR/reloc
ROOT_DIR=$FILE_DIR/root
BO_Deletes=$FILE_DIR/deletes
THIS_DIR=`dirname $0`
PROTO_FILE=$BUILD_DIR/prototype

if [ "$PATCH_PROGRESSIVE" = "true" ]; then

########################################################################
# SUNWpcnet specific section - start
########################################################################

rm -f ${SNPATH}/${LICFILE}

###########################################
# Check for the existence of needed files
###########################################
for FILE in ${NEEDEDFILES}
do
        if [ ! -f "${FILE}" ] ; then
                Display_error "${ERRNOFILE} ${FILE}"
                exit 1
        fi
done

############################################################
# Check to see if a dhcptab exists.  If it doesn't then we
# can't update it.
############################################################
${SNTABLE} -Q -t dhcptab
if [ "$?" -ne ${SUCCESS} ] ; then
	Display_error "${ERRNODHCPTAB} ${ERRSNCONFIG}"
	exit 0
fi

#################################
# Verify user can update table.
#################################
myuser=`/usr/bin/id | /usr/bin/cut -f1 -d\) | /usr/bin/cut -f2 -d\(`
${SNTABLE} -Q -t dhcptab -u ${myuser}
if [ "$?" -ne ${SUCCESS} ] ; then
	Display_error "${ERRNORIGHTS} ${ERRSNCONFIG}"
	exit 0
fi

########################################################
# Update the dhcptab with the new template information
########################################################
${SNDHCPINIT} -i
if [ "$?" -ne ${SUCCESS} ] ; then
	Display_error "${ERRDHCPINIT} ${ERRSNCONFIG}"
	exit 0
fi

##############################################################################
# Check for installed start/stop script.  If exists, use it to start daemons.
##############################################################################
if [ -f /etc/init.d/solarnet ] ; then
        echo ""
        echo "   Starte SolarNet-Dmon-Prozesse..."
        echo ""
        /etc/init.d/solarnet start
	sleep 5
fi

##########################################################################
# Check to see if the PC-Admin server is using NIS+ as it's nameservice.
# If it is, then it might already have the client macro defined.
##########################################################################
NAMESERVICE=`${SNSETUP} -q`
if [ "$?" -ne ${SUCCESS} ] ; then
	Display_error "${ERRSNSETUP} ${ERRSNCONFIG}"
	exit 0
fi
	
if [ "${NAMESERVICE}" = "nisplus" ] ; then
	/usr/bin/nismatch key=SUNW.PCW.LAN dhcptab.org_dir > /dev/null
        if [ "$?" -eq ${SUCCESS} ] ; then
		exit 0
	fi
fi

###########################################
# Add the new client class to the dhcptab
###########################################
${SNDHCPADD} -m SUNW.PCW.LAN -d ${SUNW_PCW_LAN}
if [ "$?" -ne ${SUCCESS} ] ; then
	Display_error "${ERRDHCPADD} ${ERRSNCONFIG}"
	exit 0
fi

########################################################
# Send a SIGHUP to DHCP so that it rereads the dhcptab
########################################################
pid=`/usr/bin/ps -ef | 
	/usr/bin/grep /opt/SUNWpcnet/sbin/in.dhcpd |
	/usr/bin/grep -v grep |
	/usr/bin/sed -e 's/^  *//' -e 's/  */ /' |
	/usr/bin/cut -d' ' -f2`
if [ "${pid}" != "" ] ; then
	echo "Sending SIGHUP to DHCP daemon"
	kill -1 ${pid} > /dev/null 2>&1
fi


########################################################################
# SUNWpcnet specific section - end
########################################################################

	exit 0
fi

#
# At this point we either have a deletes file or we don't. If we do,
# we create a prototype entry.
#
if [ -f $BO_Deletes ]; then
	echo "i deletes=$BO_Deletes" >> $BUILD_DIR/prototype
fi

#
# Now delete everything in the deletes list after transferring
# the file to the backout package and the entry to the prototype
# file. Remember that the pkgmap will get the CLIENT_BASEDIR path
# but we have to actually get at it using the BASEDIR path. Also
# remember that removef will import our PKG_INSTALL_ROOT
#
Our_Deletes=$THIS_DIR/deletes
if [ -f $Our_Deletes ]; then
	cd $BASEDIR

	cat $Our_Deletes | while read path; do
		Reg_File=0

		if valpath -l $path; then
			Client_Path="$CLIENT_BASEDIR/$path"
			Build_Path="$RELOC_DIR/$path"
			Proto_Path=$BASEDIR/$path
		else	# It's an absolute path
			Client_Path=$path
			Build_Path="$ROOT_DIR$path"
			Proto_Path=$PKG_INSTALL_ROOT$path
		fi

		# Note: If the file isn't really there, pkgproto
		# doesn't write anything.
		LINE=`pkgproto $Proto_Path=$path`
		ftype=`echo $LINE | nawk '{ print $1 }'`
		if [ $ftype = "f" ]; then
			Reg_File=1
		fi

		if [ $Reg_File -eq 1 ]; then
			# Add source file to the prototype entry
			if [ "$Proto_Path" = "$path" ]; then
				LINE=`echo $LINE | sed -e s@$Proto_Path@$Build_Path@2`
			else
				LINE=`echo $LINE | sed -e s@$Proto_Path@$Build_Path@`
			fi

			DirName=`dirname $Build_Path`
			# make room in the build tree
			mkdir -p $DirName
			cp -p $Proto_Path $Build_Path
		fi

		# Insert it into the prototype file
		echo $LINE 1>>$PROTO_FILE 2>/dev/null

		# Remove the file only if it's OK'd by removef
		rm `removef $PKGINST $Client_Path` 1>/dev/null 2>&1
	done
	removef -f $PKGINST

	rm $Our_Deletes
fi

#
# Unless specifically denied, make the backout package.
#
if [ "$PATCH_NO_UNDO" != "true" ]; then
	cd $BUILD_DIR	# We have to build from here.

	if [ -n "$PATCH_UNDO_ARCHIVE" -a -d "$PATCH_UNDO_ARCHIVE" ]; then
		STAGE_DIR="$PATCH_UNDO_ARCHIVE/$SUNW_PATCHID"
		ARCHIVE_DIR="$STAGE_DIR/$PKGINST"
		mkdir -p $ARCHIVE_DIR
	else
		if [ -d $PKGSAV/$SUNW_PATCHID ]; then
			rm -r $PKGSAV/$SUNW_PATCHID
		fi
		STAGE_DIR=$PKGSAV
		ARCHIVE_DIR=$PKGSAV/$SUNW_PATCHID
		mkdir $ARCHIVE_DIR
	fi

	pkgmk -o -d $STAGE_DIR 1>/dev/null 2>&1
	pkgtrans -s $STAGE_DIR $ARCHIVE_DIR/undo $PKG 1>/dev/null 2>&1
	compress $ARCHIVE_DIR/undo
	rm -r $STAGE_DIR/$PKG

	cd ..
	rm -r $BUILD_DIR
fi

#
# Since this apparently worked, we'll mark as obsoleted the prior
# versions of this patch - installpatch deals with explicit obsoletions.
#
cd ${PKG_INSTALL_ROOT:-/}
cd var/sadm/pkg

active_base=`echo $SUNW_PATCHID | nawk '
	{ print substr($0, 1, match($0, "-")-1) } '`

List=`ls -d $PKGINST/save/${active_base}*`
if [ $? -ne 0 ]; then
	List=""
fi

for savedir in $List; do
	if [ `basename $savedir` = $SUNW_PATCHID ]; then
		break
	fi

	if [ -f $savedir/undo ]; then
		mv $savedir/undo $savedir/obsolete
		echo $SUNW_PATCHID >> $savedir/obsoleted_by
	elif [ -f $savedir/undo.Z ]; then
		mv $savedir/undo.Z $savedir/obsolete.Z
		echo $SUNW_PATCHID >> $savedir/obsoleted_by
	elif  [ -f $savedir/obsolete -o -f $savedir/obsolete.Z ]; then
		echo $SUNW_PATCHID >> $savedir/obsoleted_by
	fi
done

# If additional operations are required for this package, place
# those package-specific commands here.

########################################################################
# SUNWpcnet specific section - start
########################################################################

rm -f ${SNPATH}/${LICFILE}

###########################################
# Check for the existence of needed files
###########################################
for FILE in ${NEEDEDFILES}
do
        if [ ! -f "${FILE}" ] ; then
                Display_error "${ERRNOFILE} ${FILE}"
                exit 1
        fi
done

############################################################
# Check to see if a dhcptab exists.  If it doesn't then we
# can't update it.
############################################################
${SNTABLE} -Q -t dhcptab
if [ "$?" -ne ${SUCCESS} ] ; then
	Display_error "${ERRNODHCPTAB} ${ERRSNCONFIG}"
	exit 0
fi

#################################
# Verify user can update table.
#################################
myuser=`/usr/bin/id | /usr/bin/cut -f1 -d\) | /usr/bin/cut -f2 -d\(`
${SNTABLE} -Q -t dhcptab -u ${myuser}
if [ "$?" -ne ${SUCCESS} ] ; then
	Display_error "${ERRNORIGHTS} ${ERRSNCONFIG}"
	exit 0
fi

########################################################
# Update the dhcptab with the new template information
########################################################
${SNDHCPINIT} -i
if [ "$?" -ne ${SUCCESS} ] ; then
	Display_error "${ERRDHCPINIT} ${ERRSNCONFIG}"
	exit 0
fi

##############################################################################
# Check for installed start/stop script.  If exists, use it to start daemons.
##############################################################################
if [ -f /etc/init.d/solarnet ] ; then
        echo ""
        echo "   Starte SolarNet-Dmon-Prozesse..."
        echo ""
        /etc/init.d/solarnet start
	sleep 5
fi

##########################################################################
# Check to see if the PC-Admin server is using NIS+ as it's nameservice.
# If it is, then it might already have the client macro defined.
##########################################################################
NAMESERVICE=`${SNSETUP} -q`
if [ "$?" -ne ${SUCCESS} ] ; then
	Display_error "${ERRSNSETUP} ${ERRSNCONFIG}"
	exit 0
fi
	
if [ "${NAMESERVICE}" = "nisplus" ] ; then
	/usr/bin/nismatch key=SUNW.PCW.LAN dhcptab.org_dir > /dev/null
        if [ "$?" -eq ${SUCCESS} ] ; then
		exit 0
	fi
fi

###########################################
# Add the new client class to the dhcptab
###########################################
${SNDHCPADD} -m SUNW.PCW.LAN -d ${SUNW_PCW_LAN}
if [ "$?" -ne ${SUCCESS} ] ; then
	Display_error "${ERRDHCPADD} ${ERRSNCONFIG}"
	exit 0
fi

########################################################
# Send a SIGHUP to DHCP so that it rereads the dhcptab
########################################################
pid=`/usr/bin/ps -ef | 
	/usr/bin/grep /opt/SUNWpcnet/sbin/in.dhcpd |
	/usr/bin/grep -v grep |
	/usr/bin/sed -e 's/^  *//' -e 's/  */ /' |
	/usr/bin/cut -d' ' -f2`
if [ "${pid}" != "" ] ; then
	echo "Sending SIGHUP to DHCP daemon"
	kill -1 ${pid} > /dev/null 2>&1
fi

########################################################################
# SUNWpcnet specific section - end
########################################################################

exit 0
