#!/bin/sh
#
# lpsdeinstall -- Script to de-install an existing version of the
#                 Digital PrintServer (LPS) Software
#
# @(#)lpsdeinstall	1.22	LPS_UNX_COM	4/17/95
#
# usage:  lpsdeinstall
#
# Note:  This script can only deinstall LPS V5.1 software installations.
###

SCRIPTPATH=$0
SCRIPTNAME=`basename $0`
RMFILENAME=rmlpsstartup

# Shell variables with fixed definitions

ABORT="kill -1 $$"
PRODUCT="PrintServer Software"
VERSION="5.1"

SHELL=/bin/sh				# So SCO doesn't mess up
export SHELL

# Shell variables that can already be defined upon entry

: ${NOCONFIRM:=true}
: ${LPSODB:=/etc/lpsodb}
: ${LPSODBLIST:=/etc/lpsodblist}
: ${PAGER:=more}
: ${TMPDIR:=/tmp}

# Definition to fake out supporting scripts to think we are
# the lpssetup script.

LPSSETUP=$$
export LPSSETUP

#----------------------------------------------------------------------

# Tell the user what this script is used for (if this is Pass #1)

if [ `dirname $SCRIPTPATH` != $TMPDIR ]
then
    echo "$PRODUCT V$VERSION Deinstallation Script"
    echo
    echo "Just one moment..."
fi

#----------------------------------------------------------------------

# Simple debug output function

DebugEcho ()
{
    if [ "$LPSDEBUG" ]
    then
	echo DEBUG: "$*"
    fi

    return 0
}

#----------------------------------------------------------------------

# Command line and environment checks

error=true

if [ -z "$*" ]	     # No command line args allowed
then
    if ls $SCRIPTPATH > /dev/null 2>&1
    then
	error=false
    fi
fi

# Verify the user is currently executing as the superuser.
# This is not very complete test, but it should do.

testfile=/testroot.$$
if touch $testfile > /dev/null 2>&1
then
    rm -f $testfile
else
    error=true
fi

if $error
then
    echo
    echo "usage: $SCRIPTNAME"
    echo
    echo "You must execute this script while in the directory"
    echo "in which the script resides, and you must be running"
    echo "as the 'root' user."
    echo
    exit 1
fi

#----------------------------------------------------------------------

# Setup activities to be executed upon encountering certain signals

trap 'echo ; \
      echo $SCRIPTNAME script terminating... ; \
      echo ; \
      echo $PRODUCT was NOT completely de-installed. ; \
      echo ; \
      rm -f $ENV_LPSTMP/*.$$ ; \
      rm -f $TMPDIR/*.$$ ; \
      rm -f ./*.$$ ; \
      exit 1' \
      1 2 3 15

#----------------------------------------------------------------------

# If this is the first pass at running this script, then copy the
# script to $TMPDIR and re-exec the script from the /tmp copy.

if [ `dirname $SCRIPTPATH` = $TMPDIR ]
then
    DebugEcho "[Now executing $TMPDIR/$SCRIPTNAME]"
else
    DebugEcho "Copying $SCRIPTNAME to $TMPDIR ..."

    if cp $SCRIPTPATH $TMPDIR  &&  chmod a+x $TMPDIR/$SCRIPTNAME
    then
	   cp `dirname $0`/scripts/$RMFILENAME $TMPDIR
	   chmod a+x $TMPDIR/$RMFILENAME
	exec $TMPDIR/$SCRIPTNAME
    else
	$ABORT
    fi
fi

#----------------------------------------------------------------------

# Set all common and kit-specific shell variables

DebugEcho "Setting up the de-installation environment..."

if [ ! -f $LPSODB ]
then
    echo "    The LPS Object Database file could not be found: $LPSODB"
    $ABORT
fi

# Extract the LPSROOT and LPSBIN directory paths from the ENV
# object within the $LPSODB and ensure the path is valid.

ENTRY="`sed -n -e '/^LPSENV|/,/[^\\\\]$/p' < $LPSODB`"
LPSBIN="`expr "$ENTRY" : '.*:lpsbin=\([^:]*\):'`"
LPSROOT="`expr "$ENTRY" : '.*:lpsroot=\([^:]*\):'`"

