#!/sbin/sh
#------------------------------------------------------------------#
#
# HPUX_ID: @(#) $Revision: 18.3 $ generated on Fri Jun  1 01:11:43 MDT 2001
#
# (c)Copyright 1983-2001 Hewlett-Packard Co.,  All Rights Reserved.
#
#------------------------------------------------------------------#
# SD postinstall script template for 11.X patches.                 #
#------------------------------------------------------------------#
# This file is optional for all 11.X patches.  The purpose of this #
# script is to prepare for a kernel build when required by the     #
# install conditions.  The script can also drive events that must  #
# occur before a system reboot occurs.                             #
#------------------------------------------------------------------#

_PATCHID=PHSS_23546

UTILS="/usr/lbin/sw/control_utils"

if [ ! -f $UTILS ]; then
    echo "ERROR:   Cannot find $UTILS"
    exit 1
fi

. $UTILS
exitval=$SUCCESS

#####
##### Insert any code you may require at this point in the script.
##### Pay attention to the exitval variable and set appropriately
##### in your code.  If you are unsure as to what types of operations
##### are legal in this file and what types aren't, consult the 
##### "Guidelines for SD Control Scripts" document.
#####
##### START_CUSTOMIZATION_HERE
######################### begin postinstall #####################

#
# These utilities are "almost" identical to the ones 
# located in the control_utils file.  However, we found
# it necessary to over ride the IPD stuff in this
# instance.
#                      - bhl administrator


