#! /bin/sh
#
# SETUP
# configure, compile, and install a PRODUCT interactively
#
# Initially written for use with Monsanto-NQS v3.50.
#
# Author:
#
# Stuart Herbert (S.Herbert@sheffield.ac.uk)
# Academic Computing Services, University of Sheffield
#
# Copyright (c) 1995.  All rights reserved.
#
##########################################################################
# -*- WARNING -*-
#
# This version of SETUP requires that your /bin/sh supports the use of
# shell functions.  If your /bin/sh does NOT provide this support, you
# should change line one of this script to use either /bin/ksh or the
# Free Software Foundation's /bin/bash shell.
#
# -*- WARNING -*-
##########################################################################
# Modifications
#
#	2.0	SLH	08 Jul 95	Initial version
#
##########################################################################

set -h

case "$TERM" in
  vt*|ansi*|linux|con*|xterm*)
    COLOR_NORMAL="[0m"
    COLOR_BOLD="[1m"
    COLOR_UL="[4m"
    COLOR_BLINK="[5m"
    ;;
  *)
    COLOR_NORMAL=""
    COLOR_BOLD=""
    COLOR_BLINK=""
    COLOR_UL=""
    ;;
esac

HOST=`(uname -n)` ||
HOST=`(hostname)` ||
HOST=UNKNOWN

PRODUCT_DIR="./Product/Options"
PLATFORM_DIR="./Product/Platforms"

TOP_DIR="$PWD"

if [ "$1" = "--clean" ]; then
  CLEAN=1
  shift
fi

############################################################################
function ShowNormal ()
{
  echo "$1"
}

function ShowBold ()
{
  echo "${COLOR_BOLD}$1${COLOR_NORMAL}"
}

function ShowBlink ()
{
  echo "${COLOR_BLINK}$1${COLOR_NORMAL}"
}

function ShowUl ()
{
  echo "${COLOR_UL}$1${COLOR_NORMAL}"
}

############################################################################

function StepOne ()
{
  local tmpvar
  
  clear
  ShowBold "Step One : Your Computer"
  Separator
  BlankLine
  ShowBlink "Please wait ... examining your computer."
  BlankLine
  
  tmpvar=`./Source-Tree/SETUP/all-systems/platform.sh`
  GENERIC=`echo $tmpvar | cut -d : -f 1`
  GEN_MAJOR=`echo $tmpvar | cut -d : -f 2`
  PLATFORM=`echo $tmpvar | cut -d : -f 3`
  UNIX_CLASS=`echo $tmpvar | cut -d : -f 4`
  OUR_PATH=`echo $PATH | tr ':' ' '`
  
  OUR_SHELL="x"
  for x in bash ksh ; do
    for y in $OUR_PATH ; do
        if [ -x "$y/$x" -a $OUR_SHELL = "x" ]; then
	  OUR_SHELL="$y/$x"
	fi
    done
  done
  
  if [ "$OUR_SHELL" = "x" ]; then
    OUR_SHELL=/bin/sh
  fi
  
  ShowNormal "   Hostname            : $HOST"
  ShowNormal "   Operating System    : $GENERIC"
  ShowNormal "     Version           : $PLATFORM"
  ShowNormal "   UNIX Type     (*)   : $UNIX_CLASS"
  BlankLine
  ShowNormal "   Bourne Shell (**)   : $OUR_SHELL"
  BlankLine
  ShowNormal "If any of these details are wrong, ESPECIALLY the UNIX Type, please"
  ShowNormal "contact the author of SETUP; see the file ./Source-Tree/SETUP"
  ShowNormal "/all-systems/SETUP.paper."
  BlankLine
  ShowNormal "(*)  The UNIX Type indicates which UNIX programming interface is the"
  ShowNormal "     prefered one on this machine."
  BlankLine
  ShowNormal "(**) SETUP has determined that this is the most powerful /bin/sh-compatible"
  ShowNormal "     shell installed on your system."
  BlankLine
  Separator
  Pause
}

############################################################################

