: $Workfile:   kixcob.sh  $ $Revision:   1.0  $
#**********************************************************************#
#*                                                                    *#
#* Copyright (c) 2001 by Sun Microsystems, Inc.                       *#
#* All rights reserved.                                               *#
#*                                                                    *#
#**********************************************************************#


# kixcob
# call cobol compiler
# params: y	Terminal device	used
#	  9	temporary output listing file name
#	  o	output file name (.int/.gnt)
#	  VAXCLONEintut7	miscellaneous cobol options
#	  <filename> file name to process

COBOPT1="-v"
COBOPT2="ibmcomp perform-type=osvs "

OUTFORM=INT
FOUTPUT=OFF

set -- `getopt WVAXCLONEinTo:ut7y:9:c: $*`

# while	options	to process
while test "$1"	!= "--"
do
    case $1 in
    -y)
	shift
	TTYNAME=$1
	;;
    -c)
	shift
	if test "$COBCPY" = ""
	then
	   COBCPY=$1
        else
	   COBCPY="$1:$COBCPY"
        fi
        ;;
    -9)
	shift
	FOUTNAME=$1
	FOUTPUT=YES
	;;
    -o)
	shift
	NEWFILE=$1
	;;
    -C)
	COBOPT2="$COBOPT2 cics"
	;;
    -L)
	COBOPT2="$COBOPT2 list"
	;;
    -n)
	COBOPT2="$COBOPT2 nobound"
	;;
    -O)
	COBOPT2="$COBOPT2 cicsopt"
	;;
    -N)
	COBOPT2="$COBOPT2 noalter"
	;;
    -7)
	COBOPT2="$COBOPT2 noans85"
	;;
    -A)
	COBOPT1="$COBOPT1 -a"
	;;
    -E)
	COBOPT2="$COBOPT2 errlist"
	;;
    -i)
	OUTFORM=INT
	COBOPT1="$COBOPT1 -i"
	;;
    -u)
	OUTFORM=GNT
	COBOPT1="$COBOPT1 -u"
	;;
    -T)
	COBOPT2="$COBOPT2 trunc"
	;;
    -t)
	COBOPT2="$COBOPT2 notrunc"
	;;
    -X)
	COBOPT2="$COBOPT2 xref"
	;;
    -V)
	COBOPT2="$COBOPT2 vsc2"
	;;
    -W)
	COBOPT2="$COBOPT2 osvs"
	;;
    *)
	exit 1
	# illegal option encountered
	;;
    esac
    shift
done
export COBCPY

EXT=`echo $2 | sed -e "s/.*\.//"`
if test $EXT = "cbl"
then
    COBOPT2="$COBOPT2 defaultbyte=32 "
else
    COBOPT2="$COBOPT2 defaultbyte=0 "
fi

if test "$FOUTNAME" != ""
then
    trap "/bin/rm -f $FOUTNAME*; exit 1"  1 2 3
fi
# echo "Processing $2, please wait..."


if test	$OUTFORM = "GNT"
then
    CBLFILE="`dirname $NEWFILE`/`basename $NEWFILE .gnt`.cbl"
elif test $OUTFORM = "INT"
then
    CBLFILE="`dirname $NEWFILE`/`basename $NEWFILE .int`.cbl"
fi

# if this is a clt first we will delete the cbl

if test $EXT != "cbl"
then
   rm -f $CBLFILE
fi

rm -f $NEWFILE

if test $OUTFORM = "INT"
then
    rm -f  "`dirname $NEWFILE`/`basename $NEWFILE .int`.idy"
    rm -f  "`dirname $NEWFILE`/`basename $NEWFILE .int`.lst"
    rm -f  "`dirname $NEWFILE`/`basename $NEWFILE .int`.gnt"
elif test $OUTFORM = "GNT"
then
    rm -f  "`dirname $NEWFILE`/`basename $NEWFILE .gnt`.idy"
    rm -f  "`dirname $NEWFILE`/`basename $NEWFILE .gnt`.int"
    rm -f  "`dirname $NEWFILE`/`basename $NEWFILE .gnt`.lst"
fi

cd `dirname $CBLFILE`
if test	$FOUTPUT = "YES"
then
    echo "####	kixcob output  ####" >$FOUTNAME
    if test $EXT != "cbl"
    then
        $UNIKIX/bin/kixclt -o $CBLFILE $2 1>>$FOUTNAME.1 2>>$FOUTNAME.2
    fi
    if [ $? -eq 1 -a $EXT != "cbl" ]
    then
        cat	$FOUTNAME.1 >> $FOUTNAME
        cat	$FOUTNAME.2 >> $FOUTNAME
	CLTFAIL=YES
        echo "CLT error " >>$FOUTNAME
    else
        cob	$COBOPT1 -C "$COBOPT2" `basename $CBLFILE`  1>>$FOUTNAME.3 2>>$FOUTNAME.4
        cat	$FOUTNAME.1 >> $FOUTNAME
        cat	$FOUTNAME.2 >> $FOUTNAME
        cat	$FOUTNAME.4 >> $FOUTNAME
        cat	$FOUTNAME.3 >> $FOUTNAME
    fi
    if test "$FOUTNAME" != ""
    then
        /bin/rm -f $FOUTNAME.*
    fi
else
    $UNIKIX/bin/kixclt -o $CBLFILE $2 1>/dev/null 2>/dev/null
    if test $? -eq 1
    then
	echo "CLT error"
	CLTFAIL=YES
    else
        cob	$COBOPT1 -C "$COBOPT2" `basename $CBLFILE` 1>/dev/null 2>/dev/null
    fi
fi

# Micro	Focus generates	.int and .gnt files in the current directory
# we must move them to where the user requested

if test "$CLTFAIL" != "YES"
then
    if test	`dirname $NEWFILE` != `pwd`
    then
        mv -f `basename $NEWFILE` $NEWFILE
        if test $OUTFORM = "INT"
        then
	    mv -f "`basename $NEWFILE .int`.idy" \
	        "`dirname $NEWFILE`/`basename $NEWFILE .int`.idy"
        fi
    fi
fi
# vi $FOUTNAME <$TTYNAME >$TTYNAME
# if test "$FOUTNAME" != ""
# then
#     /bin/rm	-f $FOUTNAME
# fi
exit 0
