#		-*- sh -*-
#
# SETUP v3
# Source Tree Builder And Compiler
# Developed for Generic NQS v3.50.0
#
# Copyright (c) 1995 The University of Sheffield
# Released under v2 of the GNU GPL
#
# Author(s) :
#
# Stuart Herbert
# Academic Computing Services
# The University of Sheffield
#
############################################################################
# WARNING
#
# To use SETUP, your computer's Bourne Shell (/bin/sh) must support shell
# scripts that define functions.  If your computer's Bourne Shell doesn't
# do this, you should modify the first line of this script to point to
# either /bin/ksh (Korn Shell) or to bash (GNU shell).
#
############################################################################

umask 022

############################################################################
# GLOBAL VARIABLES
############################################################################

TOPDIR="`pwd`"
export TOPDIR
SETUPDIR="$TOPDIR/SETUPDir"
SCRIPTSDIR="${SETUPDIR}/Scripts"
PRODUCTDIR="${SETUPDIR}/Product"
PLATFORMSDIR="${SETUPDIR}/Platforms"

COPYRIGHT_VAR="x"
COPYRIGHTEND_VAR="Released under version 2 of the GNU General Public License."
SUPPORTED="No"

which_path="x"

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

String () {
  GetString "$1" "$2" "$3" "$2" "SETUPDir/Scripts/"
}

which () {
  if [ "$which_path" = "x" ]; then
    which_path="`echo $PATH | tr ':' ' '`"
  fi

  which_y="x"
  
  for which_x in $which_path ; do
    if [ -f "$x/$1" -a ! "$which_y" = "x" ]; then
      echo "$x/$1"
      which_y="n"
    fi
  done
  
  echo "$2"
}

############################################################################
# MACHINE DESCRIPTION FUNCTIONS
############################################################################

AR () {
  AR_VAR="$1"
}

C_COMPILER () {
  C_COMPILER_VAR="$1"
}

C_COMPILER_FLAGS_OPTIMISE () {
  C_COMPILER_FLAGS_OPTIMISE_VAR="$1"
}

C_COMPILER_FLAGS_DEBUG () {
  C_COMPILER_FLAGS_DEBUG_VAR="$1"
}

C_COMPILER_FLAGS_COMMON () {
  C_COMPILER_FLAGS_COMMON_VAR="$1"
}

C_COMPILER_FLAGS_INCLUDE () {
  C_COMPILER_FLAGS_INCLUDE_VAR="$1"
}

CONFIG_MACHINE () {
  CONFIG_MACHINE_VAR="$1"
}

LINKER () {
  LINKER_VAR="$1"
}

LINKER_INCLUDE () {
  LINKER_INCLUDE_VAR="$1"
}

LINKER_LIBS () {
  LINKER_LIBS_VAR="$1"
}

INCLUDE () {
  if [ -f "${PLATFORMSDIR}/$1" ]; then
    . ${PLATFORMSDIR}/$1
  fi
}

PLATFORM_FLAGS () {
  PLATFORM_FLAGS_VAR="$1"
}

PREFERED_API () {
  PREFERED_API_VAR="$1"
}

RANLIB () {
  RANLIB_VAR="$1"
}

LoadMachineDesc () {
  showblink "  Examining your computer ... please wait."
  echo
  CONFIG_MACHINE_VAR="$machine"

  if [ -f "${PLATFORMSDIR}/${machine}" ]; then
    SUPPORTED="Yes"
    . ${PLATFORMSDIR}/${machine}
  fi

  HOSTNAME="`(uname -n)`" || HOSTNAME="`(hostname)`" || HOSTNAME="unknown"
  OS="`echo $machine | cut -d- -f3`"
  OUR_PATH="`echo $PATH | tr ':' ' '`"
  
  UNIX_SHELL="x"
  
  for x in bash ksh ; do
    for y in $OUR_PATH ; do
      if [ -f "$y/$x" -a "$UNIX_SHELL" = "x" ]; then
        UNIX_SHELL="$y/$x"
      fi
    done
  done
  if [ "$UNIX_SHELL" = "x" ]; then
    UNIX_SHELL="/bin/sh"
  fi
  export UNIX_SHELL
  REL_BUILDDIR="Build-Tree/$machine/$HOSTNAME"
  BUILDDIR="$TOPDIR/$REL_BUILDDIR"
}

############################################################################
# PRODUCT DESCRIPTION FUNCTIONS
############################################################################

LoadProductDesc () {
  if [ -f "./SETUPDir/Product/DESC" ]; then
    . ./SETUPDir/Product/DESC
  fi
}

############################################################################
# SOURCE-TREE BUILDING FUNCTIONS
############################################################################

