#!/bin/sh 
# $Id: genlib,v 1.4 1997/10/29 09:49:11 genlib Exp genlib $

help() {
      echo "Syntax: `basename $0` [-vk] source-file (without extension)"
      echo "                       -v : verbose mode"
      echo "                       -k : keeps  the executable (whith debugging"
      echo "                            informations) along with the"
      echo "                            compilation Makefile after completion"
      exit 1
}


   if [ $# -lt 1 -o $# -gt 6 ] ; then
      echo "Syntax: `basename $0` [-vk] source-file (without extension)"
      exit 1
   fi

   talk=0
   keep=0
   debug=
   lib=""
   libpath=""
   name=""
	while [ $# -gt 0 ]
	do
		case $1 in
      -v) talk=1;;
      -k) keep=1;debug=-g;;
      -vk) keep=1;debug=-g; talk=1;;
      -kv) keep=1;debug=-g; talk=1;;
		-l*) lib=$1;;
		-L*) libpath=$1;;
      *)  if [ -z "$name" ]; then 
             name=$1;
          else
		help
	  fi
      esac
      shift
	done
 	trap "rm -f ./*.$$; exit 1" 1 2 3 6

	alcbanner "GenLib" "3.3" "Procedural Generation Language" "1991"

	if [ -z "$name" ] ; then
	    help
	fi
	if [ ! -f $name.c ] ; then
		echo "There seems no to be a file called $name.c"
	    help
	fi
		
	if [ $talk -eq 1 ]; then
		echo "Generating the Makefile";
	fi

# see genpat for another way of creating Makefile ;-) Czo...

	makefile="Makefile.$$"
	echo 'include $(TOP)/etc/libraries.mk'                         > $makefile
	echo 'include $(TOP)/etc/$(MACHINE).mk'                        >> $makefile
   echo 'ALLIANCE_LIBRARY = -L$(ALLIANCE_LIB) \
                $(MGN_L) \
                $(MLU_L) \
                $(MPU_L) \
                $(MCP_L) \
                $(MAP_L) \
                $(MMG_L) \
                $(MCL_L) \
                $(MGL_L) \
                $(MAL_L) \
                $(MVL_L) \
                $(MEL_L) \
                $(MSL_L) \
                $(MHL_L) \
                $(MLO_L) \
                $(MPH_L) \
                $(MUT_L) \
                $(RCN_L)'                                       >> $makefile

   echo "ALLIANCE_INC = -I\$(ALLIANCE_INCLUDE)  -DMUT_H='\"\$(MUT_H)\"' \
                        -DMPH_H='\"\$(MPH_H)\"' -DMPU_H='\"\$(MPU_H)\"' \
                        -DMGN_H='\"\$(MGN_H)\"' -DMLU_H='\"\$(MLU_H)\"' \
                        -DMLO_H='\"\$(MLO_H)\"' "               >> $makefile
	echo "$name : $name.c"                                       >> $makefile
	echo '	$(CC) '   \
	     "$debug -o $name $name.c $libpath $lib" \
		  '$(ALLIANCE_INC) $(ALLIANCE_LIBRARY)' >> $makefile

   if [ $talk -eq 1 ]; then 
		echo "Compiling, ..."
	fi

   make -f $makefile > $name.grr 2>&1

   if [ ! $? -eq 0 ]; then 
      echo "Compilation failed!"
      cat $name.grr
		exit 1
	fi

   if [ $talk -eq 1 ] ; then
	   echo "Current execution environment"
		echo "MBK_CATA_LIB   : ${MBK_CATA_LIB-no cell library specified}"
		echo "MBK_WORK_LIB   : ${MBK_WORK_LIB-:}"
		echo "MBK_IN_LO      : ${MBK_IN_LO-vst}"
		echo "MBK_OUT_LO     : ${MBK_OUT_LO-vst}"
		echo "MBK_IN_PH      : ${MBK_IN_PH-ap}"
		echo "MBK_OUT_PH     : ${MBK_OUT_PH-ap}"
		echo "MBK_CATAL_NAME : ${MBK_CATAL_NAME-CATAL}"
   fi
   if [ $talk -eq 1 ]; then
      echo "Executing ..."
	fi
   ./$name
   exit_code=$?
   if [ $talk -eq 1 ]; then
		echo "Removing tmp files ..."
	fi
   if [ $keep -eq 0 ] ; then
		rm $name;
	fi
   rm $name.o $name.grr $makefile > /dev/null 2>&1
   exit $exit_code