########
# bhl_cp_retain --
#       $1:     copy fromfile
#       $2:     copy destination path
#
# Unconditionally copy $1 to $2 and set attributes.  Create necessary
# directories.  Caller should verify existence of $1 before calling
# 'bhl_cp_retain'.  Caller is responsible for trapping and handling errors.
#
bhl_cp_retain ()
{
    if [[ $# -ne 2 ]]
    then
	echo "Usage: bhl_cp_retain source target" >&2
	return 1
    fi

    typeset dirname=${2%/*}

    [[ -z $dirname ]] && dirname=/
    r_mkdir 755 bin bin $dirname
    cp -p $1 $2 
    if [[ $? -ne $SUCCESS ]]
    then
	echo "ERROR:   Attempt to copy \"$1\" to \"$2\" failed for the above reason."
	return 1
    else
	#####IPD_addfile $2
	return 0
    fi
}

#
########
# bhl_newconfig_cp
#       $1:     Absolute pathname of file's ultimate location (without any
#		alternate root prefix).
# 
# Handle the conditional copying of a file already delivered to the
# newconfig directory.  This function should only be called from the
# postcustomize script.  Removing the previously delivered newconfig file
# from under /usr/old/usr/newconfig is not done since multiple clients
# could invoke this function concurrently.
# 
bhl_newconfig_cp ()
{
    typeset -i cmd_result=0

    if [[ $# -ne 1 ]]
    then
	echo "ERROR:   Usage: bhl_newconfig_cp path"
	return 1
    fi
    _USRDIR=$SW_LOCATION
    [[ $SW_LOCATION = "/" ]] && _USRDIR=/usr/
    _NC_PATH=$1
    _NC_WRKG=${SW_ROOT_DIRECTORY%/}$_NC_PATH
    _NC_NEW=${SW_ROOT_DIRECTORY%/}${_USRDIR}newconfig${_NC_PATH}
    _NC_PREV=${SW_ROOT_DIRECTORY%/}${_USRDIR}old${_USRDIR}newconfig${_NC_PATH}

    # Check that there really exists an incoming file at the proper location.
    # This is also very likely to catch a relative path passed in.
    if [[ ! -e $_NC_NEW ]]
    then
	echo "ERROR:   Could not find newly delivered file $_NC_NEW."
	return 1
    fi

    if [[ ! -e "$_NC_WRKG" ]]
    then
	# If no working file exists, then it will be installed regardless.
	bhl_cp_retain $_NC_NEW $_NC_WRKG
	cmd_result=$?
	return $cmd_result
    fi

    if [[ ! -e "$_NC_PREV" ]]
    then
	# The previously delivered file has been removed by the user.
	# Documentation should warn against this.
	cmp $_NC_WRKG $_NC_NEW > /dev/null 2>&1
	cmd_result=$?
	if [[ $cmd_result -ne 0 ]]
	then
	    # If the new file and the working file are identical there is no
	    # problem.  But if they differ, we cannot know if the working
	    # file was modified or not.  Rather than overwrite the working
	    # file outright, do nothing.
	    #####IPD_addfile $_NC_WRKG
	    return 100
	else
	    # Working file and new file are identical.  No need to copy, but
	    # the working file needs to be added to the IPD.
	    #####IPD_addfile $_NC_WRKG
	    return 101
	fi
    fi

    cmp $_NC_WRKG $_NC_PREV > /dev/null 2>&1
    cmd_result=$?
    if [[ $cmd_result -eq 0 ]]
    then
	# Working file is identical to the previous file.
	cmp $_NC_WRKG $_NC_NEW > /dev/null 2>&1
	cmd_result=$?
	if [[ $cmd_result -ne 0 ]]
	then
	    # The working file was unmodified from the previous release,  
	    # and the new file differs from the previous file.  Since the
	    # previous default file was acceptable to the user, assume the
	    # new one will be also.  Copy the new file to the working path.
	    # This is the normal update case.  
	    bhl_cp_retain $_NC_NEW $_NC_WRKG
	    cmd_result=$?
	    return $cmd_result
	else
	    # Working file and new file are identical.  No need to copy, but
	    # the working file needs to be added to the IPD.
	    #####IPD_addfile $_NC_WRKG
	    return 101
	fi
    fi

    # Working version differs from the previous file.  Assume the administrator
    # has modified the working version for good cause, and retain it.

    cmp $_NC_NEW $_NC_PREV > /dev/null 2>&1
    cmd_result=$?
    if [[ $cmd_result -eq 0 ]]
    then
	# Take no action.  The new file is identical to previous file, however
	# the working file differs from the new file, and therefore might have
	# been modified by the user.  Notify the user that the working file
	# remains intact and that a default version of the file is under the
	# /usr/newconfig directory.  However, add the working file to the IPD.
	#####IPD_addfile $_NC_WRKG
	return 102
    else
	cmp $_NC_NEW $_NC_WRKG > /dev/null 2>&1
	cmd_result=$?
	if [[ $cmd_result -eq 0 ]]
	then
	    # Precognition case:  take no action.  The new file is identical to
	    # working file, however the working file needs to be added to the
	    # IPD.
	    #####IPD_addfile $_NC_WRKG
	    return 101
	else
	    # Take no action.  The working file differs from both the new file
	    # and the previous file.  Send a message indicating that the working
	    # file remains intact since it was not identical to either the new
	    # file or the previous file, and therefore might have been modified
	    # by the user.  Instruct the user to consider incorporating the new
	    # file into the modified working file.  However, add the working
	    # file to the IPD.
	    #####IPD_addfile $_NC_WRKG
	    return 103
	fi
    fi
} # bhl_newconfig_cp()


##
## Begin XEXT-DBE-MAN
##
#
##
## Adjust SW_INSTALL_DIR to include the entire path to
## the man pages.
##
#
#DBESW_INSTALL_DIR=${SW_INSTALL_DIR}usr/share/man/man3.Z/
#
##
## Save the list of installed files
##
#
#INSTALLED_FILES="   XdbeAllocat.3x \
  	#XdbeBeginId.3x \
  	#XdbeDealloc.3x \
  	#XdbeEndIdio.3x \
  	#XdbeFreeVis.3x \
  	#XdbeGetBack.3x \
  	#XdbeGetVisu.3x \
  	#XdbeQueryEx.3x \
  	#XdbeSwapBuf.3x"
#
##
## Copy the files, ignoring long/short.
##
#
#mv ${DBESW_INSTALL_DIR}XdbeAllocat.3x ${DBESW_INSTALL_DIR}XdbeAllocateBackBufferName.3x
#mv ${DBESW_INSTALL_DIR}XdbeBeginId.3x ${DBESW_INSTALL_DIR}XdbeBeginIdiom.3x
#mv ${DBESW_INSTALL_DIR}XdbeDealloc.3x ${DBESW_INSTALL_DIR}XdbeDeallocateBackBufferName.3x
#mv ${DBESW_INSTALL_DIR}XdbeEndIdio.3x ${DBESW_INSTALL_DIR}XdbeEndIdiom.3x
#mv ${DBESW_INSTALL_DIR}XdbeFreeVis.3x ${DBESW_INSTALL_DIR}XdbeFreeVisualInfo.3x
#mv ${DBESW_INSTALL_DIR}XdbeGetBack.3x ${DBESW_INSTALL_DIR}XdbeGetBackBufferAttributes.3x
#mv ${DBESW_INSTALL_DIR}XdbeGetVisu.3x ${DBESW_INSTALL_DIR}XdbeGetVisualInfo.3x
#mv ${DBESW_INSTALL_DIR}XdbeQueryEx.3x ${DBESW_INSTALL_DIR}XdbeQueryExtension.3x
#mv ${DBESW_INSTALL_DIR}XdbeSwapBuf.3x ${DBESW_INSTALL_DIR}XdbeSwapBuffers.3x
#
##
## End XEXT-DBE-MAN
##

#
# Begin X11-SERV
#

ERROR_FLAG=0

#################
#
# chperm
#
# function to change permissions of directories to 777
#
#################

chperm()
{
    chown bin $1 > /dev/null 2>&1
    chgrp bin $1 > /dev/null 2>&1
    chmod 777 $1 > /dev/null 2>&1
}

################
#
# copynewconfig
#
#    copy files from /usr/newconfig into their target
#    destinations if they do not already exist
#
##############

copynewconfig()
{
    for FILE in `echo $FILES`
    do
        if [ ! -f $DEST/$FILE ] ; then
            cp $SRC$DEST/$FILE $DEST/$FILE && \
            echo "NOTE:    Copying $SRC$DEST/$FILE to $DEST/$FILE" || \
                { echo "ERROR:   Could not copy $SRC$DEST/$FILE to "
                  echo "         $DEST/$FILE . "
                  ERROR_FLAG="yes" ; }
        fi
    done;
}

#############################################
##
## X11_config_files
##
## install various config files using newconfig util rules
##
#############################################
X11_config_files()
{
        DEST=""

        #Borrowed from control_utils: newconfig_cp ()
        USRDIR=$SW_ROOT_DIRECTORY
        NC_WRKG=/etc/X11
        [[ $SW_ROOT_DIRECTORY = "/" ]] && USRDIR=/usr
        NC_PREV=${SW_ROOT_DIRECTORY%/}${USRDIR}/old${USRDIR}/newconfig${NC_WRKG}

        # First some special preprocessing for XHPKeymaps.  If we don't
        # deliver a new one, some systems updating from older won't be usable.
        # Moving the old one will force delivery of the new one.

        if [ -f $NC_WRKG/XHPKeymaps -a -f $NC_PREV/XHPKeymaps ]
          then
          if cmp $NC_WRKG/XHPKeymaps $NC_PREV/XHPKeymaps > /dev/null 2>&1
            then #same
            rm -f /etc/X11/XHPKeymaps
          else #working copy was edited
            mv -f /etc/X11/XHPKeymaps /etc/X11/XHPKeymaps.old 2> /dev/null
            # Make sure it's gone
            [ -f /etc/X11/XHPKeymaps ]  && rm -f /etc/X11/XHPKeymaps
            echo "NOTE:    A new version of $NC_WRKG/XHPKeymaps"
            echo "         has been placed on the system."
            echo "         The new version may be essential to the functioning of your system."
            echo "         The old version is in $NC_WRKG/XHPKeymaps.old."
            echo "         It appears to have been edited.  You may want to"
            echo "         merge your changes into the new version."
          fi
        else # nothing to compare.  Proceed
          rm -f /etc/X11/XHPKeymaps 2> /dev/null
        fi


        #
        # install the X11 config files from their newconfig areas
        #
	while read DEST; do
		bhl_newconfig_cp $DEST
		newconfig_msgs $?
	done <<-EOF
	/etc/X11/XHPKeymaps
	EOF

}

ERROR_FLAG="no"


DESTOPT=/etc/X11
SRC=/usr/newconfig
SOCKETS=/var/spool/sockets
X11SOCKDIR=$SOCKETS/X11

##################################
#
# Set up default files which are not already on the system. 
# X0screens, X0pointerkeys and X0devices now in /etc/X11
#
# If default files are already on the system, they are not overwritten
# in case they have been customized by the user.
#
# Correctly set up /var/spool/sockets install X11
# subdirectory correctly if necessary.
##################################


mkdir -p /etc/X11 > /dev/null 2>&1
DEST=$DESTOPT
FILES=`echo "X0screens X0devices X0pointerkeys"`

	copynewconfig		# move to new /etc/X11

# DO NOT add these files to the IPD so they will be deleted at de-install
#IPD_addfile /etc/X11/X0screens
#IPD_addfile /etc/X11/X0devices
#IPD_addfile /etc/X11/X0pointerkeys

#
# handle copying of the appropriate rgb file
# for 10.0 this will be the the Sony-based rgb file
#

    RGBFILE=rgb.txt.98xxx

    if [ ! -f /etc/X11/rgb.txt ]; then
        cp /usr/newconfig/etc/X11/$RGBFILE $DESTOPT/rgb.txt > /dev/null 2>&1
    fi

#
# Run rgb. Rgb.txt should have been configured
# by the above code
#
# this code was moved to configure to assist the clean update
# from 10.X to 11.X and eliminate the 11.x command running on a 10.x
# kernel
# run in separate shell to avoid directory problems
#(
#        cd $DESTOPT
#        /usr/bin/X11/rgb < rgb.txt > /dev/null 2>&1
#)
	#DO NOT ADD TO IPD
	 #IPD_addfile /etc/X11/rgb.txt
	 #IPD_addfile /etc/X11/rgb.pag
	 #IPD_addfile /etc/X11/rgb.dir

if [ ! -d $X11SOCKDIR ]	;  then
	mkdir -p $X11SOCKDIR > /dev/null 2>&1
	echo "NOTE:    Creating $X11SOCKDIR ."
fi

chperm $X11SOCKDIR
chperm $SOCKETS

X11_config_files

#
# These lines belong in the config file. Otherwise,
# client nodes for NFS diskless don't get set up
# correctly.
#
cond_mkdir 755 bin bin ${SW_ROOT_DIRECTORY%/}/var
cond_mkdir 755 bin bin ${SW_ROOT_DIRECTORY%/}/var/X11
cond_mkdir 755 bin bin ${SW_ROOT_DIRECTORY%/}/var/X11/Xserver
cond_mkdir 777 bin bin ${SW_ROOT_DIRECTORY%/}/var/X11/Xserver/logs

if [ "$ERROR_FLAG" = "yes" ] ; then
    exitval=1
else
    exitval=0
fi

#
# End X11-SERV
#

# copy 2fc1066block stub in place
#
#  Commented out on 5/18, no longer depending on this library
#if [[ ! -f /opt/graphics/common/lib/2fc1066block.1 ]]
#then
#    cp /usr/contrib/tmp/2fc1066blockstub.1 /opt/graphics/common/lib/2fc1066block.1
#    chmod 555 /opt/graphics/common/lib/2fc1066block.1
#    chown bin:bin /opt/graphics/common/lib/2fc1066block.1
#fi

# copy libSX.1 in place (if needed)

SWMOD_FILES="/usr/lib/X11/extensions/libSX.1"
if [[ ! -f /usr/lib/X11/extensions/libSX.1 ]]
then
	cp /usr/newconfig/usr/lib/X11/extensions/libSX.1 /usr/lib/X11/extensions
	chmod 555 /usr/lib/X11/extensions/libSX.1
	chown bin:bin /usr/lib/X11/extensions/libSX.1
        swlist -l file > /tmp/swlist.out 2>/dev/null
        for file in ${SWMOD_FILES}; do
            grepres=`grep $file /tmp/swlist.out`
            if [ $? -eq 0 ]                     # file is in IPD
            then
                grep $file /tmp/swlist.out | awk ' { print $1 } ' | sed -e "s/://" > /tmp/grep.out
                while read prod_fs
                do
                    if [ ! -f $file ]           # file is not on system
                    then
                        swmodify -uxfiles="$file" $prod_fs
                    else
                        swmodify -xfiles="$file" $prod_fs
                    fi
                done < /tmp/grep.out
            fi
        done

rm -f /tmp/swlist.out /tmp/grep.out
fi
####################### end postinstall #########################

exit $exitval