# $1 - module dir name
# $2 - module type
# $3 - module description

MODULE () {
  if [ ! -d "Source-Tree/$1" ]; then
    echo
    echo "  The list of product components is wrong; I cannot find the"
    echo "  component called '$1'."
    echo
    pause
  else
    echo "  Creating Module   : $1"
    echo "    Description     : $3"
  
    if [ ! -d $BUILDDIR/$1 ]; then
      mkdir $BUILDDIR/$1
    fi
    SubLibs=". $4"
    
    for y in $SubLibs ; do
      if [ ! -d "$BUILDDIR/$1/$y" ]; then
        mkdir $BUILDDIR/$1/$y
      fi
    done
    
    for y in $SubLibs ; do
      for x in $machine $PREFERED_API_VAR all-systems ; do
        if [ -d "Source-Tree/$1/$y/$x" ]; then
          find Source-Tree/$1/$y/$x -type f -exec $UNIX_SHELL $SCRIPTSDIR/CopyFile $BUILDDIR/$1 {} $y $1 \;
        fi
      done
    done
    
    MakeMakefiles "$1" "$2" "$3"
    if [ "$2" = "c_lib" -o "$2" = "cxx_lib" ]; then
      PRODUCT_LIBS="-l`echo $1 | cut -c4-` ${PRODUCT_LIBS}"
    fi
  fi
}

CreateBuildDir () {
  echo "  I will put the source code in $REL_BUILDDIR"
  if [ -d "$BUILDDIR" ]; then
    echo
    echo "  You have already compiled this product on this machine."
    echo "  I have removed the previous compiled version."
    rm -rf "$REL_BUILDDIR"
    
    if [ -d "$BUILDDIR" ]; then
      echo
      showbold "  *** ERROR: Unable to remove the previous compiled version."
      echo     "  Please remove $REL_BUILDDIR by hand, and then press"
      echo     "  ENTER to continue."
      echo
      pause
    fi
  fi

  echo
  for x in Build-Tree Build-Tree/$machine Build-Tree/$machine/$HOSTNAME $BUILDDIR/bin $BUILDDIR/lib ; do
    if [ ! -d $x ]; then
      mkdir $x
    fi
  done
}

MakeAutoconfHeader () {
  WorkingDir=$BUILDDIR/SETUP
  if [ -f "$WorkingDir/SETUP.h" ]; then
    rm -f "$WorkingDir/SETUP.h"
  fi
  
  echo "#define AUTHOR  	\"$AUTHOR\""		>> $WorkingDir/SETUP.h
  echo "#define AUTHOR_EMAIL	\"$AUTHOR_EMAIL\""	>> $WorkingDir/SETUP.h
  echo "#define HOST		\"$machine\""		>> $WorkingDir/SETUP.h
  echo "#define PRODUCT 	\"$PRODUCT\""		>> $WorkingDir/SETUP.h
  echo "#define VERSION 	\"$VERSION\""		>> $WorkingDir/SETUP.h
  
  echo "AUTHOR=\"$AUTHOR\""				>> $WorkingDir/SETUP.def
  echo "AUTHOR_EMAIL=\"$AUTHOR_EMAIL\""			>> $WorkingDir/SETUP.def
  echo "HOST=\"$machine\""				>> $WorkingDir/SETUP.def
  echo "PRODUCT=\"$PRODUCT\""				>> $WorkingDir/SETUP.def
  echo "VERSION=\"$VERSION\""				>> $WorkingDir/SETUP.def
  echo "SETUP_COMPONENT=\"$SETUP_COMPONENT\""		>> $WorkingDir/SETUP.def
  echo "CONFIG_MACHINE_VAR=\"$CONFIG_MACHINE_VAR\""	>> $WorkingDir/SETUP.def
  echo "PRINTFDIR=\"$PRINTFDIR\""			>> $WorkingDir/SETUP.def
  echo "machine=\"$machine\""				>> $WorkingDir/SETUP.def
}

