#!/bin/csh -f

# pepsy
#
set pepsy = "/cs/research/icm/other/kinou/isode-8.0/pepsy -A -h -m"

if (!($?OSIMISETCPATH)) then
    echo "set environment variable OSIMISETCPATH then try again"
    exit 1
endif

if ($#argv != 2) then
    echo "usage: asn-cmpl <ASN-file> <Attr-file>"
    exit 1
endif

set asnFile  = $1
set attrFile = "attr.gen"

# get name of ASN module from ASN.1 file

set asnModule = `gawk '{if ($1) {if (substr($1, 0, 2) == "--") {next;} else {print $1; exit;}}}' < $asnFile`

if (! $?asnModule) then
    echo "could not find the name of the ASN.1 Module in $asnFile"
    exit 1
endif

echo ""
echo "generating C structures and tables from ASN.1 module $asnModule"
echo ""

# remove any existing PREFIXES and SECTIONS clauses from the asnFile -
# add "PREFIXES build parse print"

sed -f $OSIMISETCPATH/gdmodir/prefixes.sed $asnFile > $asnFile.tmp

# call pepsy

if (! { $pepsy $asnFile.tmp }) then
    echo "pepsy failed - check ASN.1 file $asnFile"
    rm $asnFile.tmp
    exit 1
endif

# create attr.gen file by prepending $2 file with the ASN.1 module name
echo $asnModule > $attrFile
cat $2 >> $attrFile

echo "using $OSIMISETCPATH/gdmodir/attrtbl.awk"
gawk -f $OSIMISETCPATH/gdmodir/attrtbl.awk $attrFile > asn-cmpl.log
echo "using $OSIMISETCPATH/gdmodir/attrgen.awk"
gawk -f $OSIMISETCPATH/gdmodir/attrgen.awk asn-cmpl.log
rm $asnFile.tmp $attrFile
# rm asn-cmpl.log

