# Configure script
# by Ken O. Burtch
#
# This is a Bourne Shell Script
#set -eu

# BUSH

# OS Environment
#
# Use an ARCH variable if one exists.  Otherwise, determine the operating
# system type and processor architecture type by uname.

echo "Configuring BUSH..."
sleep 1
echo
cd src
echo -n "checking for OS (uname -s)... "
OS=`uname -s 2>/dev/null`
if [ $? -ne 0 ] ; then
   echo "failed"
   echo
   echo "Could not run uname -s"
   exit 1
fi
echo "yes"
echo -n "checking for architecture (\$ARCH)... "
if [ -z "$ARCH" ] ; then
   echo "no"
   echo -n "checking for architecture (uname -p)... "
   ARCH=`uname -p 2>/dev/null`
   if [ $? -ne 0 ] ; then
      echo "failed"
      echo
      echo "Could not run uname -p. Use \$ARCH to specify your architecture."
      echo "For example, export ARCH=i386"
      exit 1
   fi
fi
echo "yes"
echo -n "making for... "

# A rose by any other name...any AMD/Intel x86 is a 386 for testing purposes
if [ "$ARCH" = "i486" ] ; then
   CPU_ARCH="i386"
elif [ "$ARCH" = "i586" ] ; then
   CPU_ARCH="i386"
elif [ "$ARCH" = "i686" ] ; then
   CPU_ARCH="i386"
elif [ "$ARCH" = "athlon" ] ; then # Intel-ish
   CPU_ARCH="i386"
elif [ "$ARCH" = "x86_64" ] ; then # Fedora: Intel-ish, just use 32-bit
   export ARCH="athlon64" # Fedora: x86_64 isn't valid gcc option
   CPU_ARCH="i386"
else
   CPU_ARCH="$ARCH"
fi
if [ "$OS" = "Linux" -a "$CPU_ARCH" = "i386" ] ; then
   echo -n "Linux/Intel/AMD"
   if test -r bush_os.ads ; then
      echo " (cached, make distclean to remove)";
   else
      echo
      cp bush_linux.ads bush_os.ads
   fi
elif [ "$OS" = "FreeBSD" -a "$CPU_ARCH" = "i386" ] ; then
   echo -n "FreeBSD/Intel/AMD"
   if test -r bush_os.ads ; then
      echo " (cached)";
   else
      echo
      cp bush_freebsd.ads bush_os.ads
   fi
   CFLAGSTYPE="-DFREEBSD"
elif [ "$OS" = "HPUX" ] ; then
   echo -n "HP-UX"
   if test ! -w bush_os.ads ; then
      echo " (cached)";
   else
      echo
      cp bush_hp.ads bush_os.ads
      cp bush_hp.adb bush_os.adb
   fi
   echo "  (Warning: BUSH HP-UX support is obsolete...you will need to edit bush_hp.ads/adb)"
else
   echo "unknown"
   echo
   echo "I couldn't identify your operating system."
   echo
   echo "BUSH requires a bush_os.ads file to make operating system calls."
   echo "If I made a mistake and your O/S is supported, manually copy"
   echo "a bush_* file to bush_os.  If you're operating system isn't supported"
   echo "and if you are experienced and have time, you can create a new"
   echo "bush_os.ads for your operating system.  Consult the Hacker's Guide"
   echo "in the documentation on how to port BUSH to a new operating system."
   exit 1
fi
# Check compiler
if test -f bush_os.ali ; then
   if test ! -w bush_os.ali ; then
      echo "bush_os.ali exists and is not writable...please remove it"
      exit 1
   fi
fi
echo -n "checking for gcc w/Ada (with gcc)... "
CC="gcc"
(exec $CC -c bush_os.ads >/dev/null 2>/dev/null)
#FreeBSD requires exec in a subshell because a command not found error
#is reported before the stderr redirect
if [ $? -eq 0 ] ; then
   echo "yes"
