#!/bin/bash
#
# zcc, zcompile ---  a wrapper script to run the  inform6 or inform7 script
#                    depending on the source's suffix
# (c) 2008 Valerie Winter
#
# requirements:
#               -- installed Inform 6.31 and/or Inform 7 packages by Graham
#                  Nelson (inform-fiction.org).
#               -- POSIX/Unix  system  structure  (ie.,  GNU/Linux,   xBSD,
#                  OpenSolaris, BeOS, QNX RTOS/RTP, MacOS X.
#                  Microsoft world? good joke.)
#
# --------------------------------------------------
# IMPORTANT NOTES
#
# --> Compiling Inform 6 source with Inform 7
#
#     If you intend to compile existing I6 source with I7, create a project
#   directory with  'zcc -6 <story>.inform'  and copy the sources  into the
#   Source/ subdirectory, with the main .inf file renamed to 'main.inf'.
#     Ensure that,  within your source code, all local includes are  of the
#   form  'Include ">myinclude.h"',  with  '>' and  the appropriate  suffix
#   given.   Otherwise,  Inform  7's inform-6.31-biplatform  will  complain
#   heavily and abort.
#
#
# --> zcc project directories and I7 GUI
#
#     The  project directories  created  with zcc  can  be used  completely
#   normally in the Inform 7 GUI.  HTML status and index files generated by
#   zcc can be viewed in I7 or any web browser, like those generated by ni.
#
#
# --> No Glulx support, never, fuck it
#
#     Inform 7  and inform6-6.31-biplatform  also support compiling  to the
#   Glulx VM.   This wrapper  front-end, however, does  *not*.  This  is on
#   purpose and fully intended, and it will never change.
#   This package is called "_z_cc", after all.
#   ZCode ---	e viva!
#
# --------------------------------------------------
#
# ---Valerie Winter

self=`basename $0`;
paramlist=$@;
passthru="$paramlist";
script="";

zasmscript=zcc.zasm;
i6script=zcc.inf;
i7script=zcc.inform;


if test -z "$paramlist"; then
    cat <<EOF
zcc --- the "ZCode Compiler" wrapper  script to run Z-ASM, Inform 6, or the
        Inform 7 compiler suite depending on the source's type.
zcc 0.5 (c) 2008 Valerie Winter.
Z-ASM (c) 1988--2002 ??
Inform, Inform 6.x, Inform 7 (c) 1993--2008 Graham Nelson et al.

usage:
   Z-Machine assembly code:           zcc [-d|-r] <main file>.zasm

   classical (Inform 6) .inf source:  zcc [-d|-r] <main file>.inf
                                      zcc [-d|-r] <story name>.i6

	    where <story name>.i6 must be a directory containing 
	    <story name>.inf as the main file.

   Inform 7 project trees:            zcc [-d] <project>.inform
   (classical and "natural" Inform)   zcc -r [-b] <project>.inform
                                      zcc -6|-7 <project>.inform

options:
   -d | --debug       compile with debugger options and run in dfrotz
   -r | --release     release the final version of the project

options for Inform 7 only:
   -b | --blorb       create Blorb package on release
   -6 | --newclassic  create new "classic Inform" project (Source/main.inf)
   -7 | --newnatural  create new "Natural Inform" project (Source/story.ni)

Default mode is to compile to z8.  
Default release mode is without Blorb enclosing.

  The project directories and HTML  files created with zcc can be used
completely normally in the Inform 7 GUI.

IMPORTANT: Inform 7 and inform6-6.31-biplatform also support compiling
           to  the Glulx  VM.  This  wrapper front-end,  however, does
           *not*.  This is on purpose  and fully intended, and it will
           never change.  This is the ZCode Compiler.
EOF
    exit 1; 
fi


for param in $paramlist; do
    suffix=`echo $param | cut -f2 -d.`;
    case $suffix in
	zasm) script=$zasmscript;
	    ;;
	inf) script=$i6script;
	    ;;
	inform) script=$i7script;
	    ;;
	*) test -e "$param.zasm" &&  script=$zasmscript;
	   test -e "$param.inf" &&  script=$i6script;
	   test -e "$param.i6" &&  script=$i6script;
	   test -d "$param.inform" &&  script=$i7script;
	    ;;
    esac
    shift;
done

test "$script" && \
    $script $passthru \
    || echo "$self: error: unknown type of source or no source given.";

exit 0;

