#!/bin/sh
#
# Build script for rat. This script determines the type of machine it's
# running on, and calls "make" with the appropriate macro definitions
# to set up the machine specific includes etc.
#
# NOTE: If the environment variable MAKE is set, it's value will be
#       used instead of the default "make". Use this to give a path
#       to gnu make, for example.
#
# Copyright (c) 1996,1997 University College London
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#      This product includes software developed by the Computer Science
#      Department at University College London
# 4. Neither the name of the University nor of the Department may be used
#    to endorse or promote products derived from this software without
#    specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

# Find out the type of system we're running on. The nasty stuff is needed for $OSMVER
# since some systems (HP-UX) give the OS version in a weird format. At the end of this
# we should have the following variables set:
# 
#    OSTYPE : Base operating system type
#    OSMVER : The major version number of the system
#    OSVERS : The full version number of the system
#
echo "Determining system type..."
OSTYPE=`uname -s`
OSVERS=`uname -r`
case $OSTYPE in
  Linux                  ) OSMVER=`echo $OSVERS | awk -F. '{printf("%d_%d", $1, $2)}'`
                           ;;
  SunOS | IRIX | FreeBSD ) OSMVER=`echo $OSVERS | awk -F. '{print $1}'`
                           ;;
  HP-UX                  ) OSTYPE=HPUX
                           OSMVER=`echo $OSVERS | awk -F. '{print $2}'`
                           ;;
  *             ) echo "$OSTYPE $OSVERS is not supported!"
                  exit
                  ;;
esac

echo "OSTYPE=$OSTYPE"
echo "OSVERS=$OSVERS"
echo "OSMVER=$OSMVER"

# Create a directory for the .o files, if it doesn't exist...
if [ ! -d objs/${USER:=`whoami`}/${OSTYPE}_${OSVERS} ]; then
  echo "Creating object directory objs/${USER}/${OSTYPE}_${OSVERS}..."
  mkdir bin/${USER}
  mkdir objs/${USER}
  mkdir objs/${USER}/${OSTYPE}_${OSVERS}
  chmod 775 bin/${USER}
  chmod 775 objs/${USER}
  chmod 775 objs/${USER}/${OSTYPE}_${OSVERS}
fi

cmd="${MAKE:=make} OSTYPE=$OSTYPE OSMVER=$OSMVER OSVERS=$OSVERS USER=`whoami`"
case $1 in
  rat   ) echo "Running: $cmd bin/${USER}/rat-${OSTYPE}-${OSVERS}"
          eval $cmd bin/${USER}/rat-${OSTYPE}-${OSVERS}
          exit
          ;;
  depend|tags|tar|clean) echo "Running: $cmd $1"
          eval $cmd $1
          exit
          ;;
  dist)   rm -rf dist/*
          mkdir dist/bin; mkdir dist/man; mkdir dist/man/man1
	  cd dist
	  cp ../man/man1/rat.1 man/man1/rat.1
	  cp ../README ../MODS ../COPYRIGHT .
	  for i in `ls ../bin/${USER}/`
	  do
	    echo "Making distribution $i"
	    cp ../bin/${USER}/$i bin/rat
	    tar chf $i.tar README MODS COPYRIGHT bin man
	    gzip $i.tar
	  done
	  rm -rf README MODS bin man
	  exit
	  ;;
esac

echo "                                                "
echo "Usage: Build <target>                           "
echo "Possible values of <target> are:                "
echo "  rat   : Configure and build RAT               "
echo "  depend: Derive depenency information          "
echo "  dist  : Make distribution files               "
echo "  tags  : Rebuild tags file                     "
echo "  tar   : Build compressed tar file             "
echo "  clean : Remove old object files               "
echo "The build will use make, unless the environment "
echo "variable MAKE is set, in which case it will take"
echo "precedence.                                     "
echo "                                                "