MakeMakefile () {
  echo
  echo "  Building          : Main Makefile"
  
  echo "# Makefile for $PRODUCT $VERSION"			>> $BUILDDIR/Makefile
  echo "# Maintained by $AUTHOR ($AUTHOR_EMAIL)"		>> $BUILDDIR/Makefile
  echo								>> $BUILDDIR/Makefile
  if [ -f "$BUILDDIR/Makefile.build" ]; then
    echo "BUILD = \\"						>> $BUILDDIR/Makefile
    cat $BUILDDIR/Makefile.build				>> $BUILDDIR/Makefile
    echo "	GNQS.VERSION.bin \\"				>> $BUILDDIR/Makefile
    echo "  SETUP.DUMMY"					>> $BUILDDIR/Makefile
    echo " "							>> $BUILDDIR/Makefile
  fi
  
  if [ -f "$BUILDDIR/Makefile.configure" ]; then
    echo "CONF = \\"						>> $BUILDDIR/Makefile
    cat $BUILDDIR/Makefile.configure				>> $BUILDDIR/Makefile
    echo "  SETUP.DUMMY"					>> $BUILDDIR/Makefile
    echo " "							>> $BUILDDIR/Makefile
  fi
  
  if [ -f "$BUILDDIR/Makefile.install" ]; then
    echo "INST = \\"						>> $BUILDDIR/Makefile
    cat $BUILDDIR/Makefile.install				>> $BUILDDIR/Makefile
    echo "  SETUP.DUMMY"					>> $BUILDDIR/Makefile
    echo " "							>> $BUILDDIR/Makefile
  fi
  
  echo "all: build"						>> $BUILDDIR/Makefile
  echo								>> $BUILDDIR/Makefile
  
  if [ -f "$BUILDDIR/Makefile.buildrules" ]; then
    echo "build: \$(BUILD)"					>> $BUILDDIR/Makefile
    echo							>> $BUILDDIR/Makefile
    echo "GNQS.VERSION.bin: FORCE"				>> $BUILDDIR/Makefile
    echo "	echo $VERSION > bin/GNQS.VERSION"		>> $BUILDDIR/Makefile
    echo							>> $BUILDDIR/Makefile
    cat $BUILDDIR/Makefile.buildrules				>> $BUILDDIR/Makefile
    rm -f "$BUILDDIR/Makefile.build"
    rm -f "$BUILDDIR/Makefile.buildrules"
  else
    echo "build: FORCE"						>> $BUILDDIR/Makefile
    echo "	@echo \"  Nothing to build for $PRODUCT $VERSION\""	>> $BUILDDIR/Makefile
  fi
  echo								>> $BUILDDIR/Makefile
  
  echo "clean: FORCE"						>> $BUILDDIR/Makefile
#  echo "	@echo \"  Removing compiled code.\""		>> $BUILDDIR/Makefile
  echo "	find . -name '*.o' -exec rm -f {} \;"		>> $BUILDDIR/Makefile
  echo "	find . -name '*~' -exec rm -f {} \;"		>> $BUILDDIR/Makefile
  echo "	find . -name '*.a' -exec rm -f {} \;"		>> $BUILDDIR/Makefile
  echo "	rm -f bin/* lib/*"				>> $BUILDDIR/Makefile
  echo								>> $BUILDDIR/Makefile

  if [ -f "$BUILDDIR/Makefile.configurerules" ]; then
    echo "config: \$(CONF)"					>> $BUILDDIR/Makefile
    echo							>> $BUILDDIR/Makefile
    cat $BUILDDIR/Makefile.configurerules			>> $BUILDDIR/Makefile
    rm -f "$BUILDDIR/Makefile.configure"
    rm -f "$BUILDDIR/Makefile.configurerules"
  else
    echo "config: FORCE"
    echo "	@echo \"  Nothing to configure for $PRODUCT $VERSION\""	>> $BUILDDIR/Makefile
  fi
  echo								>> $BUILDDIR/Makefile
  
  if [ -f "$BUILDDIR/Makefile.installrules" ]; then
    echo "install: \$(INST)"					>> $BUILDDIR/Makefile
    echo 							>> $BUILDDIR/Makefile
    cat $BUILDDIR/Makefile.installrules				>> $BUILDDIR/Makefile
    rm -f "$BUILDDIR/Makefile.install"
    rm -f "$BUILDDIR/Makefile.installrules"
  else
    echo "install: FORCE"					>> $BUILDDIR/Makefile
    echo "	@echo \"  Nothing to install for $PRODUCT $VERSION\""	>> $BUILDDIR/Makefile
  fi
  echo								>> $BUILDDIR/Makefile
  echo "FORCE:"							>> $BUILDDIR/Makefile
  echo								>> $BUILDDIR/Makefile
  echo "SETUP.DUMMY:"						>> $BUILDDIR/Makefile
  echo "	@echo \"  Target complete.\""			>> $BUILDDIR/Makefile
}
# $1 - the name of the module
# $2 - the type
# $3 - the description

