#!/bin/sh
#
# advbuild for Linux/Unix/OSX. Copyleft Mike Arnautov 2003 - 2017.
#
# The C sources can be built into a basic executable (no readline)
# by simply compiling the lot into an executable, with an ANSI C
# compiler. The purpose of this script is to build a readline version
# if possible, and failing that  just simply compile the lot as
# indicated above. 
#
# Usage: ./advbuild [source_dir [target_dir]] [options]
#        Square brackets indicate optional arguments
#
#
# ====== This is a Linux/Unix/OSX shell script for building an   ======
# ====== A-code adventure from derived C sources. Unless the     ======
# ====== invocation command is prefixed with READLINE=NO, it     ======
# ====== attempts to build an executable which supports command  ======
# ====== history recall and editing in the console display mode. ======
# ====== In the browser display mode these features are always   ======
# ====== supported.                                              ======
#
# *********************** GENERIC DEFINITIONS **************************
#
# Change the CC definition as appropriate.
#
CC=cc
#
# INCDIRS specifies the "usual" list in which to look for header files.
# LIBDIRS ditto for libraries
#
INCDIRS="/usr/include $ul/include $HOME/include ./include $HOME ."
LIBDIRS="/usr/lib /usr/lib64 /usr/local/lib /usr/local/lib64 $HOME/lib $HOME/lib64" 
#
# ************ Some additional build flags mey be needed ***************
#
# Doesn't the unistd.h header file exist?
#
FLAGS=-DNO_UNISTD
for dir in $LIBDIRS; do
   if [ -r "$dir/unistd.h" ]; then
      FLAGS=
   fi
done
#
# ************************** READLINE SECTION **************************
#
READLINELIB=readline
CURSESLIB=ncurses
#
# INCREADLINE specifies the possible locations of the readline.h and
# history.h header files.
#
INCREADLINE="$INCDIRS /usr/include/readline /usr/local/include/readline"
INCREADLINE="$INCREADLINE $HOME/readline ./readline ../readline"
#
# ********* Define usage function to be called on errors **********
#
usage()
{
   echo
   echo "Usage: $0 [<source_dir> [<target_dir>]] [options]"
   echo
   echo "   The source directory must contain the three kernel source"
   echo "   files adv00.c adv01.c and adv0.h, as well as all *.c and *.h"
   echo "   files generated by acdc from the game's A-code sources."
   echo "   Default source directory is the current directory." 
   echo "   Default target directory is the current directory." 
   echo "   Options are any additional compilation options to be"
   echo "   passed on to the C compiler -- usually none are needed."
   echo "   See http://mipmip.org/acode/ if more information required."
   echo
   exit
}
#
# ******************* Now for the actual script... *********************
#
# Parse the command line.
#
SOURCE='.'
TARGET='.'
while [ $# -ne 0 ]; do
   case "$1" in
     -h*|--h*) usage ;;
     -*) FLAGS="$FLAGS $1" ;;
     *) if [ "$SOURCE" = '.' ]; then
           if [ -d "$1" ]; then
              SOURCE="$1"
           else
              echo Source directory $1 not found.
              exit
           fi
        else
           if [ "$TARGET" = '.' ]; then
              if [ -d "$1" ]; then
                 TARGET="$1";
              else
                 echo Target directory $1 not found.
                 exit
              fi
           else
              usage
           fi
        fi
      ;;
   esac
   shift
done
#
# Do we have something that looks like adventure C source?
#
if [ ! -r "$SOURCE/adv01.c" -o ! -r "$SOURCE/adv0.h" ]; then
   [ "$SOURCE" = '.' ] && SOURCE="current directory" || \
                          SOURCE="directory $SOURCE"
   echo
   echo The $SOURCE does not appear to contain A-code game C sources.
   echo
   exit
