#! /bin/sh
#
# @(#)mkodbdef	1.14	LPS_UNX_COM	02/19/95
#
# Copyright 1995   Digital Equipment Corporation, Maynard, MA
#
# mkodbdef
#
# Common script to create the class-specific components of an LPSODB
# object definition.
#
# It is expected that the associated class variables have been exported
# to this process.
#
# Parameters:
#    $1 - Class identifier, one of {PS, MC, PC, ENV}
#    $2 - Path of the attribute list file
#    $3 - Path of the output file that contains the fully formed components
#
# Note that this script does not currently support the ENV class.
#
# Global variables:
#    TR
#    OSTYPE
#    All class variables for the specified class (eg: PC_*)
#
# Exit values:
#    0 - Success, the definition has been stored in the specified file.
#    1 - Some sort of error occurred, existence/contents of the output
#	 file are undefined.

CLASS=$1
ATTRLIST=$2
OUTFILE=$3

: ${SILENT:=false}

if $SILENT
then
    :
else
    echo
    echo "    [Creating the `getobjdesc $CLASS` object definition]"
fi

rm -f $OUTFILE

# Get the attribute list as a sequence of words in "LIST", then count
# and save the number of attributes, and initialize the "i" attribute
# counter.
#
# Note that the attribute list is sorted.  This is *not* a requirement
# for the LPSODB, but makes it easier to read the file later.

LIST="`sort $ATTRLIST`"
CNT="`echo "$LIST" | wc -l`"
i=1

# Set the LASTUPDATED attribute value to the current date/time
# using the proper format.  Note CAREFULLY that we have to do
# some pretty silly variable handling in order to keep SCCS
# from "helping us out" by substituting the current date for
# the character sequence "percent-M-percent"...

p1="+19%y%m%d%H"
p2="%M"
eval ${CLASS}_LASTUPDATED=`date ${p1}${p2}%S`

# For each attribute, resolve the associated class variable, then resolve
# that variable's value.  Then create a lowercase version of the attribute
# name (required for the LPSODB definition), and create the attribute/value
# definition line; for all attributes EXCEPT the last one, append the
# required "\" line-continuation character.  Finally, the generated line
# is appended to the output file.

for ATTR in $LIST
do
    eval VAR="${CLASS}_$ATTR"
    eval VAL="\$$VAR"

    if [ "$OSTYPE" = "OSF1" -o "$OSTYPE" = "BSD" ]
    then
	ATTR="`echo $ATTR | $TR 'A-Z' 'a-z'`"       # Make ATTR lowercase
    else
	ATTR="`echo $ATTR | $TR '[A-Z]' '[a-z]'`"   # Make ATTR lowercase
    fi

    LINE="	:${ATTR}=${VAL}:"   # Contains a leading TAB character!!!

    # Output the line, but if this is not the last line in the list, then
    # append "\\" to the string (to continue the logical line).  Then
    # increment the list counter so we know when to stop adding this.
    #
    # NOTE:  The following code is not quite what you'd expect, as it is
    #        structured specifically to get around a bug in Pyramid OSx
    #        where the shell will HANG if a shell variable with a "\\"
    #        on the end is echo'd...

    if [ $i -ne $CNT ]
    then
	echo "${LINE}\\" >> $OUTFILE
    else
	echo "${LINE}"   >> $OUTFILE	# Only for the last line!
    fi

    if [ $? -eq 0 ]			# The echo worked?
    then
	i="`expr $i + 1`"		# Yes
    else
	exit 1
    fi
done

exit 0
