#!/bin/sh
#
# lpssetup -- Master Script for PrintServer Management Functions
#
# @(#)lpssetup	1.11	LPS_UNX_COM	2/19/95
#
# Copyright 1994   Digital Equipment Corporation, Maynard, MA
#
# DESCRIPTION:
#
# This script is used to control the operation of all LPS support scripts
# (called "subscripts") used to configure and otherwise define the
# operation of the various PrintServer objects.
#
# The current process must be "root" to execute this script.
#
# -------------------------------------------------------------------------

# Initialize those variables that are very specific to this script

: ${LPSODB:="/etc/lpsodb"}		# Default LPS Object Database file

FUNCS=scripts/lpsshfuncs
LPSSETUP="$$"				# For process validation by subscripts
SUBSCRIPTS="lpsmps lpsmmc lpsmpc setkitvars lpsshfuncs"	# Set of req. scripts
TITLE="PrintServer System Setup Facilities" # Formal base title of this script
TMPDIR="/tmp"

REINSTALLMSG="
    Sorry, but it may be necessary for you to reinstall the software
    kit to ensure proper operation on your system."

# Initialize other critical global variables that will possibly change
# during execution of this script, but are otherwise needed immediately.

PRODUCT="PrintServer Software Kit"

if [ "$1" = "-V" ]
then
    VALIDATE=true
else
    VALIDATE=false
fi

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

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

# This is the actual top of the script as "seen" by the user.

echo
echo "$TITLE  (lpssetup)"
echo
echo "One moment please..."

if $VALIDATE
then
    echo
    echo [Validating software installation...]
    echo
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
    cat << END_OF_INPUT

    To run this script you must be running as the super-user
    (the "root" account), and the PrintServer software kit
    must be properly installed on your system.

END_OF_INPUT
    exit 1
fi

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

# Ensure the LPS Object Database file exists, then extract all critical
# Environment attributes necessary for basic operation.

if [ ! -f $LPSODB ]
then
	cat << END_OF_INPUT

    The LPS Object Database ($LPSODB) does not exist.

    You must first install the $PRODUCT
    using the "lpsinstall" script before you can setup any
    PrintServer components on your system.

END_OF_INPUT
    exit 1
fi

# Extract the LPSROOT and LPSBIN attributes of the LPSENV
# object, and ensure the values are not null.

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

if [ -z "$ENTRY" -o -z "$ENV_LPSROOT" -o -z "$ENV_LPSBIN" ]
then
    if [ -z "$ENTRY" ]
    then
	echo "The \"LPSENV\" entry could not be extracted"
    elif [ -z "$ENV_LPSROOT" ]
    then
	echo "The \"lpsroot\" variable could not be extracted"
    else
	echo "The \"lpsbin\" variable could not be extracted"
    fi
    cat << END_OF_INPUT

    The LPS Object Database ($LPSODB) exists, but appears
    to be corrupted in some way.
    $REINSTALLMSG

END_OF_INPUT
    exit 1
fi

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

# Perform software kit integrity checking...but only if the flag is set.
#
# Ensure that the kit has been installed, then change over to the
# LPSROOT directory.  The verification process involves checking for
# the existence and accessibility of a number of files and directories.
# If any test fails, then it is possible that the kit may have to be
# reinstalled.

# Ensure the LPSROOT directory exists

if [ ! -d "$ENV_LPSROOT" ]
then
    cat << END_OF_INPUT

    The LPS Object Database ($LPSODB) exists, but the critical
    root directory ($ENV_LPSROOT) does not exist.
    $REINSTALLMSG

END_OF_INPUT
    exit 1
fi

# Now change over to the LPSROOT directory

if cd $ENV_LPSROOT
then
    :
else
    echo
    echo "    Unable to change working directory to $ENV_LPSROOT !!"
    echo
    exit 1
fi


if $VALIDATE
then
    # Ensure key support scripts exists

    problem=false

    for f in $SUBSCRIPTS
    do
	f=$ENV_LPSROOT/scripts/$f
	if [ ! -f $f ]
	then
	    if $problem ; then : ; else echo ; fi
	    echo "    Unable to find script file ($f)"
	    problem=true
	fi
    done

    if $problem
    then
	cat << END_OF_INPUT

    One or more of the required LPS shell script files could not be found.
    $REINSTALLMSG
