#!/bin/bash
#
#  ifcompile  (c) 2007 by Valerie Winter
#             being part of the author's if compiler packages,
#             if_compilers_free_engines-<os>-<arch>-bin.tar.bz2
#
#  Note: since  people tend to  name their  compilers and  sources somewhat
#        wildly  strange,  my  clearly  named  default  settings  might  be
#        irritating   to  them.    The  compiler   names  are   links  into
#        /etc/alternatives, pointing to the actual binaries.  fallbacks are
#        inform, advcom, hc,  alan, t3make; note that inform  6.31, tads 3,
#        hugo  3,  and alan  3  will be  assumed.   Source  code for  older
#        compilers will not compile.
#
# Concerning Inform6/7:
# *NO* compilation to Glulx supported.  Dot.  Big one.  Not with my script.
# Use zcode or buzz off.


self=`basename $0`;
params=$@;
logfile=compile.log

function display_usage()
  {
    cat <<EOF
ifcompile  --- a  small  helper  script for  the  compilation of  text
             adventures (aka Interactive Fiction) that spares you from
             typing common settings.
             Supports: Inform6, TADS3, Hugo3, ALan3, Adventure System.
(c) 2007 by Valerie Winter	     

usage: 
        $self -h|--help
        $self <main source file>
        $self
        $self <engine>

The latter  two cases make use  of 'make -f  Makefile.<engine>' if any
Makefile is available, where <engine> can be either of inform6, zcode,
hugo,  alan, tads,  or advsys.   The <engine>  parameter distinguishes
which engine  should be built if  more than one  Makefile is available
(no  name  given  builds  all).  See  the  accompanying  demonstration
Makefiles for details.

Andrew Plotkin, the "Author" of Glulx, is explicitly forbidden to use,
modify, or even look at this script.
EOF
  }

for item in $params; do
    case $item in
	-h|--help) display_usage;
	           exit 0;
	           ;;
	        *) sourcefile=$item;
	           shift;
		   ;;
    esac
done


case $sourcefile in
    inform6|\
    zcode|\
    tads|\
    hugo|\
    alan|\
    advsys) test -r ./Makefile.$sourcefile && \
		make -f Makefile.$sourcefile &>${logfile} || \
	      echo "$self: error: no $sourcefile makefile available";
	    sourcefile="";
	    ;;
    *) for item in inform6 zcode tads hugo alan advsys; do
	 test -r ./Makefile.$item && \
	    {
		echo "$self: compiling for $item engine";
	      make -f Makefile.$item &>${logfile} 
	    } || \
	    echo "$self: no $item makefile found";
       done
       test -r Makefile -o -r makefile && \
	  {
	    echo "$self: generic 'Makefile' found, using this";
	    make &>${logfile};
	  }
	    || echo "$self: no generic 'Makefile' found";
       sourcefile="";
       ;;
esac


test -z "$sourcefile" && exit 0;

story=`basename $sourcefile | cut -f1 -d.`;
suffix=`basename $sourcefile | cut -f2 -d.`;

case $suffix in
    inf) if test -r ./Makefile.inform6; then
	    make -f Makefile.inform6 &>${logfile};
	 elif test -r ./Makefile.zcode; then
	    make -f Makefile.zcode &>${logfile};
         #  *NO*  compilation  to Glulx.  Dot.  Big one.  Not  with my
         # script.  Use zcode or f*** off.
	 elif test "`type -p inform6-compiler`"; then
	    inform6-compiler -ceusx\~S\~D -v5 -C1 \$large $sourcefile;
	 elif test "`type -p inform`"; then
	    inform -ceusx\~S\~D -v5 -C1 \$large $sourcefile;
	 else
	    echo "$self: error: no Inform6 compiler found.";
	    exit 1;
	 fi
	;;
      t) echo "$self: warning: assuming source to be in TADS3 format.";
	 if test -r ./Makefile.tads; then
	    make -f Makefile.tads &>${logfile};
	 elif test "`type -p tads3-compiler`"; then
	    test -d build || mkdir build;
	    test -d build/symbols || mkdir build/symbols;
	    tads3-compiler -o ${story}.t3 -pre -nodef \
	      -D LANGUAGE=en_us -D MESSAGESTYLE=neu \
	      -Fy build/symbols -Fo build -w1 -lib system -lib adv3/adv3 \
	      $sourcefile;
	 elif test "`type -p t3make`"; then
	    test -d build || mkdir build;
	    test -d build/symbols || mkdir build/symbols;
	    t3make -o ${story}.t3 -pre -nodef -D LANGUAGE=en_us -D MESSAGESTYLE=neu \
	   -Fy build/symbols -Fo build -w1 -lib system -lib adv3/adv3 \
	   $sourcefile;
	 else
	    echo "$self: error: no TADS 3 compiler found.";
	    exit 1;
	 fi
	;;
    adv) if test "`type -p advsys-compiler`"; then 
	    advsys-compiler $story;
	 elif test "`type -p advcom`"; then 
	    advcom $story;
	 else
	    echo "$self: error: no Adventure System compiler found.";
	    exit 1;
	 fi
	;;
 aln|alan) if test -r ./Makefile.alan; then
	    make -f Makefile.alan &>${logfile};
	 elif test "`type -p alan3-compiler`"; then
	    alan3-compiler $sourcefile -pack -summary;
	 elif test "`type -p alan`"; then
	    echo "$self: warning: defaulting to 'alan'.  It is not sure that it";
	    echo -e "\tis the right version for the source code you gave me";
	    echo -e "\t(eg. alan3 source while only an alan2 compiler is installed)";
	    alan $sourcefile -pack -summary;
	 else
	    echo "$self: error: no ALan compiler found.";
	    exit 1;
	 fi
	;;
hug|hugo) if test -r ./Makefile.hugo; then
	     make -f Makefile.hugo &>${logfile};
	  elif test "`type -p hugo3-compiler`"; then
	     test -d build || mkdir build;
	     hugo3-compiler -fs $sourcefile \@temp=build \@object=. ;
	  elif test "`type -p hc`"; then
	    echo "$self: warning: defaulting to 'hc'.  It is not sure that it";
	    echo -e "\tis the right version for the source code you gave me";
	    echo -e "\t(eg. hugo3 source while only a hugo2 compiler is installed)";
	    echo -e "\tif this attempt fails, you need to install the right one";
	     test -d build || mkdir build;
	     hc -fs $sourcefile \@temp=build \@object=. ;
	  else
	    echo "$self: error: no Hugo compiler found.";
	    exit 1;
	  fi
	;;
esac

exit 0;
