#!/bin/sh
#
# makeall - little helper for calling
# make in the appropriate subdirs
#
# created: mpichler, 19960523
# changed: mpichler, 19970516


usage()
{
  echo '
usage: makeall [-]OPTION
       makeall [-]i [MAKEOPTS]

OPTION is one (cannot be combined) of:
  h  print this usage help
  r  set root dir
  m  make Makefiles from Imakefiles
  a  make Makefiles and make depend
  i  make and "install" binaries (into ./installed)
     MAKEOPTS (e.g. -n or -k) are passed to make call

See file INSTALLATION for more information.

sample usage (set root dir, create Makefiles, do compilation):
  makeall -r
  makeall -m
  makeall -i
'
  exit 0
} # usage


echo "VRweb 1.5 source code compilation script."


# here you may hardcode your CPU type
# CPU=

# try to determine CPU type from system name (if unset)
if [ _"$CPU" = _ ]
then
  case "`uname -s`" in
    AIX )     CPU=IBMAIX_GNU ;;
    FreeBSD ) CPU=FREEBSD    ;;
    HP-UX )   CPU=HPUX9_GNU  ;;
    IRIX* )   CPU=SGI_GNU    ;;
    Linux )
      case "`uname -r`" in
        1* )  CPU=LINUX      ;;
        2* )  CPU=LINUX_ELF  ;;
      esac
    ;;
    OSF1 )    CPU=ALPHA_GNU  ;;
    SunOS )
      case "`uname -r`" in
        4* )  CPU=SUN4_GNU   ;;
        5* )  CPU=SUN5_GNU   ;;
      esac
    ;;
    ULTRIX )  CPU=PMAX_GNU   ;;
  esac
fi

if [ _"$CPU" = _ ]
then
  echo "makeall. error: unable to set CPU"
  exit 1
fi

echo "CPU set to: $CPU"
export CPU

# make sure scripts are found
if [ ! -d `pwd`/config/scripts ]
then
  echo "makeall. error: could not find script directory ./config/scripts"
  echo "(must run makeall in directory where it is located)"
  exit 1
fi

PATH=`pwd`/config/scripts:"$PATH" ; export PATH
# echo "PATH set to: $PATH"
echo "`pwd`/config/scripts prepended to PATH"

# here you may prepend the path to GNU make
# (or create link in scripts dir, see INSTALLATION guide)
# PATH=/usr/local/bin:"$PATH" ; export PATH
echo "make command used: `type make`"
if make --help 2>/dev/null >&2
then
  echo "make seems to be GNU make"
else
  echo "makeall. error: make is not GNU make; please set PATH to GNU make"
  exit 1
fi

# now ready to compile
date


setrootdir()
{
# usage (internal): setrootdir file prefix
  pfile=`pwd`/$1
  if [ ! -f "$pfile" ]
  then
    echo "makeall. error: could not find ./$1"
    exit 1
  fi
  mv "$pfile" "$pfile"~
  echo "setting RootDir in ./$1 to" `pwd`
  # this will fail if the current path contains a '%' sign, do it manually then
  sed 's%^\('"$2"'\).*%\1'`pwd`'%' "$pfile"~ > "$pfile"
} # setrootdir


case "$1" in
  *h* | *\?* )
    usage
  ;;
  r | -r )
    setrootdir config/hg_path.def "#define RootDir "
    setrootdir config/scripts/hgmkmf "RootDir="
    chmod +x config/scripts/hgmkmf
    exit
  ;;
  m | -m )
    cd src ; hgmkmf ; make Makefiles
  ;;
  a | -a )
    cd src ; hgmkmf -a
  ;;
  i | -i )
    shift
    cd src ; make $* install
    # install (into ./installed) or .h files are not found
  ;;
  * )
    echo '
makeall: invalid or missing option.'
    usage
  ;;
esac

# output no longer logged by this script itself
echo "... makeall finished."
date