if [ -z "$ENTRY" -o -z "$LPSBIN" -o -z "$LPSROOT" ]
then
    ERRTEXT="could not be extracted from $LPSODB"

    if [ -z "$ENTRY" ]
    then
	echo "    The ENV entry $ERRTEXT"
    elif [ -z "$LPSROOT" ]
    then
	echo "    The LPSROOT path $ERRTEXT"
    else
	echo "    The LPSBIN path $ERRTEXT"
    fi
    echo "    The LPS Object Database ($LPSODB) appears to be corrupted"
    $ABORT
fi

# Check to see if $LPSBIN is an absolute path; if not, then
# prepend the $LPSROOT definition to derive what should be
# an absolute path.

if [ "`echo $LPSBIN | cut -c1`" != "/" ]
then
    LPSBIN="$LPSROOT/$LPSBIN"
fi

# Prefix our PATH with the LPSBIN directory

PATH="${LPSBIN}:${PATH}"

# Create a short-hand variable definition for the LPS scripts directory

LPSSCRIPTS=$LPSROOT/scripts

# If the current definition of $LPSODBLIST does not represent an
# executable file, then set LPSODBLIST to the fully formed absolute
# path to the utility in the $LPSBIN directory.

if [ ! -x $LPSODBLIST ]
then
    LPSODBLIST="$LPSBIN/lpsodblist"
fi

if [ ! -x "$LPSODBLIST" ]
then
    echo "    The lpsodblist utility could not be found: $LPSODBLIST"
    $ABORT
fi

# Change our working directory over to $LPSROOT, then import the
# standard LPS subscripts and set all kit variables, including
# our PATH.

if cd $LPSROOT  &&  . $LPSSCRIPTS/lpsshfuncs  &&  . $LPSSCRIPTS/setkitvars
then
    :
else
    $ABORT
fi

# Quick definition for obtaining LPSODB info

GETODB="$LPSODBLIST -f $LPSODB"

#----------------------------------------------------------------------

# Special function to support debugging of the actual de-installation
# commands.

export LPSDEBUG

Cmd ()
{
    if [ "$LPSDEBUG" ]
    then
	echo "[Executing command:  $* ]"

###	return 0	# Only enabled during development as necessary!
    fi

    # Execute the command, returning the exit value of that command

    $*
}

#----------------------------------------------------------------------

# Ask the user to confirm the deinstallation

if yesno n "Do you want to deinstall the software on $LOCALHOST"
then
    echo
else
    echo
    exit 0
fi

#----------------------------------------------------------------------

# Import the ENV object attributes, then get the list of all currently
# defined MC/PC objects.

echo "Acquiring the Environment definition..."

if getobject false ENV
then
    :
else
    $ABORT
fi

expobjvars ENV

# Init key identification stuff
#
# Note that the KITCLASS variable is fetched from KITINFO,
# and the X11APPDEFS variable is fetched from the setvars file.

PRODUCT="$ENV_PRODUCT"	# Reset from LPSODB definitions
VERSION="V$ENV_SYSVERSION"

if . $LPSROOT/KITINFO  && . $LPSROOT/scripts/setvars.$KITCLASS
then
    :
else
    $ABORT
fi

echo
echo "Acquiring the list of all defined LPS objects..."

if PSLIST=`$GETODB -c ps` && MCLIST=`$GETODB -c mc` && PCLIST=`$GETODB -c pc` 
then
    :
else
    $ABORT
fi

echo
echo "Ready to deinstall the $PRODUCT on $LOCALHOST"
echo
pause "Press the interrupt key to QUIT, or press RETURN to continue..."
echo

# If this is an OSF/1 system, the formally remove this kit from the
# system's registered installed packages.

if [ "$ENV_OSTYPE" = OSF1 ]
then
    setld -d `getOSFinstdir $ENV_SYSVERSION`
fi

echo
echo "Removing all currently defined LPS objects on $LOCALHOST ..."

#----------------------------------------------------------------------