MakeMakefiles () {
#  echo "    Building        : Makefile"
  WorkingDir="$BUILDDIR/$1"
  
  echo "######################################" 	>> $WorkingDir/Makefile
  echo "# $1"						>> $WorkingDir/Makefile
  echo "# $3"						>> $WorkingDir/Makefile
  echo "#"						>> $WorkingDir/Makefile
  echo "######################################" 	>> $WorkingDir/Makefile
  echo "# Makefile generated by SETUP v3"		>> $WorkingDir/Makefile
  echo "######################################" 	>> $WorkingDir/Makefile
  echo							>> $WorkingDir/Makefile
  echo "include ../SETUP/Makefile.inc"			>> $WorkingDir/Makefile

  if [ -f "$BUILDDIR/$1/Makefile.inc" ]; then
    echo "include Makefile.inc"				>> $WorkingDir/Makefile
  fi
  echo							>> $WorkingDir/Makefile
  
  if [ -f "$BUILDDIR/$1/Makefile.objs" ]; then
    echo "# List of object code files"			>> $WorkingDir/Makefile
    echo "OBJS = \\"					>> $WorkingDir/Makefile
    cat $BUILDDIR/$1/Makefile.objs 			>> $WorkingDir/Makefile
    echo " "						>> $WorkingDir/Makefile
    rm $WorkingDir/Makefile.objs
  fi
  
  if [ -f "$BUILDDIR/$1/Makefile.configure" ]; then
    echo "# List of configuration files"		>> $WorkingDir/Makefile
    echo "CONF = \\"					>> $WorkingDir/Makefile
    cat $WorkingDir/Makefile.configure			>> $WorkingDir/Makefile
    echo " "						>> $WorkingDir/Makefile
    rm $WorkingDir/Makefile.configure
  fi
    
  if [ -f "$BUILDDIR/$1/Makefile.install" ]; then
    echo "# List of installation files"			>> $WorkingDir/Makefile
    echo "INST = \\"					>> $WorkingDir/Makefile
    cat $WorkingDir/Makefile.install			>> $WorkingDir/Makefile
    echo " "						>> $WorkingDir/Makefile
    rm $WorkingDir/Makefile.install
  fi
  
  case "$2" in
    c_bin)
      echo "build: $1 \$(OBJS)"				>> $WorkingDir/Makefile
      echo						>> $WorkingDir/Makefile
      echo "$1: \$(OBJS)"				>> $WorkingDir/Makefile
#      echo "	@echo \"  Linking $1\""			>> $WorkingDir/Makefile
      echo "	\$(LD) -L../lib $LINKER_INCLUDE_VAR -o $1 \$(OBJS) $PRODUCT_LIBS \$(GPORT_READLINE_LIBS) $LINKER_LIBS_VAR"	>> $WorkingDir/Makefile
      echo "	-rm -f ../bin/$1"			>> $WorkingDir/Makefile
      echo "	ln $1 ../bin"				>> $WorkingDir/Makefile
      echo						>> $WorkingDir/Makefile
      echo "clean: FORCE"				>> $WorkingDir/Makefile
#      echo "	@echo \"  Cleaning $1\""		>> $WorkingDir/Makefile
      echo "	-rm -f \$(OBJS)"			>> $WorkingDir/Makefile
      echo "	-rm -f ../bin/$1"			>> $WorkingDir/Makefile
      echo						>> $WorkingDir/Makefile
      echo "	$1.bin \\"				>> $BUILDDIR/Makefile.build
      echo "$1.bin: FORCE"				>> $BUILDDIR/Makefile.buildrules
      echo "	cd $1; make ${MAKE_FLAGS} build"	>> $BUILDDIR/Makefile.buildrules
      echo						>> $BUILDDIR/Makefile.buildrules
      ;;
    c_bin_run)
      echo "build: $1"					>> $WorkingDir/Makefile
      echo						>> $WorkingDir/Makefile
      echo "$1: \$(OBJS)"				>> $WorkingDir/Makefile
#      echo "	@echo \"  Linking $1\""			>> $WorkingDir/Makefile
      echo "	\$(LD) -L../lib $LINKER_INCLUDE_VAR -o $1 \$(OBJS) $PRODUCT_LIBS \$(GPORT_READLINE_LIBS) $LINKER_LIBS_VAR"	>> $WorkingDir/Makefile
      echo "	-rm -f ../bin/$1"			>> $WorkingDir/Makefile
      echo "	ln $1 ../bin"				>> $WorkingDir/Makefile
      echo "	echo \"  Running $1\""			>> $WorkingDir/Makefile
      echo "	../bin/$1"				>> $WorkingDir/Makefile
      echo						>> $WorkingDir/Makefile
      echo "clean: FORCE"				>> $WorkingDir/Makefile