fi
#
# Work out the game name.
#
   if [ -r "$SOURCE/adv1.h" ]; then
      NAME="`grep '\.dat' \"$SOURCE\"/adv1.h`"
      RIFS="$IFS"
      IFS="\"."
      set -- $NAME
      IFS="$RIFS"
      NAME=${2:-adventure}
   else
      echo
      echo Unable to find game C sources generated from A-code sourcse.
      usage
   fi
#
echo
echo Building $NAME.
#
# Search for readline...
#
if [ "$READLINE" != "NO" ]; then
   READLINE=NO
   CURSES=NO
   echo
   echo Searching for $READLINELIB and $CURSESLIB libraries...
   for dir in $LIBDIRS; do
      if [ -r $dir/lib$READLINELIB.a -o -r $dir/lib$READLINELIB.so ]; then
         [ "$dir" = "/usr/lib" ] && READLINE='' || READLINE=" -L$dir"
            fi
      if [ -r $dir/lib$CURSESLIB.a -o -r $dir/lib$CURSESLIB.so ]; then
         [ "$dir" = "/usr/lib" ] && CURSES='' || CURSES=" -L$dir"
      fi
   done
   [ "$READLINE" = "NO" ] && \
      echo Unable to find lib$READLINELIB - cannot build readline-enabled executable.
   [ "$READLINE" != "NO" -a "$CURSES" = "NO" ] && \
      echo Unable to find lib$CURSESLIB - cannot build readline-enabled executable.
   [ "$READLINE" = "$CURSES" ] && CURSES=''
   [ "$READLINE" != "NO" -a "$CURSES" != "NO" ] && \
      READLINE="-DREADLINE$READLINE$CURSES -l$READLINELIB -l$CURSESLIB"
fi
#
# If readline and ncurses libraries found, look for readline header files.
#
if [ "$READLINE" != "NO" ]; then
   echo Searching for readline header files...
   INCRDL=NO
   INCHST=NO
   for dir in $INCREADLINE; do
      if [ -r $dir/readline.h ]; then
         [ "$dir" = "/usr/include" ] && INCRDL='' || INCRDL=" -I$dir"
      fi
      if [ -r $dir/history.h ]; then
         [ "$dir" = "/usr/include" ] && INCHST='' || INCHST=" -I$dir"
      fi
   done
   [ "$INCRDL" = "NO" ] && \
      echo Unable to find readline.h - cannot build readline-enabled executable.
   [ "$INCHST" = "NO" ] && \
      echo Unable to find history.h - cannot build readline-enabled executable.
   [ "$INCHST" = "$INCRDL" ] && INCHST=''
   [ "$INCRDL" != "NO" -a "$INCHST" != "NO" ] && \
      READLINE="$READLINE$INCRDL$INCHST"
fi
#
# Are we building a readline executable?
#
if [ "$READLINE" = "NO" ]; then
   READLINE='-DNO_READLINE'
fi
#
# Say what we are about to do...
#
echo
[ "$READLINE" != '-DNO_READLINE' ] && \
  echo Creating readline-enabled executable... || \
  echo Creating non-readline executable...
case "$TARGET" in
   .)  TRG=.;               TARGET=''         ;;
   /*) TRG="$TARGET";       TARGET="$TARGET/" ;;
   *)  TRG=`pwd`/"$TARGET"; TARGET="$TARGET/" ;;
esac
#
# Do it!
#
CC="$CC $FLAGS"
cd $SOURCE
if $CC *.c -o "$TRG/$NAME" $DEBUG $READLINE 2>/dev/null; then
   echo
   echo Executable $TARGET$NAME created.
else
   if [ -n "$READLINE" ]; then
      echo Failed somehow... Let\'s try for a non-readline one...
      if $CC *.c -o "$TRG/$NAME" $DEBUG 2>/dev/null; then
         echo
         echo "Sucessfuly created $TRG/$NAME."
      else
         echo Sorry... Don\'t seem to be able to do it.
      fi
   fi
fi
echo
#
############################# End of script #############################