END_OF_INPUT
	exit 1
    fi

fi	# End of $VALIDATE section

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

# Import the common LPS shell functions, then define and export most common
# and platform-specific variables.
#
# Note that some of the platform-specific stuff is set after we
# have loaded the LPS Environment object in just a little bit.

. $FUNCS

. scripts/setkitvars

export LPSSETUP

# Ensure the special LPS installation flag file exists

if [ ! -f "$INSTFLAG" ]
then
    cat << END_OF_INPUT

    The LPS Object Database ($LPSODB) exists and the critical
    root directory ($ENV_LPSROOT) exists, but the
    special installation flag file could not be found.  This
    could mean your installation of the $PRODUCT
    did not succeed.
    $REINSTALLMSG

END_OF_INPUT
    exit 1
fi

# Ensure the LPS Object Database list utility exists

if [ -z "$LPSDEBUG" ]  # If not debugging, set LPSODBLIST to full path spec
then
    LPSODBLIST=$ENV_LPSBIN/lpsodblist
fi

if [ ! -x $LPSODBLIST -a -z "$LPSDEBUG" ]
then
    cat << END_OF_INPUT

    The LPS Object Database list utility ($LPSODBLIST)
    could not be found.
    $REINSTALLMSG

END_OF_INPUT
    exit 1
fi

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

# To make it easier to centralize the eight bazillion temporary files
# created by the subscripts, attempt to create a directory within the
# currently defined TMPDIR, save the current definition in OLDTMPDIR,
# then create a subdirectory and redefine TMPDIR to point to that new
# directory.  When we quit, we will delete the entire subdirectory in
# one fell swoop...and keep the system manager happy.

OLDTMPDIR=$TMPDIR
NEWTMPDIR=$TMPDIR/lpssetup.$$

if [ ! -f $NEWTMPDIR -a ! -d $NEWTMPDIR ]
then
    if mkdir $NEWTMPDIR > /dev/null 2>&1
    then
	TMPDIR=$NEWTMPDIR
	NEWTMPDIR=""
    fi
fi

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

# Define a "quit" function to perform cleanup activities before exiting.
# The only parameter is an integer exit code.  This function should be
# called whenever this script needs to exit after this point.

quit ()
{
    rm -rf /tmp/*.$$
    rm -f ./*.$$

    echo

    exit $1
}

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

# Redefine our PATH to ensure the LPS executables are searched first,
# then import all LPS Environment configuration variables.
#
# Once the Environment variables are loaded, reset any platform-specific
# variables that should be defined based on the current LPSODB.

PATH="$ENV_LPSBIN:$PATH"

if getobject false ENV
then
    PRODUCT="$ENV_PRODUCT"
else
    quit 1
fi

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

# Ensure that all subscripts are set for execute access.
#
# Due the sometimes long amount of time to perform this,
# we only do it if validation is enabled.

if $VALIDATE
then
    chmod a+x ./scripts/*
fi

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

# Ask if verbose help information should be displayed when possible.

if yesno y "Do you want extra help information displayed"
then
    HELP=true
else
    HELP=false
fi

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

# Trap all keyboard-related signals so that the subscripts operating
# in subshells can accurately determine flow control for signals.

trap : 1 2 3 15

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

# Sit in an infinite loop, requesting a menu number and dispatching to
# the corresponding subscript.

while true
do
    clear
    echo "
$TITLE Main Menu

    1) Manage PrintServer Printers
    2) Manage Management Clients
    3) Manage Print Clients
    4) Exit
"
    while true
    do
	$ECHON "Enter a menu number from the above list [4]: "
	read function
	case ${function:-4} in
	    1) SCRIPT=lpsmps ; break ;;
	    2) SCRIPT=lpsmmc ; break ;;
	    3) SCRIPT=lpsmpc ; break ;;
	    4) quit 0 ;;       # Only "successful" exit point
	    *) continue ;;
	esac
    done

    $SCRIPT	# Invoke the chosen script
done
#
# Local Variables:
# page-delimiter:"^#---"
# fill-column:75
# End:
