#!/bin/sh
#
#	@(#)mksls_nelsis3 1.5 Delft University of Technology 11/24/92
#
### 
### USAGE
###    mksls [-h] [-m] [topcell]
### 
### DESCRIPTION
###    Mksls searches the current project directory for *.sls and *.fun files
###    that need be recompiled. It then calls sls_mkdb or func_mkdb, whatever's
###    appropriate. If topcell is specified, mksls calls sls_exp for this
###    topcell and then executes ``sls topcell topcell.cmd''. Mksls aborts if
###    an error occurs in any of the subprocesses it spawns.
### 
### OPTIONS
###    -h                Print this help form, then exit.
###    -m                Print a script on stdout that can be processed by
###                      make(1) to obtain the required recompilations, then
###                      exit.
###
### FILES
###    The following files provide the user of mksls with a possibility to
###    override mksls's default actions and arguments to subprocesses. None of
###    the files is obligatory.
### 
###    mk.slsfiles       This file contains the names of the files that need be
###                      processed by sls_mkdb. The order in which the
###                      filenames appear in mk.slsfiles is significant.
###                      Default is *.sls.
###    mk.funfiles       This file contains the names of the files that need be
###                      processed by func_mkdb. The order in which the
###                      filenames appear in mk.funfiles is significant.
###                      Default is *.fun
###    mk.topcell        Contains the name of the top level cell. Default is
###                      the cell name supplied on the command line, see the
###                      description above.
###    mk.slsargs        Contains extra args for sls_mkdb. Defaults to "-s".
###    mk.funargs        Contains extra args for func_mkdb. The default is
###                      "-s -Cg -CI$INCLUDE", where $INCLUDE is substituted
###                      with the contents of the file mk.include (see below).
###    mk.expargs        Contains the argument to sls_exp. Defaults to "-g".
###    mk.include        Contains directory names to be searched for include
###                      files. Include files must have a suffix ".h".  The
###                      default is to have no include directories.
###    mk.dates          This is the directory that mksls creates to keep track
###                      of the time a file was last processed. If you remove
###                      this directory --or entries in it-- mksls assumes that
###                      the missing timestamp is the beginning of the epoch,
###                      thus forcing (re)compilation of that particular file.
### 
### AUTHOR
###    Paul Stravers.   Please send bug reports and suggestions to
###    nelsis-r4-bugs@donau.et.tudelft.nl
### 

# Next hack to select the correct "echo" command on bloody sparc. I hate Sun.
PATH=/usr/5bin:$PATH ; export PATH

trap "echo ajuuparapluu" 2

MAKE="make -f -"

PROGNAME=$0

set geneuzel `getopt mh $*`
if [ $? != 0 ] ; then
   sed -n -e '/^### /s///p' $PROGNAME >&2
   exit 1
fi
shift
for i in $* ; do
   case $i in
   -m)  MAKE=cat; shift;;
   -h)  sed -n -e '/^### /s///p' $PROGNAME >&2 ; exit 0;;
   --)  shift; break;;
   esac
done


if [ -f mk.slsfiles ] ; then SLSFILES=`cat mk.slsfiles` ; else SLSFILES="`echo *.sls`" ; fi
if [ -f mk.funfiles ] ; then FUNFILES=`cat mk.funfiles` ; else FUNFILES="`echo *.fun`" ; fi
if [ -f mk.topcell ] ; then TOPCELL=`cat mk.topcell` ; else TOPCELL="$1" ; fi
if [ -f mk.slsargs ] ; then SLSARGS=`cat mk.slsargs` ; else SLSARGS="-s" ; fi
if [ -f mk.funargs ] ; then FUNARGS=`cat mk.funargs` ; else FUNARGS="-s -Cg" ; fi
if [ -f mk.expargs ] ; then EXPARGS=`cat mk.expargs` ; else EXPARGS="-g" ; fi
if [ -f mk.include ] ; then INCLUDE=`cat mk.include` ; else INCLUDE=""; fi

# check if the wildcards *.sls and *.fun matched ...:
if [ "$SLSFILES" = "*.sls" ] ; then SLSFILES="" ; fi
if [ "$FUNFILES" = "*.fun" ] ; then FUNFILES="" ; fi

HEADERFILES=`for INCL in $INCLUDE; do echo $INCL/*.h; done`

for INCL in $INCLUDE; do FUNARGS="$FUNARGS -CI$INCL"; done

FUNC_MKDB="${FUNC_MKDB:-func_mkdb} $FUNARGS"
SLS_MKDB="${SLS_MKDB:-sls_mkdb} $SLSARGS"
SLS_EXP="${SLS_EXP:-sls_exp} $EXPARGS"

# construct a Makefile and feed it into $MAKE
DATESDIR="mk.dates"
(if [ "$MAKE" = "cat" ] ; then
    echo "# DO NOT EDIT !!! This file is regenerated every time you run mksls ..."
    echo "#\n# host    `hostname`\n# project `pwd`\n# date    `date`\n# user    `whoami`\n#\n"
 fi
 echo "all: $DATESDIR funfiles slsfiles"
 if [ "$TOPCELL" != "" ] ; then echo "\t$SLS_EXP $TOPCELL\n\tsls $TOPCELL $TOPCELL.cmd\n" ; fi
 echo "\n${DATESDIR}: \n\tmkdir ${DATESDIR}\n"
 if [ "$SLSFILES" = "" ] ; then
    echo "#\n# No *.sls files found in the current project directory...:\n#\nslsfiles:;\n\n"
 else
    echo slsfiles: `for SLSFILE in $SLSFILES ; do echo "$DATESDIR/$SLSFILE"; done` "\n\n"
    for SLSFILE in $SLSFILES ; do
        echo "$DATESDIR/$SLSFILE: $SLSFILE "`echo $HEADERFILES`"\n\t$SLS_MKDB $SLSFILE\n\t@touch $DATESDIR/$SLSFILE\n";
    done
 fi
 if [ "$FUNFILES" = "" ] ; then
    echo "#\n# No *.fun files found in the current project directory...:\n#\nfunfiles:;"
 else
    echo funfiles: `for FUNFILE in $FUNFILES ; do echo "$DATESDIR/$FUNFILE"; done`"\n\n"
    for FUNFILE in $FUNFILES ; do
        echo "$DATESDIR/$FUNFILE: $FUNFILE "`echo $HEADERFILES`"\n\t$FUNC_MKDB $FUNFILE\n\t@touch $DATESDIR/$FUNFILE\n";
    done
 fi
) | $MAKE