else
   echo "no"
   echo -n "checking for gcc w/Ada (with gnatgcc)... "
   CC="gnatgcc"
   (exec $CC -c bush_os.ads >/dev/null 2>/dev/null)
   if [ $? -eq 0 ] ; then
      echo "yes"
   else
      echo "no"
      echo -n "checking for gcc w/Ada (with adagcc)... "
      CC="adagcc"
      (exec $CC -c bush_os.ads >/dev/null 2>/dev/null)
      if [ $? -eq 0 ] ; then
         echo "yes"
      else
         echo "no"
         echo -n "checking for gcc w/Ada (with gcc-gnat)... "
         CC="gcc-gnat"
         $CC -c bush_os.ads >/dev/null 2>/dev/null
         if [ $? -eq 0 ] ; then
            echo "yes"
         else
            echo "no"
            echo
            echo "Your system does not have a GCC compiler that understands the Ada language"
            echo
            echo "BUSH requires one of the following:"
            echo "   GCC 3.x or newer with the Ada language enabled (http://gcc.gnu.org)"
            echo "   ACT's GNAT compiler 3.14 or newer, based on GCC 2.7.2 (ftp://cs.nyu.edu/pub/gnat)"
            exit 1
        fi
      fi
   fi
fi

# Check for the Ada Run-Time Library

echo -n "checking for libgnat (ldconfig -p, Linux)... "
TMP=`/sbin/ldconfig -p 2>/dev/null | grep libgnat`
if [ -z "$TMP" ] ; then
   echo "no"
   echo -n "checking for libgnat (ldconfig -r, FreeBSD)... "
   TMP=`/sbin/ldconfig -r 2>/dev/null | grep libgnat`
   if [ -z "$TMP" ] ; then
      echo "no"
      echo "No version of the Ada Run-Time Environment (libgnat) was found"
      echo "If you have installed libgnat, you may need to use ldconfig (as"
      echo "root) to update the shared library search paths for libgnat"
      echo
      echo "BUSH requires one of the following:"
      echo "   GCC 3.x with Ada enabled"
      echo "   ACT's GNAT 3.14 or newer"
      exit 1
    else
       echo "yes"
    fi
 else
  echo "yes"
fi

# Check for the libSDL (Simple DirectMedia Layer) Library

echo -n "checking for libSDL (ldconfig -p, Linux)... "
TMP=`/sbin/ldconfig -p 2>/dev/null | grep libSDL`
if [ -z "$TMP" ] ; then
   echo "no"
   echo -n "checking for libSDL (ldconfig -r, FreeBSD)... "
   TMP=`/sbin/ldconfig -r 2>/dev/null | grep libSDL`
   if [ -z "$TMP" ] ; then
      echo "no"
      echo "No version of the Simple DirectMedial Layer (libSDL) was found"
      echo "If you have installed libSDL, you may need to use ldconfig (as"
      echo "root) to update the shared library search paths for libSDL"
      echo
      echo "libSDL is used by BUSH to access sound and graphics on your machine"
      exit 1
    else
       echo "yes"
    fi
 else
  echo "yes"
fi

if [ -z "$SDL_CONFIG" ] ; then
   SDL_CONFIG="sdl-config"
fi
echo -n "checking for SDL_config... "
TMP=`(exec $SDL_CONFIG --version 2>/dev/null)`
if [ -z "$TMP" ] ; then
   # Cannot find SDL_config?  Use these defaults for makefile sed...
   if [ "$OS" = "FreeBSD" ] ; then
      SDLLIBSTYPE="-L\/usr\/local\/lib -lSDL-1.1\ -lSDL_image -lpthread"
      SDLINCLTYPE="-I\/usr\/local\/include\/SDL11"
   else
      SDLLIBSTYPE="-l\ SDL\ -l\ SDL_image"
      SDLINCLTYPE="-I\/usr\/include\/SDL"
   fi
   echo "no"
else
   # Remove -D_REENTRANT (not recognized by GCC Ada) and escape special
   # sed characters (space and slash)
   SDLLIBSTYPE=`$SDL_CONFIG --libs`" -lSDL_image"
   SDLLIBSTYPE=`echo "$SDLLIBSTYPE" | sed 's/\ /\\\ /g;s/\//\\\\\//g'`
   SDLINCLTYPE=`$SDL_CONFIG --cflags | sed "s/\ -D_REENTRANT//g"`
   SDLINCLTYPE=`echo "$SDLINCLTYPE" | sed 's/\ /\\\ /g;s/\//\\\\\//g'`
   echo "yes"
fi

# Check for the libSDL_image (Simple DirectMedia Layer image loading) Library
# This is normally included with SDL but is not included with Fedora Core 4 Linux.

echo -n "checking for libSDL_image (ldconfig -p, Linux)... "
TMP=`/sbin/ldconfig -p 2>/dev/null | grep libSDL_image`
if [ -z "$TMP" ] ; then
   echo "no"
   echo -n "checking for libSDL_image (ldconfig -r, FreeBSD)... "
   TMP=`/sbin/ldconfig -r 2>/dev/null | grep libSDL_image`
   if [ -z "$TMP" ] ; then
      echo "no"
      echo "No version of the Simple DirectMedial Layer image library (libSDL_Image) was found"
      echo "This auxillary library is normally included with libSDL."
      echo "Fedora users can install it using this command: yum install SDL_image"
      echo "If you have installed libSDL_image, you may need to use ldconfig (as"
      echo "root) to update the shared library search paths for libSDL_image"
      echo
      echo "libSDL_image is used by BUSH to load image files (JPEGs, GIFs, etc.)"
      exit 1
    else
       echo "yes"
    fi
 else
  echo "yes"
fi

# Check for the libSDL_ttf (Simple DirectMedia Layer truetype font) Library

#echo -n "checking for libSDL_ttf (ldconfig -p, Linux)... "
#TMP=`/sbin/ldconfig -p 2>/dev/null | grep libSDL_ttf`
#if [ -z "$TMP" ] ; then
#   echo "no"
#   echo -n "checking for libSDL_image (ldconfig -r, FreeBSD)... "
#   TMP=`/sbin/ldconfig -r 2>/dev/null | grep libSDL_ttf`
#   if [ -z "$TMP" ] ; then
#      echo "no"
#      echo "No version of the Simple DirectMedial Layer truetype font library (libSDL_Image) was found"
#      echo "This is a third-party  library not normally included with libSDL."
#      echo "Fedora users can install it using this command: yum install SDL_ttf"
#      echo "If you have installed libSDL_ttf, you may need to use ldconfig (as"
#      echo "root) to update the shared library search paths for libSDL_ttf"
#      echo
#      echo "libSDL_ttf is not currently used.  It will be used by the pen package to"
#      echo "to display text."
#      exit 1
#    else
#       echo "yes"
#    fi
# else
#  echo "yes"
#fi

# Make

echo -n "checking for gmake... "
GMAKETYPE=gmake
$GMAKETYPE --version > /dev/null
if [ $? -eq 0 ] ; then
   echo "yes"
else
   echo "no"
   echo -n 'checking for ${GMAKE}... '
   if [ ! -z "$GMAKE" ] ; then
      GMAKETYPE="$GMAKE"
      $GMAKETYPE --version > /dev/null
      if [ $? -eq 0 ] ; then
         echo "yes"
      else
         echo "no"
         echo
         echo "BUSH requires GNU make"
         exit 1
      fi
   else
      echo "no"
      echo
      echo "BUSH requires GNU make"
      exit 1
   fi
fi

echo "BUSH is configured"

# ADAVOX library

echo
sleep 3
echo "Configuring third-party ADAVOX (AU/WAV Library)..."
sleep 1

# /dev/dsp

echo
echo -n "adavox library: checking for /dev/dsp... "
if test ! -z "$BASH" ; then
   test -a /dev/dsp
else
   test -r /dev/dsp
fi
if [ $? -eq 0 ] ; then
   echo "yes"
else
      echo "no"
      echo
      echo "ADAVOX requires /dev/dsp.  Create a /dev/dsp symlink to your real audio device"
      echo "(If you don't care about audio support, try ln -s /dev/null /dev/dsp)"
      exit 1
fi

echo "ADAVOX library is configured"

# APQ libarary

echo
sleep 3
echo "Configuring third-party APQ (Database Library)..."
sleep 1
echo "Running APQ's configure..."
cd apq-2.1
./configure
cd -
#if test -f Makeincl ; then
#   mv -f Makeincl apq-2.1/Makeincl
#fi
echo "APQ library is configured"

# Create GNUmakefile from GNUmakefile.orig

echo
echo "Returning to BUSH..."
echo "Creating src/GNUmakefile..."

sed "s/CCTYPE/$CC/g;s/CPUTYPE/$ARCH/g;s/GMAKETYPE/$GMAKETYPE/g;s/SDLLIBSTYPE/$SDLLIBSTYPE/g;s/SDLINCLTYPE/$SDLINCLTYPE/g;s/CFLAGSTYPE/$CFLAGSTYPE/g" < GNUmakefile.orig > GNUmakefile
cd ..
echo "Creating GNUmakefile..."
sed "s/CCTYPE/$CC/g;s/CPUTYPE/$ARCH/g;s/GMAKETYPE/$GMAKETYPE/g;s/CFLAGSTYPE/$CFLAGSTYPE/g" < GNUmakefile.orig > GNUmakefile

# Done

echo "Type gmake all"
exit 0

