#!/bin/csh -f
#>
#> PACT-CONFIG - Configure the Makefiles for this PACT release.
#>  
#> Usage: 
#>   pact-config [-g] [-h] [-v] [-i <directory>] 
#>                         [-f <directory>] [-s <sys-id>] <config>
#>
#> Options:
#>         -g        - build an absolutely debuggable version
#>         -h        - display the help package
#>         -v        - display the Version of the Source Control System
#>   
#>         -i        - base installation directory (default /usr/local)
#>                     to omit installation use "-i none"
#>         -f        - a directory containing your configuration file
#>         -s        - explicitly set the hardware/OS/release system id
#>         <config>  - the configuration for the installation
#>                     sample configs (see manager/configs.std) are:
#>                         -    auto    auto-configuration
#>                         -    aix     IBM RS6000 systems
#>                         -    hp      HP RISC 700 systems
#>                         -    irix    SGI systems
#>                         -    irix    SGI systems with GCC
#>                         -    irix64  64 bit SGI systems running IRIX64 6.x
#>                         -    linux   LINUX systems
#>                         -    mips    MIPS OS systems
#>                         -    osf     OSF systems
#>                         -    osfg    OSF systems with GCC
#>                         -    paragon INTEL Paragon systems running OSF1
#>                         -    solaris Sun Solaris systems
#>                         -    sung    Sun systems with GCC
#>                         -    sun     standard Sun systems
#>                         -    sunmos  INTEL Paragon systems running SUNMOS
#>                         -    t3d     CRAY t3d systems
#>                         -    ultrix  DEC ULTRIX systems
#>                         -    unicos  CRAY UNICOS systems 
#>                   (check manager/configs.local for any local configs)
#>
# Comments:  
#    The Makefiles are configured for the given system.
#    Some of the complexity of this process stems from the fact
#    that there are several ways of obtaining the source:
#        -    a CVS repository
#        -    an anonymous FTP node
#        -    a compressed tar file
#    and that the other parts of the manager interlock with this script.
#
# Deprecated option:
#   -n  auto-configuration 
#
# Modification History:
#   07-23-94 Jan Moura, LLNL:  Modifie usage info, added  options [-h], [-v]
####################
#
# Source Version: 2.0
# Software Release #92-0043
#
# include "cpyright.h"
#

# Setting umask is necessary so that members of dpact group have access.
# Otherwise directories are created that other group members cannot remove.
umask 002
unalias cd

# see PCD for the reason for this
set Here = `pwd` ; cd ; set RealHome = `pwd` ; cd $Here ; unset $Here

set Here = `pwd | sed "s|$RealHome|$home|"`

if ($?USER == 0) then
   if ($?LOGNAME == 0) then
      set USER = "anonymous"
   else
      set USER = $LOGNAME
   endif
endif

set UserDir       = $Here
set Parent        = $Here:h
set ConfigDir     = ( "configs.*" )
set InstArg       = ""
set NoConfig      = FALSE
set Configuration = ""
set System        = `./system-id`
set ExplSystem    = FALSE
set EnvFile       = "env-$System"
set AbsoluteDeb   = ""

set HelpCode      = ./$0
set WhoIAm        = $USER

set SCSRepository = ""
set SRCDate       = ""
 