#      echo "	@echo \"  Cleaning $1\""		>> $WorkingDir/Makefile
      echo "	-rm -f \$(OBJS)"			>> $WorkingDir/Makefile
      echo "	-rm -f ../bin/$1"			>> $WorkingDir/Makefile
      echo						>> $WorkingDir/Makefile
      echo "	$1.bin \\"				>> $BUILDDIR/Makefile.build
      echo "$1.bin: FORCE"				>> $BUILDDIR/Makefile.buildrules
      echo "	cd $1; make ${MAKE_FLAGS} build"	>> $BUILDDIR/Makefile.buildrules
      echo						>> $BUILDDIR/Makefile.buildrules
      ;;
    c_lib)
      echo "build: $1.a"				>> $WorkingDir/Makefile
      echo						>> $WorkingDir/Makefile
      echo "$1.a: \$(OBJS)"				>> $WorkingDir/Makefile
#      echo "	@echo \"  Archiving $1\""		>> $WorkingDir/Makefile
      echo "	\$(AR) $1.a \$(OBJS)"			>> $WorkingDir/Makefile
      echo "	\$(RANLIB) $1.a \$(RANLIBEND)"		>> $WorkingDir/Makefile
      echo "	-rm -f ../lib/$1.a"			>> $WorkingDir/Makefile
      echo "	ln $1.a ../lib"				>> $WorkingDir/Makefile
      echo						>> $WorkingDir/Makefile
      echo "clean: FORCE"				>> $WorkingDir/Makefile
#      echo "	@echo \"  Cleaning $1\""		>> $WorkingDir/Makefile
      echo "	-rm -f \$(OBJS)"			>> $WorkingDir/Makefile
      echo "	-rm -f ../lib/$1.a"			>> $WorkingDir/Makefile
      echo						>> $WorkingDir/Makefile
      echo "	$1.lib \\"				>> $BUILDDIR/Makefile.build
      echo "$1.lib: FORCE"				>> $BUILDDIR/Makefile.buildrules
      echo "	cd $1; make ${MAKE_FLAGS} build"	>> $BUILDDIR/Makefile.buildrules
      echo						>> $BUILDDIR/Makefile.buildrules
      ;;
    cxx_bin)
      echo "build: $1"					>> $WorkingDir/Makefile
      echo						>> $WorkingDir/Makefile
      echo "$1: \$(OBJS)"				>> $WorkingDir/Makefile
#      echo "	@echo \"  Linking $1\""			>> $WorkingDir/Makefile
      echo "	\$(LD) -L../lib $LINKER_INCLUDE_VAR -o $1 \$(OBJS) $PRODUCT_LIBS \$(GPORT_READLINE_LIBS) $LINKER_LIBS_VAR"	>> $WorkingDir/Makefile
      echo "	-rm -f ../bin/$1"			>> $WorkingDir/Makefile
      echo "	ln $1 ../bin"				>> $WorkingDir/Makefile
      echo						>> $WorkingDir/Makefile
      echo "clean: FORCE"				>> $WorkingDir/Makefile
