#! /bin/sh
#
# @(#)getPScfgvars	1.6	LPS_UNX_COM	2/19/95
#
# Copyright 1994   Digital Equipment Corporation, Maynard, MA
#
# getPScgfvars
#
# Fetch all PS object variables associated with the given printer
# configuration file.  The file is parsed and an "import" script is
# generated that assigns the values to the associated object variables.
#
# Parameters:
#    $1 - Path of the configuration file.
#    $2 - Path of the file to receive the final Bourne shell commands.
#
# If the configuration file does not exist, then the generated script
# is constructed such that all typical PS object variables are defined
# to have null values (or point to empty table files), and the exit value
# is 0 (success).
#
# This script expects to operate in the LPSROOT directory and that the
# scripts directory is immediately below LPSROOT, as a special awk script
# file needed here resides in LPSROOT/scripts.
#
# Global variables:
#    TMPDIR
#
# Exit values:
#    0 - Success, all variables have been acquired and written out.
#    1 - Error of some kind, error messages go to stderr, contents of
#        the output file are undefined.
###

CFGFILE=$1
OUTFILE=$2

: ${TMPDIR:=/tmp}

# Paths of related/working files

COMMFILE=$TMPDIR/commfile.$$	# SNMP communities table
TRAPFILE=$TMPDIR/trapfile.$$	# SNMP traps table

AWKSCRIPT=scripts/parsecfg.awk

# General error msg function

Error ()
{
    echo
    echo "    $*" 1>&2
    echo
    pause

    return 0
}

# Fatal error function -- display msg on stderr and exit with 1

Fatal ()
{
    Error "$*"
    exit 1
}

# Command line validation

if [ -z "$CFGFILE" ]
then
    Fatal "No configuration file specified!"
elif [ -z "$OUTFILE" ]
then
    Fatal "No shell variable output file specified!"
fi

# Function used to create the final output file
# containing the definitions of all PS_XXX variables.

mkoutfile ()
{
    if cat << ENDOFINPUT > $OUTFILE
PS_ACCOUNTING="$PS_ACCOUNTING"
PS_ALLOWDN="$PS_ALLOWDN"
PS_ALLOWIP="$PS_ALLOWIP"
PS_DENYDN="$PS_DENYDN"
PS_DENYIP="$PS_DENYIP"
PS_GATEWAY="$PS_GATEWAY"
PS_LANGUAGE="$PS_LANGUAGE"
PS_LOGGING="$PS_LOGGING"
PS_PASSWORD="$PS_PASSWORD"
PS_PRINTERNAME="$PS_PRINTERNAME"
PS_SUBNETMASK="$PS_SUBNETMASK"

PS_ENABLESNMP="$PS_ENABLESNMP"
PS_SYSLOCATION="$PS_SYSLOCATION"
PS_SYSCONTACT="$PS_SYSCONTACT"
PS_ENABLETRAPS="$PS_ENABLETRAPS"

PS_COMMUNITIES="$PS_COMMUNITIES"
PS_TRAPS="$PS_TRAPS"

ENDOFINPUT
    then
	return 0
    else
	return 1
    fi
}

# Set an important state variable based on whether the
# configuration file exists or not.

if [ -f "$CFGFILE" ]		# If the config file exists
then
    CFGEXISTS=true
else
    CFGEXISTS=false

    Error "Configuration file \"$CFGFILE\" does not exist!"

    Error "You should delete this printer definition and recreate it"
fi

# Create empty copies of the separate COMMUNITIES and TRAPS files

rm -f $COMMFILE $TRAPFILE

if touch $COMMFILE $TRAPFILE
then
    :
else
    Fatal "Unable to initialize all temporary working configuration files"
fi

# If the configuration file exists, then munge it using the special
# awk script.  What the awk script does is output fully formed
# shell variable settings for all PS_XXX variables, as well as copy
# all community and trap specs into their respective files.

if $CFGEXISTS		# If the config file exists
then
    if eval `awk -f $AWKSCRIPT cfile=$COMMFILE tfile=$TRAPFILE $CFGFILE`
    then
	:
    else
	Fatal "Unable to create shell script of configuration variables"
    fi
else
    # If no configuration file exists, then leave all PS_XXXX variables
    # intact, except handle those variables that contain paths to
    # the files that contain the actual configuration information.

    PS_COMMUNITIES="$COMMFILE"
    PS_TRAPS="$TRAPFILE"

fi

# Create the output file containing the shell variable definitions,
# even if they are all set to null (due to a missing config file).

if mkoutfile
then
    :
else
    Fatal "Unable to create shell script of configuration variables"
fi

# If we get here, then we've enjoyed success

exit 0
