#!/bin/sh
#
# advbuild for Linux/Unix/MacOS. Copyleft Mike Arnautov 2003 - 2013.
#
# 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 [DEBUG]]]]
#
# ====== This is a Linux/Unix/MacOS shell script for building an  ======
# ====== A-code advneture 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
#
# For MacOS we need to do a few extra things. Thanks to Richard Mathew for
# the necessary adjustments!
#
case "`file /usr/bin/open 2>/dev/null`" in
   *Mach*)
      ARCH="-arch i386 -arch ppc" 
      CC="$CC $ARCH"
      SDK="/Developer/SDKs/MacOSX10.2.8.sdk"
      if [ -d ${SDK} ]; then
         CFLAGS="-isysroot ${SDK}"
         LDFLAGS="-isysroot ${SDK} -Wl,-syslibroot,${SDK}"
         CC="$CC $CFLAGS $LDFLAGS"
      fi 
   ;;
   *) ;;
esac
#
# If stdlib.h exists, it is better to force its use.
#
[ -r /usr/include/stdlib.h ] && CC="$CC -DNEED_STDLIB"
#
# INCDIRS specifies the "usual" list in which to look for header files.
#
INCDIRS="/usr/include /usr/local/include $HOME/include ./include $HOME ."
#
# ************************** 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"
#
# ******************* Now for the actual script... *********************
#
SOURCE="${1:-.}"
TARGET="${2:-.}"
DEBUG="${3:+-DLOG -DDEBUG -g}"
#
# Do we actually have something that look 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 complete C sources.
   echo
   exit
fi
#
# Work out the game name.
#
   NAME="`grep '\.dat' $SOURCE/adv1.h`"
   RIFS="$IFS"
   IFS="\"."
   set -- $NAME
   IFS="$RIFS"
   NAME=${2:-adventure}
#
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!
#
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 #############################