#      echo "	@echo \"  Cleaning $1\""		>> $WorkingDir/Makefile
      echo "	-rm -f \$(OBJS)"			>> $WorkingDir/Makefile
      echo "	-rm -f ../bin/$1"			>> $WorkingDir/Makefile
      echo						>> $WorkingDir/Makefile
      echo "	$1.bin \\"				>> $BUILDDIR/Makefile
      echo "$1.bin: FORCE"				>> $BUILDDIR/Makefile.buildrules
      echo "	cd $1; make build"			>> $BUILDDIR/Makefile.buildrules
      echo						>> $BUILDDIR/Makefile.buildrules
      ;;
    *)
      echo "build: FORCE"				>> $WorkingDir/Makefile
      echo "	@echo \"  Nothing to build for $1\""	>> $WorkingDir/Makefile
      echo						>> $WorkingDir/Makefile
      ;;
  esac
  
  echo "# Rules for configuration"			>> $WorkingDir/Makefile
  if [ -f "$BUILDDIR/$1/Makefile.conf" ]; then
    cat $WorkingDir/Makefile.conf			>> $WorkingDir/Makefile
    echo " "						>> $WorkingDir/Makefile
    echo "Configure: \$(CONF)"				>> $WorkingDir/Makefile
    echo 						>> $WorkingDir/Makefile
    rm -f $WorkingDir/Makefile.conf
    echo "	$1.conf \\"				>> $BUILDDIR/Makefile.configure
    echo "$1.conf: FORCE"				>> $BUILDDIR/Makefile.configurerules
    echo "	cd $1; make Configure"			>> $BUILDDIR/Makefile.configurerules
    echo						>> $BUILDDIR/Makefile.configurerules
  else
    echo "Configure: FORCE"				>> $WorkingDir/Makefile
    echo "	@echo \"  Nothing to configure for $1\""	>> $WorkingDir/Makefile
    echo						>> $WorkingDir/Makefile
  fi
  
  echo "# Rules for installation"			>> $WorkingDir/Makefile
  if [ -f "$BUILDDIR/$1/Makefile.inst" ]; then
    cat $WorkingDir/Makefile.inst			>> $WorkingDir/Makefile
    echo " "						>> $WorkingDir/Makefile
    echo "Install: \$(INST)"				>> $WorkingDir/Makefile
    echo						>> $WorkingDir/Makefile
    rm -f $WorkingDir/Makefile.inst
    echo "	$1.inst \\"				>> $BUILDDIR/Makefile.install
    echo "$1.inst: FORCE"				>> $BUILDDIR/Makefile.installrules
    echo "	cd $1; make Install"			>> $BUILDDIR/Makefile.installrules
    echo						>> $BUILDDIR/Makefile.installrules
  else
    echo "Install: FORCE"				>> $WorkingDir/Makefile
    echo "	@echo \"  Nothing to install for $1\""	>> $WorkingDir/Makefile
    echo						>> $WorkingDir/Makefile
  fi
    
  if [ -f "$BUILDDIR/$1/Makefile.c" ]; then
    echo "# Rules for C source code files"		>> $WorkingDir/Makefile
    cat $WorkingDir/Makefile.c				>> $WorkingDir/Makefile
    echo " "						>> $WorkingDir/Makefile
    rm $WorkingDir/Makefile.c
  fi
  
  if [ -f "$BUILDDIR/$1/Makefile.cc" ]; then
    echo "# Rules for C++ source code files"		>> $WorkingDir/Makefile
    cat $WorkingDir/Makefile.cc				>> $WorkingDir/Makefile
    echo " "						>> $WorkingDir/Makefile
    rm $WorkingDir/Makefile.cc
  fi
  
  echo "# Dummy rule to make things happen"		>> $WorkingDir/Makefile
  echo "FORCE:"						>> $WorkingDir/Makefile
}

MakeMakefileInc () {
  WorkingDir="$BUILDDIR/SETUP"
  echo "# Makefile.inc"					>> $WorkingDir/Makefile.inc
  echo " "						>> $WorkingDir/Makefile.inc
  echo "include ../SETUP/GPort.inc"			>> $WorkingDir/Makefile.inc
  echo "# Variables for use with other makefiles"	>> $WorkingDir/Makefile.inc
  echo							>> $WorkingDir/Makefile.inc
  
  echo "AR     = \$(GPORT_EXE_AR)"				>> $WorkingDir/Makefile.inc
  echo "CC     = \$(GPORT_CC)"				>> $WorkingDir/Makefile.inc
  echo "LD     = \$(GPORT_CC)"				>> $WorkingDir/Makefile.inc

  if [ "$BUILDNO" = "0" ]; then
    echo "CFLAGS = \$(GPORT_CC_FLAGS_OPTIMISE) \$(GPORT_CC_FLAGS_COMMON) -I.. \$(GPORT_CC_FLAGS_INCLUDE) $PLATFORM_FLAGS_VAR"	>> $WorkingDir/Makefile.inc
  else
    echo "CFLAGS = \$(GPORT_CC_FLAGS_DEBUG) \$(GPORT_CC_FLAGS_COMMON) -I.. \$(GPORT_CC_COMPILER_FLAGS_INCLUDE) $PLATFORM_FLAGS_VAR"	>> $WorkingDir/Makefile.inc
  fi

  echo " "						>> $WorkingDir/Makefile.inc
  echo "SETUP.DUMMY:"					>> $WorkingDir/Makefile.inc
  echo "	@echo \"  Target completed.\""		>> $WorkingDir/Makefile.inc
  
}

ChooseComponentList () {
  if [ ! -d "./SETUPDir/Product/Options" ]; then
    COMPONENTS="./SETUPDir/Product/COMPONENTS"
    SETUP_COMPONENT="1"
  else
    cat ./SETUPDir/Product/Options/OptionsList
    String "  Please enter your choice" SETUP_COMPONENT "1"
    while [ ! -f "./SETUPDir/Product/Options/Option_$SETUP_COMPONENT" ]; do
      echo "  *** That option is not available."
      String "  Please enter your choice:" SETUP_COMPONENT "1"
    done
    echo
    COMPONENTS="./SETUPDir/Product/Options/Option_$SETUP_COMPONENT"
  fi
}

ParseComponentList () {
  MODULE "SETUP" "other" "Support tools for SETUP" "ConfigDocs"
  . $COMPONENTS
  MakeMakefile
  MakeMakefileInc
  MakeAutoconfHeader
}

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