function StepTwo ()
{
  clear
  ShowBold "Step Two : The Product"
  Separator
  BlankLine
  . ./${PRODUCT_DIR}/${PRODUCT}
  BlankLine
  if [ -n "$BUILD_NO" ]; then
    ShowBold "(*) USE THIS VERSION FOR TESTING PURPOSES ONLY.  DO NOT USE TO PROVIDE"
    if [ -n "${PREVIOUS_RELEASE}" ]; then
      ShowBold "    AN END-USER SERVICE - USE VERSION ${PREVIOUS_RELEASE} INSTEAD."
    else
      ShowBold "    AN END-USER SERVICE."
    fi
    BlankLine
  fi
  Separator
  Pause
}

############################################################################
# StepThree - build the source tree

function StepThree ()
{
  local x
  
  clear
  ShowBold "Step Three : The Source Code Tree"
  Separator
  BlankLine
  
  BUILD_DIR="./Build-Tree/$PLATFORM/$HOST"
  ShowNormal "  Configuring For          : $PLATFORM"
  
  if grep "$PLATFORM : Y" ${PLATFORM_DIR}/system-list 2> /dev/null 1>&2 ; then
    TARGET="$PLATFORM"
  elif grep "$GEN_MAJOR : Y" ${PLATFORM_DIR}/system-list 2> /dev/null 1>&2 ; then
    TARGET="$GEN_MAJOR"
  elif grep "$GENERIC : Y" ${PLATFORM_DIR}/system-list 2> /dev/null 1>&2 ; then
    TARGET="$GENERIC"
  elif grep "$UNIX_CLASS : Y" ${PLATFORM_DIR}/system-list 2> /dev/null 1>&2 ; then
    TARGET="$UNIX_CLASS"
  else
    TARGET="UNKNOWN"
  fi
  
  if [ "$TARGET" = "UNKNOWN" ]; then
    BlankLine
    ShowBold "  SETUP cannot guarentee that this product will compile on this"
    ShowBold "  version of UNIX, because this version of UNIX is not listed"
    ShowBold "  in the file ${PLATFORM_DIR}/system-list."
    BlankLine
    ShowBold "  The text file ./PORTING has instructions on how to port this"
    ShowBold "  product to this version of UNIX."
    BlankLine
    Pause
  fi
    
  ShowNormal "  Placing Source Code In   : $BUILD_DIR"
  
  if [ -d "$BUILD_DIR" ]; then
    if [ "$CLEAN" = "1" ]; then
      ShowBold "    Removing Old Code From : $BUILD_DIR"
      BlankLine
      rm -rf $BUILD_DIR
    else
      BlankLine
      ShowBold "  You have already run SETUP on this computer to install this software."
      ShowBold "  You may want to run 'SETUP --clean $PRODUCT' to delete the directory"
      ShowBold "  ${BUILD_DIR}."
      BlankLine
      Pause
    fi
  else
    BlankLine
  fi
  
  for x in ./Build-Tree ./Build-Tree/$PLATFORM ${BUILD_DIR} ${BUILD_DIR}/bin ${BUILD_DIR}/lib ; do
    if [ ! -d "$x" ]; then
      mkdir $x
    fi
  done
  
  ShowNormal "  Building                 : SETUP modules"
  Module "printf" 	"c_bin" 	"printf binary for use with make config"
  Module "SETUP"  	"other" 	"SETUP support scripts"
  Module "Catalogue"	"c_lib"		"Message catalogue support library"
  
  echo "#ifndef __SETUP_HEADER"			>  ${BUILD_DIR}/SETUP/SETUP.h
  echo "#define __SETUP_HEADER"			>> ${BUILD_DIR}/SETUP/SETUP.h
  echo						>> ${BUILD_DIR}/SETUP/SETUP.h
  echo "/*"					>> ${BUILD_DIR}/SETUP/SETUP.h
  echo " * SETUP.h"				>> ${BUILD_DIR}/SETUP/SETUP.h
  echo " * Variables created by SETUP."		>> ${BUILD_DIR}/SETUP/SETUP.h
  echo " *"					>> ${BUILD_DIR}/SETUP/SETUP.h
  echo " * Automatically generated file."	>> ${BUILD_DIR}/SETUP/SETUP.h
  echo " * EDIT AT YOUR OWN RISK"		>> ${BUILD_DIR}/SETUP/SETUP.h
  echo " */"					>> ${BUILD_DIR}/SETUP/SETUP.h
  echo						>> ${BUILD_DIR}/SETUP/SETUP.h
  
  for x in ${GENERIC} ${GEN_MAJOR} ${PLATFORM} ${UNIX_CLASS} ; do
    echo "#ifndef ${x}"				>> ${BUILD_DIR}/SETUP/SETUP.h
    echo "  #define ${x} 1"			>> ${BUILD_DIR}/SETUP/SETUP.h
    echo "#endif /* ${x} */"			>> ${BUILD_DIR}/SETUP/SETUP.h
    echo					>> ${BUILD_DIR}/SETUP/SETUP.h
  done
  
  echo "#define SETUP_SHELL		${OUR_SHELL}"				>> ${BUILD_DIR}/SETUP/SETUP.h
  echo "  /* The /bin/sh-compatible shell recommended by SETUP */"		>> ${BUILD_DIR}/SETUP/SETUP.h
  echo "#define szAuthorName		${AUTHOR_NAME}"				>> ${BUILD_DIR}/SETUP/SETUP.h
  echo "#define szAuthorEmail		${AUTHOR_EMAIL}"			>> ${BUILD_DIR}/SETUP/SETUP.h
  echo "  /* The name and email address of the product's author */"		>> ${BUILD_DIR}/SETUP/SETUP.h
  echo "#define szTitle			${TITLE}"				>> ${BUILD_DIR}/SETUP/SETUP.h
  echo "#define szVersion		${VERSION}"				>> ${BUILD_DIR}/SETUP/SETUP.h
  echo "#define szReleaseDate		${RELEASEDATE}"				>> ${BUILD_DIR}/SETUP/SETUP.h
  echo "#define szCopyright		${COPYRIGHT}"				>> ${BUILD_DIR}/SETUP/SETUP.h
  echo "#define szPreviousVersion	${PREVIOUS_RELEASE}"			>> ${BUILD_DIR}/SETUP/SETUP.h
  if [ "${BUILD_NO}" ]; then
    echo "#define szBuildNo		${BUILD_NO}"				>> ${BUILD_DIR}/SETUP/SETUP.h
    echo "#define DEBUG			1"					>> ${BUILD_DIR}/SETUP/SETUP.h
    echo "  /* This is a test release, so we enable debugging by default */"	>> ${BUILD_DIR}/SETUP/SETUP.h
  fi
  echo										>> ${BUILD_DIR}/SETUP/SETUP.h
  echo "#endif /* __SETUP_HEADER */"						>> ${BUILD_DIR}/SETUP/SETUP.h
  
  BlankLine
  ShowNormal "  Building                 : Product modules"
  . ${PRODUCT_DIR}/${PRODUCT}.mod
  BlankLine
  
  ShowNormal "  Building                 : Main Makefile"
  
  echo "# Makefile variables"				>  ${BUILD_DIR}/SETUP/Makefile.in
  echo "#"						>> ${BUILD_DIR}/SETUP/Makefile.in
  echo "# AR		- library archiver for C"	>> ${BUILD_DIR}/SETUP/Makefile.in
  echo "# CC		- ANSI C compiler"		>> ${BUILD_DIR}/SETUP/Makefile.in
  echo "# CXX		- ANSI C++ compiler"		>> ${BUILD_DIR}/SETUP/Makefile.in
  echo "# CFLAGS	- flags for the C compiler"	>> ${BUILD_DIR}/SETUP/Makefile.in
  echo							>> ${BUILD_DIR}/SETUP/Makefile.in

  DONE="0"
  for x in $PLATFORM $GEN_MAJOR $GENERIC $UNIX_CLASS ; do
    if [ -f "${PLATFORM_DIR}/$x/Makefile.in" -a "$DONE" != "1" ]; then
      echo "# defaults supplied for this platform"	>> ${BUILD_DIR}/SETUP/Makefile.in
      echo						>> ${BUILD_DIR}/SETUP/Makefile.in
      cat ${PLATFORM_DIR}/$x/Makefile.in		>> ${BUILD_DIR}/SETUP/Makefile.in
      DONE=1
    fi
  done
  
  if [ "$DONE" = "0" ]; then
    echo "# no defaults found for this version of UNIX - using basic settings"	>> ${BUILD_DIR}/SETUP/Makefile.in
    echo						>> ${BUILD_DIR}/SETUP/Makefile.in
    cat ${PLATFORM_DIR}/defaults/Makefile.in		>> ${BUILD_DIR}/SETUP/Makefile.in
  fi
  
  echo "#"						>> ${BUILD_DIR}/SETUP/Makefile.in
  echo "# More variables supplied by SETUP"		>> ${BUILD_DIR}/SETUP/Makefile.in
  echo "#"						>> ${BUILD_DIR}/SETUP/Makefile.in
  echo "INCLUDE = -I../"				>> ${BUILD_DIR}/SETUP/Makefile.in
  if [ -n "${BUILD_NO}" ]; then
    echo "#"						>> ${BUILD_DIR}/SETUP/Makefile.in
    echo "# This is a test release, and so we use debugging flags"	>> ${BUILD_DIR}/SETUP/Makefile.in
    echo "#"						>> ${BUILD_DIR}/SETUP/Makefile.in
    echo "CFLAGS =\$(INCLUDE) \$(C_DEBUG)"		>> ${BUILD_DIR}/SETUP/Makefile.in
    echo "CXXFLAGS = \$(INCLUDE) \$(CXX_DEBUG)"		>> ${BUILD_DIR}/SETUP/Makefile.in
  else
    echo "#"						>> ${BUILD_DIR}/SETUP/Makefile.in
    echo "# This is a production release, and so we use optimising flags"	>> ${BUILD_DIR}/SETUP/Makefile.in
    echo "#"						>> ${BUILD_DIR}/SETUP/Makefile.in
    echo "CFLAGS = \$(INCLUDE) \$(C_OPTIMIZE)"		>> ${BUILD_DIR}/SETUP/Makefile.in
    echo "CXXFLAGS = \$(INCLUDE) \$(CXX_OPTIMIZE)"	>> ${BUILD_DIR}/SETUP/Makefile.in
  fi
  echo							>> ${BUILD_DIR}/SETUP/Makefile.in
  
  echo "# Main Makefile"				>  ${BUILD_DIR}/Makefile
  echo "#"						>> ${BUILD_DIR}/Makefile
  echo "# Generated by SETUP"				>> ${BUILD_DIR}/Makefile
  echo							>> ${BUILD_DIR}/Makefile
  echo "# Some default variables"			>> ${BUILD_DIR}/Makefile
  echo "include SETUP/Makefile.in"			>> ${BUILD_DIR}/Makefile
  echo							>> ${BUILD_DIR}/Makefile
  
  if [ -n "${BUILD_MODULE}" ]; then
    echo "BUILD_MODULE = \\"				>> ${BUILD_DIR}/Makefile
    for x in ${BUILD_MODULE} ; do
      echo "               $x \\"			>> ${BUILD_DIR}/Makefile
    done
    echo						>> ${BUILD_DIR}/Makefile
    
    echo "build: \$(BUILD_MODULE)"			>> ${BUILD_DIR}/Makefile
    echo						>> ${BUILD_DIR}/Makefile
    
    for x in ${BUILD_MODULE} ; do
      echo "$x: FORCE"					>> ${BUILD_DIR}/Makefile
      echo "	set -e; cd $x ; make build"		>> ${BUILD_DIR}/Makefile
      echo						>> ${BUILD_DIR}/Makefile
    done
  else
    echo "build: FORCE"					>> ${BUILD_DIR}/Makefile
    echo "	@echo \"There is nothing to compile.\""	>> ${BUILD_DIR}/Makefile
  fi
  
  if [ -n "${CONFIGURE_MODULE}" ]; then
    echo "CONFIGURE_MODULE = \\"			>> ${BUILD_DIR}/Makefile
    for x in ${CONFIGURE_MODULE} ; do
      echo "                   $x \\"			>> ${BUILD_DIR}/Makefile
    done
    echo						>> ${BUILD_DIR}/Makefile
    
    echo "config: printf"				>> ${BUILD_DIR}/Makefile
    echo "	set -e ; for x in \$(CONFIGURE_MODULE) ; do \\"	>> ${BUILD_DIR}/Makefile
    echo "	cd \$\$x ; make config ; cd .. ; done"	>> ${BUILD_DIR}/Makefile
    echo						>> ${BUILD_DIR}/Makefile
  else
    echo "config: FORCE"				>> ${BUILD_DIR}/Makefile
    echo "	@echo \"There is nothing to configure.\""	>> ${BUILD_DIR}/Makefile
    echo						>> ${BUILD_DIR}/Makefile
  fi
  
  if [ -n "${CATALOGUE_MODULE}" ]; then
    echo "CATALOGUE_MODULE = \\"			>> ${BUILD_DIR}/Makefile
    for x in ${CATALOGUE_MODULE} ; do
      echo "                   $x \\"			>> ${BUILD_DIR}/Makefile
    done
    echo						>> ${BUILD_DIR}/Makefile
    
    echo "catalogue: Catalogue"				>> ${BUILD_DIR}/Makefile
    echo "	set -e ; for x in \$(CATALOGUE_MODULE) ; do \\"	>> ${BUILD_DIR}/Makefile
    echo "	cd \$\$x ; make catalogue ; cd .. ; done"	>> ${BUILD_DIR}/Makefile
    echo						>> ${BUILD_DIR}/Makefile
  else
    echo "catalogue: FORCE"				>> ${BUILD_DIR}/Makefile
    echo "	@echo \"There are no message catalogues.\""	>> ${BUILD_DIR}/Makefile
    echo						>> ${BUILD_DIR}/Makefile
  fi
  
  if [ -n "${INSTALL_MODULE}" ]; then
    echo "INSTALL_MODULE = \\"				>> ${BUILD_DIR}/Makefile
    for x in ${CONFIGURE_MODULE} ; do
      echo "                 $x \\"			>> ${BUILD_DIR}/Makefile
    done
    echo						>> ${BUILD_DIR}/Makefile
    
    echo "install: FORCE"				>> ${BUILD_DIR}/Makefile
    echo "	set -e ; for x in \$(INSTALL) ; do \\"	>> ${BUILD_DIR}/Makefile
    echo "	cd \$x ; make install ; cd .. ; done"	>> ${BUILD_DIR}/Makefile
    echo						>> ${BUILD_DIR}/Makefile
  else
    echo "install: FORCE"				>> ${BUILD_DIR}/Makefile
    echo "	@echo \"There is nothing to install.\""	>> ${BUILD_DIR}/Makefile
    echo						>> ${BUILD_DIR}/Makefile
  fi
  
  echo "FORCE:"						>> ${BUILD_DIR}/Makefile
  echo							>> ${BUILD_DIR}/Makefile
  
  BlankLine
  Separator
  Pause
}

