#!/bin/sh

cat > build.hlp <<EOF
Usage: build <target-platform> <make-options>

if target-platforms is:
  clean           : Clean up after a compile
  mrclean         : Clean up all non-distibution files
  install         : Install Cosm headers and libraries on system

target-platform may be one of the following:
  beos-x86                 : BeOS on x86 family
  freebsd-x86              : FreeBSD on x86 family
  irix-mips                : IRIX on 32bit mips family (cc)
  irix-mips64              : IRIX on 64bit mips family (cc)
  irix64-mips64            : IRIX64 on 64bit mips family (cc)
  linux-x86                : Linux on x86 family
  linux-alpha              : Linux on Alpha family
  linux-alpha-ccc          : Linux on Alpha family (Compaq compiler)
  linux-ppc                : Linux on PowerPC family
  linux-sparc              : Linux on Sparc family
  linux-sparc64            : Linux on 64bit Sparc family
  nutc-x86                 : Win32 on x86 family via NuTCRACKER
  solaris-x86              : Solaris on x86 family
  solaris-sparc            : Solaris on 32bit Sparc family
  solaris-sparc64          : Solaris on 64bit Sparc family
  solaris-sparc-workshop   : Solaris on 32bit Spar familyc (Sun Workshop cc)
  solaris-sparc64-workshop : Solaris on 64bit Sparc family (Sun Workshop cc)
  sunos-sparc              : SunOS (Solaris 1) on 32bit Sparc family
  tru64-alpha              : Tru64/OSF1 on Alpha family (cc)
  win32-x86                : Win32 on x86 family (cygwin)
  <missing os-cpu>: Start with make/TEMPLATE, and send in what works :)

General <make-options> (see also: cputypes.html in documentation)
-DNO_THREADS      : Do not add thread support.
-DNO_NETWORKING   : Do not add networking support.
-DNO_IEEE_FLOAT   : Compile for systems without IEEE floating point support.
-DWITH_GUI        : Include OpenGL/GLU support, system libraries required.
-DNO_CRYPTO       : Do not compile in crypto functions. (i.e. no crypto.c)

Additional options may be required on old platforms, or platforms
with many non-standard configurations, see README.TXT for notes.
EOF

maketarget="NULL"
makeargs=""
makelibs=""

args=$#
while [ $args -gt 0 ]
do
  case $1 in

    help)
      more build.hlp
      exit ;;

    -L*|-l*)
      makelibs="$makelibs $1" ;;

    *??-?*|install|clean|mrclean)
      if [ $maketarget != NULL ]
      then
        makeargs="$makeargs $1"
      else
        maketarget=$1
      fi ;;

    *)
      makeargs="$makeargs $1" ;;

  esac

  shift

  args=`expr $args - 1`

done

echo 'make target is : '$maketarget''
echo 'extra args are : '$makeargs''
echo 'extra libs are : '$makelibs''
echo ''

case $maketarget in

  *??-?*)
    echo "Copying Makefiles."
    rm -f Makefile
    if [ -f make/common ]; then
      cat make/common > Makefile
    fi
    rm -f Makefile.cosm
    if [ -f make/$maketarget ]; then
      cat > Makefile.cosm <<EOF
EXTRA_FLAGS = $makeargs
EXTRA_LIBS = $makelibs

EOF
      cat make/$maketarget >> Makefile.cosm
    fi
    #add more error checking above
    echo "Making common library."
    cd lib
    make "EXTRA_FLAGS=$makeargs" "EXTRA_LIBS=$makelibs" all
    cd ..
    echo "Done"
    ;;

  clean)
    echo "Cleaning base directory"
    rm -f *~ make/*~ lib/*~
    echo "Cleaning common library"
    cd lib
    make "EXTRA_FLAGS=$makeargs" "EXTRA_LIBS=$makelibs" clean
    cd ..
    ;;

  install)
    cd lib
    make "EXTRA_FLAGS=$makeargs" "EXTRA_LIBS=$makelibs" install
    cd ..
    echo "  To install the man pages type 'make' then 'make install'"
    echo "  in the doc/ directory"
    ;;

  mrclean)
    ./build clean
    rm -f Makefile*
    rm -f build.hlp
    ;;

  NULL)
    echo "No target plaform for which to build given."
    echo 'Give command "build help" for help.'
    ;;

  *)  echo 'Do not know how to make for target "'$maketarget'".'
    ;;
esac