ErrorWhileConfig() {
  echo
  showbold "$PRODUCT $VERSION SETUP Error"
  separator
  echo
  echo "  A problem has occured while attempting to configure the source code"
  echo "  prior to compilation.  SETUP will exit."
  echo
  pause
  exit 1
}

ErrorWhileCompile() {
  echo
  showbold "$PRODUCT $VERSION SETUP Error"
  separator
  echo
  echo "  A problem has occured while attempting to compile the source code"
  echo "  prior to installation.  SETUP will exit."
  echo
  pause
  exit 1
}

ErrorWhileInstall() {
  echo
  showbold "$PRODUCT $VERSION SETUP Error"
  separator
  echo
  echo "  A problem has occured while attempting to install the product."
  echo "  SETUP will exit."
  echo
  pause
  exit 1
}

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

MakeRelease () {
  newpage "$PRODUCT $VERSION Create Distribution"
  showbold "  *** WARNING ***"
  echo
  echo "  This option is designed to be used ONLY by the current author of"
  echo "  this product; if anyone else attempts to use this option, permanent"
  echo "  loss of data could result."
  echo
  pause
  echo "  SETUP will now create a .tar.gz file suitable for distribution to"
  echo "  others."
  echo
  
  # remove any compiled source code
  echo "  o  Removing compiled code."
  if [ -d "./Build-Tree" ]; then
    rm -rf ./Build-Tree
  fi
  
  # remove any backups of edited source code
  echo "  o  Removing backups of edited source files."
  find . -name '*~' -exec rm -f {} \;
  
  # sort out file permissions
  echo "  o  Sorting out file permissions."
  find . -type d -exec chmod 755 {} \;
  find . -type f -exec chmod 644 {} \;
  chmod 755 SETUPDir/Scripts/*
  chmod 755 SETUP
  chmod 755 SETUPDir/GSetup-Scripts/GPort-Scripts/config.guess
  
  # update the product description file
  echo "  o  Updating product description file prior to creating tar file."
  ( $SCRIPTSDIR/BUILD-BEFORE )
  
  # create the tar file
  echo "  o  Creating distribution file $BUILDSTRING."
  ( cd .. ; tar -cf - $BUILDSTRING | gzip -9 > $BUILDSTRING.tar.gz )
  
  echo "  o  Updating product description file after creating tar file."
  ( $SCRIPTSDIR/BUILD-AFTER )
  
  # run maintainer-specific script to finish off the job
  echo "  o  Running Maintainer's script to finish the job."
  . $SCRIPTSDIR/MakeRelease
  
  echo
  echo "  Done."
  endpage
}

############################################################################
# STEP-BY-STEP FUNCTIONS
############################################################################

Initialise () {
  echo "Please wait ... preparing to run SETUP ..."
  
  machine="`${SCRIPTSDIR}/config.guess`"
  export machine

  if [ -f "./Source-Tree/SETUP/all-systems/libSETUP.sh" ]; then
    . ./Source-Tree/SETUP/all-systems/libSETUP.sh
  fi
}

Introduction () {
  newpage "$PRODUCT $VERSION Setup"
  echo "  Welcome to SETUP."
  echo
  echo "  SETUP will guide you through the following steps :"
  echo
  echo "   1.  Examine this machine."
  echo "   2.  Create the source code for this machine."
  echo "   3.  Interactively configure the source code."
  echo "   4.  Compile $PRODUCT $VERSION."
  echo "   5.  Install $PRODUCT $VERSION."
  echo
  echo "  You can stop SETUP at any time by pressing 'CTRL-C'."
  endpage
  
  newpage "$PRODUCT $VERSION Setup"
  echo "  SETUP provides a number of command-line parameters which you can use"
  echo "  to help you once you've run SETUP the first time ..."
  echo
  echo "  --config"
  echo "    Start SETUP from Step 3 - interactively configure the source code"
  echo "    before compiling it."
  echo
  echo "  --compile | --build"
  echo "    Start SETUP from Step 4 - compile the source code."
  echo
  echo "  --install"
  echo "    Install the compiled product."
  echo
  echo "  Also, Step 2 creates traditional makefiles which you can use if you"
  echo "  prefer.  Step 2 will tell you where the source code can be found."
  endpage
}

ProductInformation() {
  LoadProductDesc
  
  newpage "$PRODUCT $VERSION Setup"
  echo "  $PRODUCT $VERSION"
  echo "  Released $RELEASEDATE"
  echo
  echo "  Maintained By $AUTHOR"
  echo "  Please email all correspondance to $AUTHOR_EMAIL"
  echo
  sh_printf "$COPYRIGHT_VAR"
  echo "  $COPYRIGHTEND_VAR"
  echo
  echo "  Portions copyrighted by individual members of the user community"
  
  if [ "$BUILDNO" != "0" ]; then
    echo
    showbold "  USE THIS VERSION FOR TESTING PURPOSES ONLY.  DO *NOT* USE TO"
    showbold "  PROVIDE AN END-USER SERVICE - USE VERSION ${LASTRELEASE} INSTEAD." 
  fi
  endpage
}

StepOne () {
  newpage "Step One: Examine This Machine"
  
  echo "  Your machine is called $HOSTNAME and is a '$machine'."
  if [ "$SUPPORTED" = "No" ]; then
    echo "  Unfortunately, I do not know how to compile $PRODUCT for this machine."
    echo
    echo "  I may know how to compile this product for a different type of machine"
    echo "  running $OS.  This may help you port this product."
    echo
    echo "  I will now show you a list of machines and operating systems which I"
    echo "  can compile this product for.  Have a look, and see if your operating"
    echo "  system is in this list."
    endpage
    ls -CF $PLATFORMSDIR | more
    echo
    separator
    echo
    echo "  Please select one of the machines or operating systems listed above"
    echo "  and type it in at the prompt below.  If you do not wish to select"
    echo "  an operating system, just press ENTER."
    echo
    sh_printf "Please type in your choice : "
    read x < /dev/tty
    echo
    if [ ! -f "$PLATFORMSDIR/$x" ]; then
      echo "  You have typed in a name which is not in my list."
      echo
      echo "  Please read the file \"Porting.txt\", which explains what to do next."
      echo "  Or, please email the author for assistance.  You can reach the author"
      echo "  at $AUTHOR_EMAIL."
      echo
      separator
      exit 0
    fi
    echo "#"					>  $PLATFORMSDIR/$machine
    echo "# Machine description file"		>> $PLATFORMSDIR/$machine
    echo "# For $machine"			>> $PLATFORMSDIR/$machine
    echo "# Generated by SETUP"			>> $PLATFORMSDIR/$machine
    echo					>> $PLATFORMSDIR/$machine
    echo "INCLUDE \"$x\""			>> $PLATFORMSDIR/$machine
    
    echo "  Okay, we will try and compile the product for $x."
    echo
    echo "  (Please make a note of this.)  If this works, and $PRODUCT"
    echo "  compiles okay, please send a copy of the file"
    echo
    echo "    SETUPDir/Platforms/$machine"
    echo
    echo "  to $PRODUCT's maintainer, $AUTHOR ($AUTHOR_EMAIL)."
    echo "  This will be added to the next release of $PRODUCT."
    endpage
    newpage "Thank Your For Using SETUP"
    echo "  Please run SETUP again to compile $PRODUCT."
    echo
    separator
    exit 0
  fi
  echo "  Good.  I know how to compile $PRODUCT for this machine."
  echo
  echo "  Your best Bourne Shell is '$UNIX_SHELL'."
  echo "  I will use this to run the interactive configuration scripts later"
  echo "  on during SETUP."
  endpage
}

StepTwo () {
  newpage "Step Two: Create A Source Tree"
  ChooseComponentList
  CreateBuildDir
  ParseComponentList
  endpage
}

StepThree () {
  newpage "Step Three: Configure The Source Code"
  ( cd $BUILDDIR ; make config ) || ErrorWhileConfig
  endpage
}

StepFour () {
  newpage "Step Four: Compile The Source Code"
  ( cd $BUILDDIR ; make build ) || ErrorWhileCompile
  endpage
}

StepFive () {
  newpage "Step Five: Install The Compiled Software"
  ( cd $BUILDDIR ; make install ) || ErrorWhileInstall
  endpage
}

Outroduction () {
  newpage "Thank You For Using SETUP"
  echo "  $PRODUCT has been installed, and is now ready for use."
  echo "  For further information, please consult the INSTALL"
  echo "  document."
  endpage
}

main () {
  if [ "$1" = "--CC" ]; then
    CC="$2"
    shift
  fi
  
  Initialise
  ProductInformation
  
  case "$1" in
    --release)
      LoadMachineDesc
      MakeRelease
      ;;
    --install)
      LoadMachineDesc
      StepFive
      ;;
    --config)
      LoadMachineDesc
      StepThree
      StepFour
      StepFive
      ;;
    --build|--compile)
      LoadMachineDesc
      StepFour
      StepFive
      ;;
    *)
      LoadMachineDesc
      Introduction
      StepOne
      StepTwo
      StepThree
      StepFour
      StepFive
      Outroduction
      ;;
  esac
}

main "$@"
