#!/bin/sh
FITMK_ALCICP="-I/alliance/archi/emx/include"
FITMK_ALCLBP="-L/alliance/archi/emx/lib"
FITMK_ALCGGN="-lGrf604 -lGbs201 -lGga301"
FITMK_ALCAPR="-lApr102"
FITMK_ALCMGN="-lMgn322"
FITMK_ALCMLO="-lMlu413 -lMlo404 -lMal404 -lMcl408 -lMel406 -lMhl403 -lMsl611 -lMvl408"
FITMK_ALCMPH="-lMpu403 -lMph402 -lMap405 -lMcp402 -lMmg402 -lMut316"
FITMK_ALCICN="-lIcu201 -lIcc201 -lIca201 -lIcn201 -lm"
FITMK_FITLDP="-LD:\USR\JAESCHKE/dev/lib"
FITMK_FITLMP="-LD:\USR\JAESCHKE/labo/emx/lib"
FITMK_FITLLS="-lFgn105 -lGfp105"
FITMK_FITIDP="-ID:\USR\JAESCHKE/dev/include"
FITMK_FITIMP="-ID:\USR\JAESCHKE/labo/emx/include"
          CC="gcc -m486"
          RM="rm"
        ECHO="echo"


## ************************** Print FpGen Banner **************************


echo ""
echo "             @@@@@@@@@                @@@@ @                        "
echo "               @@    @              @@    @@                        "
echo "               @@     @            @@      @                        "
echo "               @@       @@@ @@@   @@             @@@@@   @@@ @@@    "
echo "               @@   @    @@@  @@  @@            @     @   @@@   @   "
echo "               @@@@@@    @@    @@ @@     @@@@@ @@     @@  @@    @@  "
echo "               @@   @    @@    @@ @@     @ @@  @@@@@@@@@  @@    @@  "
echo "               @@        @@    @@ @@    @  @@  @@         @@    @@  "
echo "               @@        @@    @@  @@      @@  @@      @  @@    @@  "
echo "               @@        @@@  @@    @@    @@    @@    @@  @@    @@  "
echo "             @@@@@@      @@ @@@       @@@@        @@@@   @@@@  @@@@ "
echo "                         @@                                         "
echo "                        @@@@                                        "
echo ""
echo "                           Fitpath Generation Language              "
echo ""
echo "                    Alliance CAD System 3.0,      fpgen 1.05        "
echo "                    Copyright (c) 93-95, MASI, CAO-VLSI Team        "
echo "                    E-mail support:     cao-vlsi@masi.ibp.fr        "
echo ""
echo ""


## ********************* Read Command Line Arguments **********************


#
# If no arguments, print usage.
#
if [ $# -eq 0 ]; then 
	$ECHO -n "usage: fpgen [-v] [--keep-exec] [--no-exec] [--no-rm-core] "
	$ECHO    "<progam> [-e [args]]"
	exit 0
fi


#
# Defaults settings.
#
       NAME=""
       ROOT="fpgen_out"
      CLEAN="y"
     RMCORE="n"
    VERBOSE="n"
    EXECUTE="y"
  ARGUMENTS=""
INCLUDE_DEV=""
INCLUDE_LAB=""
LIBRARY_DEV=""
LIBRARY_LAB=""
#
# Process command line arguments.
#
while [ $# -gt 0 ]
do
	case $1 in
	-e) shift; ARGUMENTS=$*; break;;
	-v)          VERBOSE="y";;
#	--no-rm-core) RMCORE="n";;
	--keep-exec)   CLEAN="n";;
	--no-exec)     CLEAN="n";
				 EXECUTE="n";;
	--dev)   INCLUDE_DEV=$FITMK_FITIDP;
			 LIBRARY_DEV=$FITMK_FITLDP;;
	--lab)   INCLUDE_LAB=$FITMK_FITIDP;
			 LIBRARY_LAB=$FITMK_FITLDP;;
	*)       if [ -z "$NAME" ]; then
				 NAME=$1;
			 else
				 $ECHO -n "fpgen:*** warning ***:"
				 $ECHO    "Invalid argument \"$1\" ignored.";
			 fi;;
	esac
	shift
done


#
# Check arguments coherency.
#
if [ -z "$NAME" ]; then
	echo "fpgen:*** Error ***:Missing <program> argument."
	exit 1;
fi
ROOT=`basename $NAME .c`
if [ -n "$LIBRARY_DEV" -a -n "$LIBRARY_LAB" ]; then
	echo "fpgen:*** Error ***:--dev and --lab are exclusive options."
	exit 1;
fi


#
# Removing previously generated files.
#
$RM -f $ROOT.frr $ROOT.o > nul 2>&1


#
# Sets libraries and includes.
# *_DEV and *_LAB are for debug purpose.
#
FITMK_FPGICP="$INCLUDE_DEV $INCLUDE_MAC $FITMK_ALCICP"
FITMK_FPGLBP="$LIBRARY_DEV $LIBRARY_MAC $FITMK_ALCLBP"
#
# Echo libraries and includes, when in debug mode.
#
if [ -n "$LIBRARY_DEV" -o -n "$LIBRARY_LAB" ]; then
	echo "fpgen:*** debug ***:INCLUDE:"             | tee -a $ROOT.frr
	echo $FITMK_FPGICP |                          \
		awk 'BEGIN { FS=" "; }                    \
				   { for( i=1; i<=NF; i++)        \
						printf("      %s\n", $i); \
				   }' -                             | tee -a $ROOT.frr
	echo "fpgen:*** debug ***:LIBRARY:"             | tee -a $ROOT.frr
	echo $FITMK_FPGLBP |                          \
		awk 'BEGIN { FS=" "; }                    \
				   { for( i=1; i<=NF; i++)        \
						printf("      %s\n", $i); \
				   }' -                             | tee -a $ROOT.frr
	echo "" | tee -a $ROOT.frr
	echo "" | tee -a $ROOT.frr
fi


## ****************** Printing Current MBK environment ********************


if [ "$VERBOSE" = "y" ]; then
	 echo    "fpgen:Current MBK environment:"               | tee -a $ROOT.frr
	 echo    " MBK_IN_LO      : ${MBK_IN_LO-not set}"       | tee -a $ROOT.frr
	 echo    " MBK_OUT_LO     : ${MBK_OUT_LO-not set}"      | tee -a $ROOT.frr
	 echo    " MBK_IN_PH      : ${MBK_IN_PH-not set}"       | tee -a $ROOT.frr
	 echo    " MBK_OUT_PH     : ${MBK_OUT_PH-not set}"      | tee -a $ROOT.frr
	 echo    " MBK_WORK_LIB   : ${MBK_WORK_LIB-not set}"    | tee -a $ROOT.frr
	$ECHO -n " MBK_CATA_LIB   : "                           | tee -a $ROOT.frr
	 if [ -z "$MBK_CATA_LIB" ]; then
		 echo "not set"                                     | tee -a $ROOT.frr
	 else
		 echo ${MBK_CATA_LIB} |                                    \
			 awk 'BEGIN { FS=":"; }                                \
					    { for( i=1; i<=NF; i++)                    \
						  {                                        \
							 printf("%s\n", $i);                   \
								 if (i < NF)                       \
									 printf("                  "); \
						   }                                       \
					    }' -                                | tee -a $ROOT.frr
	 fi
	 echo    " MBK_CATAL_NAME : ${MBK_CATAL_NAME-not set}"  | tee -a $ROOT.frr
	 echo    " MBK_VDD        : ${MBK_VDD-not set}"         | tee -a $ROOT.frr
	 echo    " MBK_VSS        : ${MBK_VSS-not set}"         | tee -a $ROOT.frr
	 echo    ""                                             | tee -a $ROOT.frr
	 echo    "RDS_TECHNO_NAME : ${RDS_TECHNO_NAME-not set}" | tee -a $ROOT.frr
	 echo    ""                                             | tee -a $ROOT.frr
	 echo    ""                                             | tee -a $ROOT.frr
	 echo    "fpgen:FpGen Macro-Functions library:"         | tee -a $ROOT.frr
	 echo    "    FPGEN_LIB   : ${FPGEN_LIB-not set}"       | tee -a $ROOT.frr
	 echo    ""                                             | tee -a $ROOT.frr
	 echo    ""                                             | tee -a $ROOT.frr
fi


#
# Adding FPGEN_LIB to MBK_CATA_LIB.
# (needed for hierarchical analisys functions)
#
MBK_CATA_LIB=$MBK_CATA_LIB:$FPGEN_LIB


## ********************** Compilation And Execution ***********************


#
# Compiling program.
#
if [ "$VERBOSE" = "y" ]; then
	$ECHO -n "            " | tee -a $ROOT.frr
	$ECHO -n "Compiling $NAME.c:" | tee -a $ROOT.frr
fi;
$CC   $FITMK_FPGICP -c -g -o $ROOT.o $NAME.c 2>>$ROOT.cc.log 1>&2
EXIT_CODE=$?
if [ $EXIT_CODE -ne 0 ]; then
	$RM -f $ROOT.o > nul 2>&1
	$ECHO "Compilation aborted." | tee -a $ROOT.frr
	cat $ROOT.cc.log             | tee -a $ROOT.frr
	CLEAN="n"
else
	if [ "$VERBOSE" = "y" ]; then
		$ECHO "Done." | tee -a $ROOT.frr
	fi
fi
$RM -f $ROOT.cc.log > nul 2>&1


#
# Linking program.
#
if [ $EXIT_CODE -eq 0 ]; then
	if [ "$VERBOSE" = "y" ]; then
		$ECHO -n "              " | tee -a $ROOT.frr
		$ECHO -n "Linking $ROOT.o with FpGen and GenLib:" | tee -a $ROOT.frr
	fi
	$CC  -g -o $ROOT \
			  $ROOT.o  $FITMK_FPGLBP \
					   $FITMK_FITLLS \
					   $FITMK_ALCLBP \
					   $FITMK_ALCGGN \
					   $FITMK_ALCAPR \
					   $FITMK_ALCMGN \
					   $FITMK_ALCMLO \
					   $FITMK_ALCMPH \
					   $FITMK_ALCICN 2>>$ROOT.ld.log 1>&2
	EMXBIND.EXE -b -s -q $ROOT
	rm $ROOT
	touch $ROOT
	EXIT_CODE=$?
	if [ $EXIT_CODE -ne 0 ]; then
		$RM -f $ROOT.exe $ROOT > nul 2>&1
		$ECHO "Link aborted." | tee -a $ROOT.frr
		cat $ROOT.ld.log      | tee -a $ROOT.frr
	else
		if [ "$VERBOSE" = "y" ]; then
			$ECHO "Done." | tee -a $ROOT.frr
		fi
	fi
	$RM -f $ROOT.ld.log > nul 2>&1
	$RM -f $ROOT.o      > nul 2>&1
fi


#
# Executing program.
#
if [ $EXIT_CODE -eq 0 ]; then
	if [ "$EXECUTE" = "y" ]; then
		if [ "$VERBOSE" = "y" ]; then
			$ECHO -n "            "       | tee -a $ROOT.frr
			$ECHO    "Executing $ROOT."   | tee -a $ROOT.frr
			$ECHO    ""                   | tee -a $ROOT.frr
			$ECHO    ""                   | tee -a $ROOT.frr
			$ECHO -n "          "         | tee -a $ROOT.frr
			$ECHO    "Output from $ROOT:" | tee -a $ROOT.frr
		fi
		CMD.EXE /c $ROOT.exe $ARGUMENTS 2>>$ROOT.log 1>&2;
		EXIT_CODE=$?
		cat $ROOT.log | tee -a $ROOT.frr
		if [ $EXIT_CODE -ne 0 ]; then
			$ECHO    ""                                     | tee -a $ROOT.frr
			$ECHO -n "Abnormal termination, "               | tee -a $ROOT.frr
			$ECHO    "program exit with code:${EXIT_CODE}." | tee -a $ROOT.frr
			$ECHO    ""                                     | tee -a $ROOT.frr
			$ECHO    ""                                     | tee -a $ROOT.frr
		else
			if [ "$VERBOSE" = "y" ]; then
				$ECHO "Execution completed." | tee -a $ROOT.frr
				$ECHO ""                     | tee -a $ROOT.frr
				$ECHO ""                     | tee -a $ROOT.frr
			fi
		fi
	else
		if [ "$VERBOSE" = "y" ]; then
			$ECHO ""                   | tee -a $ROOT.frr
			$ECHO ""                   | tee -a $ROOT.frr
			$ECHO "Execution skipped." | tee -a $ROOT.frr
			$ECHO ""                   | tee -a $ROOT.frr
			$ECHO ""                   | tee -a $ROOT.frr
		fi
	fi
	$RM -f $ROOT.log > nul 2>&1
fi


#
# Removing temporary files.
#
if [ "$CLEAN" = "y" ]; then
	if [ "$VERBOSE" = "y" ]; then
		$ECHO -n " Removing binary file $ROOT:" | tee -a $ROOT.frr
	fi
	$RM -f $ROOT.exe $ROOT > nul 2>&1
	if [ "$VERBOSE" = "y" ]; then
		$ECHO "Done." | tee -a $ROOT.frr
	fi
fi
if [ $EXIT_CODE -ne 0 ]; then
	$ECHO -n "     "
	$ECHO    "Errors report in $ROOT.frr file."
else
	if [ "$VERBOSE" = "y" ]; then
		$ECHO -n "Removing history file $ROOT.frr:"
	fi
	$RM -f $ROOT.frr > nul 2>&1
	if [ "$VERBOSE" = "y" ]; then
		$ECHO "Done."
	fi
fi


#
# Return the exit code.
#
exit $EXIT_CODE