# rmfile() -- Common function to control deletion of a file
#
# Parameters:
#    $1 - Path of the file (may be null, see below)
#    $2 - Description
#    $3 - Default answer (if null, then user will not be asked)
#
# If the path is null, then this function acts as a silent noop.
#
# Return value is always 0 (success).

rmfile ()
{
    RMFILE="$1"
    DESC="$2"
    DEFANS="$3"

    if [ -z "$DESC" ] ; then DESC=" " ; else DESC="$DESC " ; fi

    if [ "$RMFILE" ]
    then
	if rmfilecheck $RMFILE
	then
	    if [ "$DEFANS" ]
	    then
		if yesno $DEFANS "Delete the $DESC file"
		then
		    :
		else
		    return 0
		fi
	    else
		echo "    Removing ${DESC}file ($RMFILE)"
	    fi

	    Cmd rm -f $RMFILE
	else
	    echo "    [Preserving special $DESC file: $RMFILE]"
	fi
    fi

    return 0
}
#----------------------------------------------------------------------

# Delete all defined Print Client (PC) objects and their associated
# files, particularly the local queuing system components.
#
# If this is a SV3 printing model, then first zap the stopLP.SV3 and
# startLP.SV3 scripts, and if those scripts existed, then manually
# shutdown and restart the printing system.

RESTARTLP=false

TMPFILE=$TMPDIR/LPcontrol.$$

if [ "$PCTYPE" = SV3 ]
then
    if [ -f scripts/stopLP.SV3 -o -f scripts/startLP.SV3 ]
    then
	# To keep the standard LPS queue-related subscripts
	# from constantly starting and stopping the LP spooler,
	# we manually remove the scripts that actually do that
	# work (since other scripts use these scripts iff they
	# exist); then we manually stop/start the LP spooler
	# ourselves.

	rm -f scripts/stopLP.SV3 scripts/startLP.SV3

	if lpstat -r | grep "is not running" > /dev/null 2>&1
	then
	    echo "The LP spooling system is already shutdown."
	else
	    echo "Shutting down the LP spooling system..."

	    if Cmd lpshut > $TMPFILE 2>&1
	    then
		RESTARTLP=true
	    else
		cat $TMPFILE

		cat << END_OF_INPUT
    Sorry, but it appears that the LP spooling system did not stop
    as directed.  Please try to fix this problem and try again.
END_OF_INPUT
		rm -f $TMPFILE
		$ABORT
	    fi
	fi
    fi
fi

# Function to delete all files associated with a PC

rmPC ()
{
    rmfile "$PC_LOGFILE"  log
    rmfile "$PC_ACCTFILE" accounting

    return 0
}

# Now delete all PC queues

for obj in $PCLIST
do
    echo
    echo "--- Removing print client $obj ... "

    if getobject true PC $obj
    then
	expobjvars PC

	if Cmd rmq.$PCTYPE $obj
	then
	    # Remove all files associated with this PC

	    if rmPC
	    then
		:
	    else
		$ABORT
	    fi
	else
	    $ABORT
	fi
    else
	$ABORT
    fi
done

# Restart the LP system IFF we were the ones who stopped it

if $RESTARTLP
then
    echo
    echo "Restarting the LP spooling system..."

    if Cmd lpsched > $TMPFILE 2>&1
    then
	:
    else
	cat $TMPFILE

	cat << END_OF_INPUT

    Sorry, but it appears that the LP spooling system did not
    restart as directed.  Please look into this problem and
    try to get it restarted (using the "lpsched" command) after
    this script completes.
END_OF_INPUT
	pause
    fi
fi

rm -f $TMPFILE

#----------------------------------------------------------------------

# Delete all defined Management Client (MC) objects and their
# associated files.

echo

# Function to delete all files associated with a MC

rmMC ()
{
    rmfile "$MC_LOGFILE"  log
    rmfile "$MC_ACCTFILE" accounting y

    return 0
}

for obj in $MCLIST
do
    echo
    echo "--- Removing management client $obj ... "

    if getobject true MC $obj
    then
	Cmd stopMC $obj
	rmMC
    else
	$ABORT
    fi