############################################################################
function StepFour ()
{
  clear
  ShowBold "Step Four : Configure The Source Code"
  Separator
  BlankLine
  
  if [ -n "${CONFIGURE_MODULE}" ]; then
    ShowNormal "  Directory            : ${BUILD_DIR}"
    ShowNormal "  Command              : make config"
    BlankLine
    cd ${BUILD_DIR}
    make config
    cd ${TOP_DIR}
  else
    echo "  No configuration is required."
  fi
 
  BlankLine
  Separator
  Pause
}

############################################################################
function StepFive ()
{
  clear
  ShowBold "Step Five : Compile The Source Code"
  Separator
  BlankLine
  
  if [ -n "${BUILD_MODULE}" ]; then
    ShowNormal "  Directory            : ${BUILD_DIR}"
    cd ${BUILD_DIR}
    ShowNormal "  Command              : make catalogue"
    if [ -n "${CATALOGUE_MODULE}" ]; then
      make catalogue
      ShowNormal "  Result               : Okay"
    else
      ShowNormal "  Result               : This product does not use message catalogues."
    fi
    ShowNormal "  Command              : make build"
    BlankLine
    make build
    cd ${TOP_DIR}
  else
    echo "There is no source code which requires compiling."
  fi
  
  BlankLine
  Separator
  Pause
}

