#!/ibin/sh
#       %Z% %M% %I% %E% %Q%
#
#	Copyright (C) 2001 The Santa Cruz Operation, Inc.
#		All Rights Reserved.
#	The information in this file is provided for the exclusive use of
#	the licensees of The Santa Cruz Operation, Inc.  Such users have the
#	right to use, modify, and incorporate this code into other products
#	for purposes authorized by the license agreement provided they include
#	this notice and the associated copyright notice with any such product.
#	The information in this file is provided "AS IS" without warranty.
#
#******************************************************************************
#-----------------------------------------------------------------------------
#  COMMENTS/DESCRIPTION
#-----------------------------------------------------------------------------
#
#       input:
#           $1 is the name of the step
#           $2 is the keyword list (e.g., INSTALL, UPGRADE, REMOVE)
#           $3 is the package list
#============================================================================

#====================================================================== INT ===
#  cleanup -- 
#   
#    cleanup exits the package script with the passed argument 
#
#------------------------------------------------------------------------------
cleanup()
{
	exit $1
}

#====================================================================== INT ===
#  do_save -- 
#   
#    do_save uses ccsUpgradeTool to save the RTS config files
#
#------------------------------------------------------------------------------
do_save()
{
	return # don't do anything here for now
}

#=================CUSTOM INSTALLATION STEP FUNCTIONS=================== INT ===
#
#====================================================================== INT ===
#  do_preload -- 
#   
#	If the keyword "UPGRADE" is specified, but the old version being
#	upgraded is not an SSO component, special action may be required
#	at this point. See guidelines 4.6.2 "How to Handle In-Place Upgrade".
#
#	Note that only the files in the "SharedControl" package of the
#	component have been loaded onto the target at this point.
#
#------------------------------------------------------------------------------
do_preload()
{
	# calls do_save to save config files for upgrade
	#

	do_save
}

do_postload()
{
    :
}

#====================================================================== INT ===
#  do_preattach -- 
#
#	In this step, the ccs should make any necessary version-compatibility
#	checks. If, for some reason it is not valid to install the component
#	on the given client -- perhaps an inappropriate version of the
#	operating system is installed, for instance -- the ccs should return
#	an exit code of $FAIL, causing teh installation to fail.
#
#	If the keyword "UPGRADE" is specified, but the old version being
#	upgraded is not an SSO component, special action is required at this
#	point. See guidelines 4.6.2 "How to Handle In-Place Upgrade".
#   
#------------------------------------------------------------------------------
do_preattach()
{
	#
	#	< FILL IN HERE >
	#
	# perform necessary version-compatibility checks. perform a 
	#	cleanup $FAIL
	# if something is not right after loggin the reasons.
	#

	# calls do_save to save config files for network client upgrade
	#

	do_save
}

#====================================================================== INT ===
#  do_postattach -- 
#
#	If the keyword "UPGRADE" is specified, this is the step in which the
#	ccs should merge the old configuration data into the appropriate files.
#
#	If the old version being upgraded was not an SSO component, the old
#	configuration data will have been saved in the CCS_PERSISTENT_STORAGE by
#	an earlier script phase.
#
#	After the saved data has been restored, any "query" parameters that
#	necessitate further self-configuration should be applied. Only files
#	wholly owned by this component -- i.e., those in the /var/opt area
#	-- can be modified.
#   
#------------------------------------------------------------------------------
do_postattach()
{
        case "$upgrade" in
	    nonSSOupgrade)
		# preserve any needed data from a 3.2v4 installation
		;;
	    SSOupgrade)
		# preserve any needed data from an OSr5 installation
                ;;
	esac
}

#====================================================================== INT ===
#  do_preregister -- 
#   
#	Here is where ScoAdmin OSAs that need to be available to other
#	components during those component's "configure" phase should be
#	installed. This implies some stringent restrictions on the
#	implementation of such OSA's: they must operate completely within
#	the boundries of their own component provided tools and data,
#	possibly relying as well on tools available in /ibin.
#
#------------------------------------------------------------------------------
do_preregister()
{
	:
	# No work defined yet
}

#====================================================================== INT ===
#  do_postregister -- 
#
#	No work is defined
#
#------------------------------------------------------------------------------
do_postregister()
{
	:
	# No work defined yet
}

#====================================================================== INT ===
#  do_preconfigure -- 
#   
#	The component should configure itself into any system service of which
#	it is a consumer. For instance, calls to "idinstall" should be made
#	here to add a drive into (or, in the case of a removal, to pull a
#	driver out of) the link kit.
#
#------------------------------------------------------------------------------
do_preconfigure()
{
	:
	#
	#	< FILL IN HERE >
	#
	# This is where things that need to register with OSA's should call
	# those OSAs.
}

#====================================================================== INT ===
#  do_postconfigure -- 
#
#	Any remaining configuration that relied on steps taken in the pre-
#	configurure and system steps should be completed here
#
#------------------------------------------------------------------------------
do_postconfigure()
{
	:
	# Continue anything started in do_preconfigure that needs finishing
	# up.
}

# Usage: filter target-file filter-command
filter()
{
    target=$1
    shift
    cp -- $target $target# || return 1
    "$@" < $target# > $target || {
	cat -- $target# > $target && rm -f -- $target#
	return 1
    }
    rm -f -- $target#
}

#====================================================================== INT ===
#  do_preexport
#
# Commands run before an export
#   
#------------------------------------------------------------------------------
do_preexport()
{
    :
}

# Usage: maybemkdir dirname mode [ownership]
maybemkdir()
{
    dir=$1
    [ $# -ge 2 ] && mode=$2 || mode=755
    [ $# -ge 3 ] && owner=$3 || owner=bin:bin

    [ -d $dir ] || mkdir $dir
    chown $owner $dir
    chmod $mode $dir
}

do_makewhatis()
{
    if [ -x /usr/man/bin/makewhatis ]; then
	for section in 1 3 5 8; do
	    [ -d /usr/man/man.$section ] &&
	    /usr/man/bin/makewhatis /usr/man/man.$section
	done
    fi
}

#====================================================================== INT ===
#  do_postexport -- 
#   
#	In the final phase, all the remaining public interfaces are exported
#	to the system's public name space.
#
#------------------------------------------------------------------------------
do_postexport()
{
    maybemkdir /var/spool/cups 710 lp
    maybemkdir /var/spool/cups/tmp 1770 lp
    maybemkdir /etc/cups/interfaces
    maybemkdir /etc/cups/ppd
    maybemkdir /etc/cups/ssl 700 root:sys
    maybemkdir /var/log
    maybemkdir /var/log/cups
    maybemkdir /var/run
    maybemkdir /var/run/cups 770 lp:lp
    maybemkdir /var/run/cups/certs 711 lp:lp
    maybemkdir /var/cache
    maybemkdir /var/cache/cups
    maybemkdir /var/cache/cups/rss 775 root:lp
    maybemkdir /usr/lib/cups/driver

    find /usr/share/cups/templates -name '*.N' | sed 's/\.N$//' |
    while read file
    do
	if [ ! -f "$file" ]; then
	    cp $file.N $file
	    chown bin:bin $file
	    chmod 444 $file
	fi
    done
    find /etc/cups -name '*.N' | sed 's/\.N$//' |
    while read file
    do
	if [ ! -f "$file" ]; then
	    cp $file.N $file
	    chown root:lp $file
	    chmod 640 $file
	fi
    done

    # Update whatis database to reflect installed man pages
    do_makewhatis

    # Update docview
    [ -e /usr/share/meta/doc/$DOCDIR/.directory ] && {
        doctool --add $DOCDIR/.directory > /dev/null 2>&1
    }
    for f in $docview_files; do
	doctool --add $DOCDIR/PR_cups$f.desktop > /dev/null 2>&1
    done

    # If SYSV printing is not currently installed,
    # configure the print system to be CUPS.
    if [ ! -f /etc/default/lpd ]; then
	echo PRINT_SYSTEM=CUPS > /etc/default/lpd
	chmod 644 /etc/default/lpd
    fi
	
    # If we're in multiuser mode, start the scheduler.
    set -- `LANG=C who -r`
    # This will kill any old instance of the daemon if neccessary,
    # and then start the daemon
    [ "$3" = 2 ] && /etc/init.d/cups start >/dev/null 2>&1
}

#=================CUSTOM REMOVAL STEP FUNCTIONS-----=================== INT ===
#
#====================================================================== INT ===
#  do_preunexport -- 
#   
#	In the first phase, all the extra public interfaces are unexported
#	from the system's public name space.
#
#------------------------------------------------------------------------------
do_preunexport()
{
    [ -x /etc/rc2.d/S99cups ] && /etc/rc2.d/S99cups stop >/dev/null 2>&1
    [ -d /var/spool/cups ] && rm -rf /var/spool/cups
    # Update docview
    for f in $docview_files; do
	doctool --remove $DOCDIR/PR_cups$f.desktop > /dev/null 2>&1
    done
    [ -e /usr/share/meta/doc/$DOCDIR/.directory ] && {
        doctool --remove $DOCDIR/.directory > /dev/null 2>&1
    }
}

do_postunexport()
{
    # Update whatis database to reflect removed man pages
    do_makewhatis
}

#====================================================================== INT ===
#  do_preunconfigure -- 
#   
#	The component should unconfigure itself from any system service it is
#	a consumer of, using calls to the appropriate service interface. For
#	instance, kernel drivers should be removed by a call to the "idinstall"
#	utility.
#
#------------------------------------------------------------------------------
do_preunconfigure()
{
	:
	# This is where things that needed to register with OSA's should
	# remove themselves from those OSAs.
}

#====================================================================== INT ===
#  do_postunconfigure -- 
#
#	No work is defined
#
#------------------------------------------------------------------------------
do_postunconfigure()
{
	:
	# No work defined yet
}

#====================================================================== INT ===
#  do_preunregister -- 
#
#	For components which provide system services, any work which is
#	necessary prior to the removal of those services should be completed
#	in this step. Depending on the service, it may be appropriate to
#	inform all service consumers of the service's removal, using a
#	provider-defined interface. See the guidlines 3.4 "Responsibilies
#	involved with providing a system service."
#
#------------------------------------------------------------------------------
do_preunregister()
{
	:
	# This is where OSAs should call classconf & osaconf to remove
	# themselves from the osa registration database
	#
}

#====================================================================== INT ===
#  do_postunregister -- 
#
#	No work is defined
#
#------------------------------------------------------------------------------
do_postunregister()
{
	:
	# No work defined yet
}

#====================================================================== INT ===
#  do_preunattach -- 
#
#	No work is defined
#
#------------------------------------------------------------------------------
do_preunattach()
{
	:
	# No work defined yet
}

#====================================================================== INT ===
#  do_postunattach -- 
#
#	No work is defined
#
#------------------------------------------------------------------------------
do_postunattach()
{
	:
	# No work defined yet
}


#====================================================================== INT ===
#  do_preunload -- 
#
#	No work is defined
#
#------------------------------------------------------------------------------
do_preunload()
{
	:
	# No work defined yet
}

#====================================================================== INT ===
#  do_postunload -- 
#
#	No work is defined
#
#------------------------------------------------------------------------------
do_postunload()
{
	:
	# No work defined yet
}

#====================================================================== INT ===
#  main --
#   
#    Based on step specified, perform actions of the general form:
#       Global initialization
#       Loop to call each package script for package-specific actions
#       Global cleanup
#
#    NOTE: Currently, a number of steps exist for which no action needs to
#    be specified in this product.  This will probably be true for most
#    products in the near future.
#------------------------------------------------------------------------------

# NOTE: Do not "cd" around the place as the SSO_CLIENT_ROOT and related
#       variables are of relative path and your use of them will be damaged
#	if you change your location.

# set a trap so if the user dels out, a WARN exit value is passed
# to the calling program
trap 'cleanup $WARN' 1 2 3 15

# Process the arguments to this script and perform action
# for the step being executed currently
pkg_step=$1
pkg_keywords=$2
pkg_list=$3

# Source in the standard functions library
. ccsSetup.sh
ETCISL=
# source in iqm file or SAVE_FILE if iqm file not found 
if [ -n "$IQM_FILE" -a -f "$IQM_FILE" ]; then
    ETCISL=yes
    . $IQM_FILE
elif [ -f "$SAVE_FILE" ]; then
    . $SAVE_FILE 
fi

#[ -n "$IQM_FILE" ] && interactive=false || interactive=true
interactive=false

export IQM_TIMEZONE

# determine action to perform, default is install
upgrade=
removal=
for word in $pkg_keywords
do
	case "$word" in
		OLD_CUSTOM_UPGRADE) upgrade=nonSSOupgrade
		;;
		UPGRADE) upgrade=SSOupgrade
		;;
	esac
done

docview_files="AG IPP Oview SPM SSR UG WN NP"
DOCDIR=AdminHelpTopics/PRhome/PRgroup/PR_cups

# This first case details in which order an "installation" takes place in:
case "$pkg_step" in
	PRE_LOAD)
			do_preload
			;;
	POST_LOAD)
			do_postload
			;;
	PRE_ATTACH)
			do_preattach
			;;
	POST_ATTACH)
			do_postattach
			;;
	PRE_REGISTER)
			do_preregister
			;;
	POST_REGISTER)
			do_postregister
			;;
	PRE_CONFIGURE)
			do_preconfigure
			;;
	POST_CONFIGURE)
			do_postconfigure
			;;
	PRE_EXPORT)
			do_preexport
			;;
	POST_EXPORT)
			do_postexport
			;;
esac

# This first case details in which order an "removal" takes place in:
case "$pkg_step" in
	PRE_UNEXPORT)
			do_preunexport
			;;
	POST_UNEXPORT)
			do_postunexport
			;;
	PRE_UNCONFIGURE)
			do_preunconfigure
			;;
	POST_UNCONFIGURE)
			do_postunconfigure
			;;
	PRE_UNREGISTER)
			do_preunregister
			;;
	POST_UNREGISTER)
			do_postunregister
			;;
	PRE_UNATTACH)
			do_preunattach
			;;
	POST_UNATTACH)
			do_postunattach
			;;
	PRE_UNLOAD)
			do_preunload
			;;
	POST_UNLOAD)
			do_postunload
			;;
esac

cleanup $OK