done

#----------------------------------------------------------------------

# Delete all defined PrintServer printer (PS) objects and their
# associated files.

echo

# Function to delete all files associated with a PS

rmPS ()
{
    rmfile "$PS_CONFIGFILE"       configuration
    rmfile "$PS_DEFAULTSFILE"     "PostScript defaults"
    rmfile "$PS_PCLDEFAULTSFILE"  "PCL defaults"
    rmfile "$PS_SYSPARAMSFILE"    "system parameters"
    rmfile "$PS_SETUPFILE"        "setup"
    rmfile "$PS_BOOTFILE"         "boot"

    return 0
}

TMPFILE="$TMPDIR/rmPS.$$"

for obj in $PSLIST
do
    echo
    echo "--- Removing PrintServer printer $obj ... "

    if getobject true PS $obj
    then
	PS_OBJNAME=$obj
	expobjvars PS
	rmPS

	# If this is not an LPS40 printer, then send an email message
	# saying that an /etc/bootptab entry is no longer required for
	# this printer and may be removed.

	if [ "$PS_MODEL" != LPS40 ]
	then
	    dobootptab remove $TMPFILE > /dev/null
	fi
    else
	$ABORT
    fi
done

rm -f $TMPFILE

#----------------------------------------------------------------------

# Remove the host-level startup commands

echo
echo "Removing system startup commands..."

$TMPDIR/$RMFILENAME

#----------------------------------------------------------------------

# Remove all files/directories not typically placed in the $LPSROOT
# directory tree.

echo
echo "Removing files installed in non-LPS directories..."

echo "  User programs..."

rmfile $ENV_LPSUSR/lpspr     "print filter program"
rmfile $ENV_LPSUSR/lpsrc     "Remote Console program"
rmfile $ENV_LPSUSR/xlpsprint "X Window Print Tool program"
rmfile $ENV_LPSUSR/xlpsrc    "X Window Remote Console program"


echo "  X11 application default files..."

rmfile $X11APPDEFS/XLpsprint "X Window Print Tool program"
rmfile $X11APPDEFS/XLpsrc    "X Window Remote Console program"


echo "  Boot files..."

rmfile $ENV_LPSBOOT/lps.sys   "PrintServer 20/32 boot"
rmfile $ENV_LPSBOOT/lps17.sys "PrintServer 17 boot"


echo "  Copies of files in /etc ..."

rmfile /etc/lpsodblist "LPS object database lister program"
rmfile /etc/lpssetup   "LPS setup script"


# Online manual pages

installman -d		# Don't care about failure here

#----------------------------------------------------------------------

# If any supporting daemons are left hanging around,
# then kill them off now.

if [ -f $ENV_LPSROOT/lpsbootd.pid ]
then
    pid=`cat $ENV_LPSROOT/lpsbootd.pid`
    if [ "$pid" ]
    then
	if kill -0 $pid  &&  kill -2 $pid
	then
	    echo
	    echo "Terminated the running lpsbootd daemon."
	fi
    fi
fi

#----------------------------------------------------------------------

# Delete the $LPSROOT tree

echo
echo "Removing the LPS directory tree rooted at $LPSROOT ..."

cd $TMPDIR

Cmd rm -rf $LPSROOT

#----------------------------------------------------------------------

# Delete the LPSODB file and any other remaining LPS software components

echo
echo "Removing the LPS Object Database ($LPSODB) ..."

Cmd rm -f $LPSODB
Cmd rm -f ${LPSODB}.[0-9]

echo "Removing the LPS Object Database lister program ..."

Cmd rm -f /etc/lpsodblist

#----------------------------------------------------------------------

# Delete $TMPDIR copy of the script as the VERY LAST step

echo
echo "De-installation of $PRODUCT is complete."
echo

rm $TMPDIR/$SCRIPTNAME	   # An error here shouldn't matter at this point
rm $TMPDIR/$RMFILENAME	   # An error here shouldn't matter at this point

exit 0

#
# Local Variables:
# fill-column:79
# page-delimiter:"^#---"
# End:
