#! /bin/sh
#
# mklpsodb -- Create a new LPS Object Database and the Environment definition
#
# @(#)mklpsodb	1.5	LPS_UNX_COM	02/19/95
#
# Copyright 1995   Digital Equipment Corporation, Maynard, MA
#
# DESCRIPTION:
#
# A shell script to create the LPS Object Database file from the current
# ENV object variable values.
###

LPSODB=$1	# Path of the LPS Object Database file
OFILE=$2	# Path of a temp file to use during construction
OLDDEFS=$3	# [Optional] path of old definitions to append to LPSODB

ENVDEF=$TMPDIR/envdef.$$   # The ENV definition gets created in here

. $FUNCS	# Import the standard LPS shell functions
expobjvars ENV	# Export all ENV variables

oksofar=true	# Ongoing flag to detect errors and control activities

# Create the basic ENV definition in a file, then append that file
# to a header and put all of it into another file that will represent
# the first version of the LPSODB.

if mkodbdef ENV Alist.ENV $ENVDEF  &&  cat > $OFILE <<END_OF_INPUT
#
# LPS Object Database for $PRODUCT
#
# Created on $LOCALHOST on `date`
#
#     --- DO NOT EDIT THIS FILE ---
#
# To perform major modifications to this file, use the "lpssetup" script
# located in the directory defined by the "lpsroot" variable in the
# "LPSENV" entry below.
#
LPSENV|LPS Environment:\\
	:class=ENV:\\
`cat $ENVDEF`
END_OF_INPUT
then
    # If no error has been encountered, then append the optional
    # definitions to the end of the working file now.

    if [ "$OLDDEFS" ]
    then
	if cat $OLDDEFS >> $OFILE
	then
	    :
	else
	    oksofar=false
	fi
    fi
else
    oksofar=false
fi

# If everything is hunky-dory at this point, then we're ready
# to copy the working file to the final, official destination,
# ensuring that it has the proper ownership and protection.

if $oksofar
then
    if cp $OFILE $LPSODB
    then
	if chmod 644 $LPSODB
	then
	    if rm $OFILE
	    then
		:
	    else
		# Consider this error to be a non-fatal warning,
		# as the system manager can worry about this later,
		# and no security problem is evident.

		echo "Unable to remove working file ($OFILE)"
	    fi
	else
	    echo
	    echo "    Unable to set the proper protection for $LPSODB"
	    oksofar=false
	fi
    else
	echo
	echo "    Unable to copy working file ($OFILE) to $LPSODB"
	oksofar=false
    fi
fi

if $oksofar
then
    exit 0
else
    $PAGER <<END_OF_INPUT

    A problem has been encountered while attempting to create the
    LPS Object Database file ($LPSODB).

    Please try to solve this problem, then re-run this procedure.
    In the meantime, a copy of the database containing all values
    defined during this session will be stored in $OFILE.
    If you copy this file to $LPSODB and set its protection (mode)
    to 644, then you should not have to re-run this procedure.
END_OF_INPUT
	exit 1
fi