############################################################################
function StepSix ()
{
  clear
  ShowBold "Step Six : Install The Product"
  Separator
  BlankLine
  
  if [ -n "${INSTALL_MODULE}" ]; then
    ShowNormal "  Directory            : ${BUILD_DIR}"
    cd ${BUILD_DIR}
    ShowNormal "  Command              : make install"
    BlankLine
    make install
    cd ${TOP_DIR}
  else
    ShowNormal "  There is nothing which can be installed by SETUP."
  fi
  
  BlankLine
  Separator
  Pause
}

##########################################################################
function StepEnd ()
{
  clear
  ShowBold "SETUP Is Complete"
  Separator
  BlankLine
  
  ShowNormal "SETUP has completed its work."
  BlankLine
  ShowNormal "  *  SETUP has placed the source code is in the directory"
  ShowNormal "     ${BUILD_DIR}."
  BlankLine
  ShowNormal "     SETUP has created a complete set of makefiles, so that you can"
  ShowNormal "     reconfigure, recompile, and reinstall this product yourself at"
  ShowNormal "     any time in the future."
  BlankLine
  
  if [ -f "${BUILD_DIR}/INSTALL.logfile" ]; then
    ShowNormal "  *  Please read the file 'INSTALL.logfile' in the directory"
    ShowNormal "     ${BUILD_DIR}."
    ShowNormal "     It was created during the installation of this product."
    BlankLine
  fi
  
  Separator
  ShowBold "Thank you for using SETUP."
  Separator
  BlankLine
}

############################################################################
# Author - show name of package author
# $1 - author name
# $2 - author email address

function Author ()
{
  ShowNormal "  Author               : $1"
  if [ -n "$2" ]; then
    ShowNormal "  Email                : $2"
  fi
  AUTHOR_NAME="$1"
  AUTHOR_EMAIL="$2"
}

############################################################################
# BlankLine - show a blank line
# No parameters

function BlankLine ()
{
  echo
}

############################################################################
# BuildNo - give the current build number

function BuildNo ()
{
  ShowBold "    Build Number   (*) : $1"
  BUILD_NO=$1
  VERSION="${VERSION}$1"
}

############################################################################
# Comment - display a comment
# $1 - the comment to show

function Comment ()
{
  ShowNormal ": $1"
}

############################################################################
# Copyright - display copyright information
# $1..$n - copyright lines

function Copyright ()
{
  COPYRIGHT=
  while [ -n "$1" ]; do
    ShowNormal "  Copyright            : (c) $1"
    COPYRIGHT="${COPYRIGHT}Copyright (c) $1\n"
    shift
  done
 ShowNormal "  Copyright            : All rights reserved."
 COPYRIGHT="${COPYRIGHT}All rights reserved."
}

############################################################################
# Module
# $1 - module dir name
# $2 - module type
# $3 - module comment

function Module ()
{
  local x
  local y
  local z
  
  local IS_C
  local IS_CC
  local OBJS
  
  ShowNormal "    Module                 : $1 ($2)"
  ShowNormal "      Description          : $3"
  
  if [ ! -d "./Source-Tree/$1" ]; then
    BlankLine
    ShowBold "  WARNING: The module '$1' cannot be found."
    BlankLine
    Pause
  fi
  
  if [ ! -d "${BUILD_DIR}/$1" ]; then
    mkdir "${BUILD_DIR}/$1"
  fi
  
  if [ -f "${BUILD_DIR}/$1/Makefile" ]; then
    rm -f "${BUILD_DIR}/$1/Makefile"
  fi
  
  for x in "./Source-Tree/$1/all-systems" "./Source-Tree/$1/${UNIX_CLASS}" "./Source-Tree/$1/${GENERIC}" "./Source-Tree/$1/${GEN_MAJOR}" "./Source-Tree/$1/${PLATFORM}" ; do
    if [ -d "$x" ]; then
      cd "$x"
      for z in `echo *` ; do
        if [ -f "$z" ]; then
          ln -sf "../../../../$x/$z" "${TOP_DIR}/${BUILD_DIR}/$1"
	  case "$z" in
	    *.c)
	      y=`basename $z .c`
	      echo "$y.o: $z"					>> "${TOP_DIR}/${BUILD_DIR}/$1/Makefile.tmp"
	      echo "	\$(CC) \$(CFLAGS) -c $z -o $y.o"	>> "${TOP_DIR}/${BUILD_DIR}/$1/Makefile.tmp"
	      echo						>> "${TOP_DIR}/${BUILD_DIR}/$1/Makefile.tmp"
	      OBJS="${OBJS}$y.o "
	      ;;
	    *.cc)
	      y=`basename $z .cc`
	      echo "$y.o: $z"					>> "${TOP_DIR}/${BUILD_DIR}/$1/Makefile.tmp"
	      echo "	\$(CXX) \$(CXXFLAGS) -c $z -o $y.o"	>> "${TOP_DIR}/${BUILD_DIR}/$1/Makefile.tmp"
	      echo						>> "${TOP_DIR}/${BUILD_DIR}/$1/Makefile.tmp"
	      OBJS="${OBJS}$y.o "
	      ;;
	    Configure)
	      echo "config:"					>> "${TOP_DIR}/${BUILD_DIR}/$1/Makefile.tmp"
	      echo "	@../SETUP/Configure.sh  --input Configure --output-header autoconf.h"	>> "${TOP_DIR}/${BUILD_DIR}/$1/Makefile.tmp"
	      echo						>> "${TOP_DIR}/${BUILD_DIR}/$1/Makefile.tmp"
	      CONFIGURE_MODULE="${CONFIGURE_MODULE}$1 "
	      ;;
	    Catalogue)
	      echo "catalogue:"					>> "${TOP_DIR}/${BUILD_DIR}/$1/Makefile.tmp"
	      echo "	@../Catalogue/Catalogue.sh"		>> "${TOP_DIR}/${BUILD_DIR}/$1/Makefile.tmp"
	      echo						>> "${TOP_DIR}/${BUILD_DIR}/$1/Makefile.tmp"
	      CATALOGUE_MODULE="${CATALOGUE_MODULE}$1 "
	      ;;
	    Install)
	      echo "install:"					>> "${TOP_DIR}/${BUILD_DIR}/$1/Makefile.tmp"
	      echo "	@../SETUP/Install.sh"			>> "${TOP_DIR}/${BUILD_DIR}/$1/Makefile.tmp"
	      echo						>> "${TOP_DIR}/${BUILD_DIR}/$1/Makefile.tmp"
	      INSTALL_MODULE="${INSTALL_MODULE}$1 "
	      ;;
	  esac
	fi
      done
      for z in echo *.sh ; do
	if [ -f "$z" ]; then
	  if [ -s "${TOP_DIR}/${BUILD_DIR}/$1/$z" ]; then
	    rm -f "${TOP_DIR}/${BUILD_DIR}/$1/$z"
	  fi
	  echo "#! ${OUR_SHELL}" 			>  "${TOP_DIR}/${BUILD_DIR}/$1/$z"
	  echo "# ** SHELL DETERMINED BY SETUP **" 	>> "${TOP_DIR}/${BUILD_DIR}/$1/$z"
	  cat $z					>> "${TOP_DIR}/${BUILD_DIR}/$1/$z"
	  chmod 755 "${TOP_DIR}/${BUILD_DIR}/$1/$z"
        fi
      done
      cd "${TOP_DIR}"
    fi
  done
  
  if [ -f "${BUILD_DIR}/$1/Makefile.tmp" ]; then
    BUILD_MODULE="${BUILD_MODULE} $1"
    
    echo "# Module $1"							>> "${BUILD_DIR}/$1/Makefile"
    echo "# $3"								>> "${BUILD_DIR}/$1/Makefile"
    echo "#"								>> "${BUILD_DIR}/$1/Makefile"
    echo "# Makefile produced automatically by SETUP."			>> "${BUILD_DIR}/$1/Makefile"
    echo								>> "${BUILD_DIR}/$1/Makefile"
    echo "include ../SETUP/Makefile.in"					>> "${BUILD_DIR}/$1/Makefile"
    echo								>> "${BUILD_DIR}/$1/Makefile"
    echo "OBJS = \\"							>> "${BUILD_DIR}/$1/Makefile"
    for x in $OBJS ; do
      echo "       $x \\"						>> "${BUILD_DIR}/$1/Makefile"
    done
    echo								>> "${BUILD_DIR}/$1/Makefile"
  
    echo "build: $1"							>> "${BUILD_DIR}/$1/Makefile"
    echo								>> "${BUILD_DIR}/$1/Makefile"
    echo "$1: \$(OBJS)"							>> "${BUILD_DIR}/$1/Makefile"
  
    case "$2" in
      c_bin)
        echo "	\$(CC) \$(LDCFLAGS) -o $1 \$(OBJS) \$(LIBS)"		>> "${BUILD_DIR}/$1/Makefile"
	echo "	ln -f $1 ../bin"					>> "${BUILD_DIR}/$1/Makefile"
        ;;
      cc_bin)
        echo "	\$(CXX) \$(LDCXXFLAGS) -o ../bin/$1 \$(OBJS) \$(LIBS)"	>> "${BUILD_DIR}/$1/Makefile"
	echo "	ln -f $1 ../bin"					>> "${BUILD_DIR}/$1/Makefile"
        ;;
      c_lib)
        echo "	\$(AR) lib$1.a \$(OBJS)"				>> "${BUILD_DIR}/$1/Makefile"
	echo "	ln -f lib$1.a ../lib"					>> "${BUILD_DIR}/$1/Makefile"
        ;;
      cc_lib)
        echo "	\$(CXXAR) ../lib/lib$1.a \$(OBJS)"			>> "${BUILD_DIR}/$1/Makefile"
	echo "	ln -f lib$1.a ../lib"					>> "${BUILD_DIR}/$1/Makefile"
        ;;
      *)
        ;;
    esac
    echo							>> "${BUILD_DIR}/$1/Makefile"
    cat < "${BUILD_DIR}/$1/Makefile.tmp" >> "${BUILD_DIR}/$1/Makefile"
    rm -f "${BUILD_DIR}/$1/Makefile.tmp"
  fi
}

############################################################################
# Pause

function Pause ()
{
  ShowUl "Press ENTER to continue, or CTRL-C to quit."
  read < /dev/tty
}

############################################################################
# PreviousRelease - record the last production version of the software
# $1 - the version string

function PreviousRelease ()
{
  ShowNormal "  Previous Version     : $1"
  PREVIOUS_RELEASE="$1"
}

############################################################################
# Product - display the product name
# $1 - the product name

function Product ()
{
  ShowNormal "  Product              : $1"
  ShowNormal "    Description        : $2"
  BlankLine
}

############################################################################
# ReleaseDate
# $1:

function ReleaseDate ()
{
  ShowNormal "  Released             : $1"
  RELEASEDATE="$1"
}

############################################################################
# Separator - display a single line separator
# No parameters

function Separator ()
{
  ShowNormal "--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--"
}

############################################################################
# Title - display the package title
# $1...$n : Each line of the title

function Title ()
{
  TITLE=
  while [ "$1x" != "x" ]; do
    ShowNormal "  Title                : $1"
    TITLE="${TITLE}$1\n"
    shift
  done
}

##########################################################################
# Version - show version information
# $1 - product version

function Version ()
{
  ShowNormal "  Version              : $1"
  VERSION="$1"
}

##########################################################################
# main - it all happens here

function main ()
{
  # do we have a directory with product info in?
  
  if [ ! -d "$PRODUCT_DIR" ]; then
    echo "SETUP: Nothing to do - no product information."
    exit 1
  fi
  
  # how many products do we have available?
  
  if [ -z "$1" -o "$1" = "INDEX" -o "$1" = "PRODUCT" -o "$1" = "OPTION" ]; then
    echo "SETUP: Products available are :"
    echo
    cat ${PRODUCT_DIR}/OPTION
    echo "Type 'SETUP <number>' to install the product."
    exit 1
  else
    if [ -f "${PRODUCT_DIR}/$1" ]; then
      clear
      ShowBold "SETUP v2.0 (July 1995)"
      Separator
      BlankLine
      ShowNormal "Welcome to SETUP."
      BlankLine
      ShowNormal "SETUP will guide you through the following steps :"
      BlankLine
      ShowNormal "  1.  Examine this machine."
      ShowNormal "  2.  Examine this product."
      ShowNormal "  3.  Create a source tree."
      ShowNormal "  4.  Configure the source code."
      ShowNormal "  5.  Compile the source code."
      ShowNormal "  6.  Install this product."
      BlankLine
      ShowNormal "For more information about SETUP, please read the file"
      ShowNormal "./Source-Tree/SETUP/all-systems/SETUP.paper, or see the URL"
      BlankLine
      ShowNormal "        http://myrddraal.shef.ac.uk/Computers/OS/UNIX/Software/"
      ShowNormal "        Development/Packaging/SETUP/"
      BlankLine
      Separator
      Pause
      
      PRODUCT="$1"
      StepOne
      StepTwo
      StepThree
      StepFour
      StepFive
      StepSix
      StepEnd
    fi
  fi
}

##########################################################################

main "$@"