if ($#argv < 1) then
   goto Help
endif

set Opt = "$HelpCode:t  $argv"

while ($#argv > 0)
   switch ($1)
      case -g:
               set AbsoluteDeb = "-g"
               breaksw

      case -h*:                                  #print Usage:
               goto Help

      case -v*:                                  #Display version...
	       source pact-scs
	       echo "  Source Version:   " $SCSVersion
	       echo "  Software Release: " $SCSRelease
	       echo ""
	       exit(1)

      case -i:
               shift
               set InstArg = "-i $1"
               breaksw

      case -f:
               shift
               set ConfigDir = ( $1 $ConfigDir )
               breaksw

      case auto:
      case -n:
               set NoConfig = TRUE
               breaksw

      case -s:
               shift
               set System     = $1
               set ExplSystem = TRUE
               set EnvFile    = "env-$System"
               breaksw

      default:
               set Configuration = $1
   endsw
   shift
end

# Source Control System Variables
source pact-scs

#
# set up some items in the working environment
#

set path = ( . $Here:h/dev/$System/bin $path )

rm -f $EnvFile
touch $EnvFile

echo "" >> $EnvFile
if ($ExplSystem == TRUE) then
   echo "set System = $System" >> $EnvFile
else
   echo 'set System = ''`'$Here"/system-id"'`' >> $EnvFile
endif
echo "set path = ( . $Here:h/dev/"'$System/bin $path )' >> $EnvFile
echo "" >> $EnvFile

#
# Make sure that the installation directories exist
#
set HostOS    = `uname -s`
set HostOSRel = `uname -r`
set HostArch  = `uname -m`
set WorkBase  = ../dev
set InstBase  = $WorkBase/$System
if (!(-d $WorkBase)) then
   mkdir $WorkBase
endif
if (!(-d $InstBase)) then
   mkdir $InstBase
endif
if (!(-d $InstBase/bin)) then
   mkdir $InstBase/bin
endif
if (!(-d $InstBase/lib)) then
   mkdir $InstBase/lib
endif
if (!(-d $InstBase/include)) then
   mkdir $InstBase/include
endif
if (!(-d $InstBase/scheme)) then
   mkdir $InstBase/scheme
endif
if (!(-d $InstBase/scheme/extensions)) then
   mkdir $InstBase/scheme/extensions
endif

#
# Finding the configuration file
#
echo ""
if ($NoConfig == TRUE) then
   set KnownConfig = `awk '$1 == "'$HostOS'" {$1 = "";$2 = "";$3 = "";printf "%s\n%s\n", $4, $5}' systems.unix | sort | uniq`

   if ($#KnownConfig > 0) then
      echo "This system is running: $HostOS $HostOSRel"
      echo "PACT has been built on systems like this before."

      if ($#KnownConfig > 1) then
         set GotIt = FALSE
         while ($GotIt == FALSE)
            if ($?prompt) then
	       echo "You have the choice of: $KnownConfig" > /dev/tty
               echo -n "Which configuration option do you wish? " > /dev/tty
	    else
               echo "The following configuration options are available: $KnownConfig"
               echo -n "Specify the option you want on the execute line."
               exit(3)
            endif
            set NewOpt = $<
            foreach Config ($KnownConfig)
               if ($Config == $NewOpt) then
                  set GotIt = TRUE
                  break
               endif
            end
         end
         set ConfigOpt = $NewOpt
         echo "The option you have chosen is: $ConfigOpt"
      else
         set ConfigOpt = $KnownConfig
         echo "Proceeding with the $ConfigOpt configuration...."
      endif
      echo ""
   else
      set ConfigOpt = "-n"
      echo "PACT has not been built on a system like this: $HostOS $HostOSRel"
      echo "It may be a known system set up in an unfamiliar way."
      echo "Attempting to build a configuration with what can be deduced."
      echo "The most likely problem area is C compiler options."
      echo "Check the resulting make-def file carefully."
      echo ""
   endif
else
   set ConfigOpt = $Configuration
endif

if ("$ConfigOpt" == "-n") then
   ./pact-make-configs $InstArg $AbsoluteDeb -s $System -n
   if ($status != 0) then
      echo "  pact-make-configs error in pact-config"
      exit(1)
   endif
else
   set GotIt = FALSE
   foreach dir ($ConfigDir)
      if ($GotIt == FALSE) then
         if (-e $dir/$ConfigOpt.config) then
            echo "Using $ConfigOpt.config in $dir to configure this PACT installation."
            cat $dir/$ConfigOpt.config | ./pact-make-configs $InstArg $AbsoluteDeb -s $System $dir/$ConfigOpt.config
            if ($status != 0) then
               exit(1)
            endif
            set GotIt = TRUE
         endif
      endif
   end
   if ($GotIt == FALSE) then
      echo "There is no $ConfigOpt.config in $ConfigDir"
      exit(3)
   endif
else
   echo "I have insufficient information to configure this installation"
   exit(3)
endif

chmod a+x code-date label-ps

#
# ensure that there is a correct pre-Make file
#
rm -f pre-Make
sed "s|BASE|$Parent|" pre-Make.dist > pre-Make

$InstBase/bin/pact -i install >& /dev/null

if ($status != 0) then
   echo "   PACT install PROBLEM in pact-config"
   exit(1)
endif

rehash

#
# create empty extensions directories if they don't exist already
#
set ExtDirs = ( ../sx/applications/extensions ../ultra/applications/extensions )
foreach dir ($ExtDirs)
   if (-d $dir:h) then
      if (!(-d $dir)) then
         mkdir $dir
      endif
   endif
end

echo ""
echo "Source $EnvFile into your current environment before proceeding"
echo "to do work on the PACT system not covered by the installation process"

echo ""

exit($status)


Help:
awk '($1 == "#>") {print}' $HelpCode      #print Usage:
exit(1)
