#! /bin/csh
#
# We need some way to pass various arguments, such as where the upper-level
# directory is, and any special cases.
#
# We need some way to identify when we want to cross-compile for the intel
# (or other such machine).  
#
if ($?P4DEFDIR == 0) then
    set P4DEFDIR  = "/usr/local/p4-1.2"
endif
if ($?PVMDEFDIR == 0) then
    set PVMDEFDIR = "/usr/local/pvm"
endif
set MOPTSDEF  = "-g -O -Opg"
#
# TOOLSLOCK is YES if the install script should protect against multiple runs;
# if you are building tools for only a single architecture, define it as no
# or leave it undefined.
if ($?TOOLSLOCK == 0) then
    set TOOLSLOCK = "NO"
endif
#
# FinalDir is the eventual resting place for tools.  This allows you to
# build a version in one place and then move it.  Mostly, this affects
# the script "toolman" and the file comm/hosts.h that specifies the
# default location of the host files.
set FINALDIR  = `pwd`
set ISUPDATE  = 0
#
# BUILDMAN is set to true unless -noman is set (good for multiple builds)
set BUILDMAN  = 1
set BUILDFORT = 1
set BUILDLINT = 1
set ALLOW_CROSS = 1
# 
# Don't change the paths in the development version
set CHANGEPATHS = 1
if ($?cwd == 0) set cwd = `pwd`
if ("$cwd" == "/home/gropp/tools.n") set CHANGEPATHS = 0
if (-e ROOTDIR) set CHANGEPATHS = 0
#
# Unset commands that we will be using in case they've been aliased..
alias cd 'cd'

if ($#argv > 0) then
    if ("$1" == "-help") then
        echo "$0 [ arch ] [ subarch ] [ options value ]"
        echo "Perform the actions needed to install the tools.  This includes"
        echo "generating the object libraries and the man pages.  If arch is"
	echo "specified, that architecture value will be used instead of "
	echo "being determined by $0.  If subarch is specified, that is used"
 	echo "to further specify the architecture.  Currently, the only valid"
	echo "value for subarch is inteldelta, when arch is intelnx."
        echo ""
    	echo "The recognized options are"
	echo "-libs [ all | g | O | pg ]"
	echo "where all builds the g, O, and pg libraries, and the other"
	echo "options build just the requested library (one option must"
	echo "be present)."
	echo " "
	echo "-finaldir <directory>"
	echo "where <directory> is the final root directory for the package."
	echo "(This is provided to allow the libraries to be built in a"
	echo "different location than they will be installed in."
	echo " "
	echo "-update"
	echo "Use -update to indicate that this is an update compilation;"
	echo "the man pages, Fortran interface, and lint libraries will "
	echo "NOT be rebuilt.  This is NOT recommended unless you are very"
	echo "sure what you are doing"
	echo " "
	echo "-complete"
	echo "Use -complete to indicate that this is a complete and new"
	echo "installation.  This should NOT be used to update or recompile"
	echo "libraries."
	echo " "
	echo "-noman"
	echo "Do not build the manual pages.  This is useful if install is"
	echo "being invoked on several architectures."
	echo " "
	echo "-nofort"
	echo "Do not build the Fortran interfaces.  This is useful if you do"
	echo "not use Fortran."
	echo " "
	echo "-nolint"
	echo "Do not build the Lint libraries."
	echo " "
	echo "-nocross"
	echo "Do not cross-compile for the Intel NX (cross compiles are "
	echo "attempted only if the PGI libraries are found)."
	echo " "
        echo "Notes on alternate communication libraries:"
	echo "This script will build versions for p4 and pvm (two inter-"
        echo "processor communications libraries) if it finds the object"
  	echo "libraries for them.  By default, it looks for p4 in $P4DEFDIR"
	echo "and pvm in $PVMDEFDIR.  You can use the environment variables"
	echo "P4DIR and PVMDIR to change these." 
	echo ""
	echo "The libraries (libp4.a and libpvm.a) are searched for in "
	echo "directories P4DEFDIR/lib and PVMDEFDIR.  If values for these"
	echo "variables are not set, then default values that work for many"
	echo "installations will be chosen."
	echo " "
	echo "The environment variable TOOLSLAPACK may be set to the library"
	echo "containing lapack; in this case, install will not build its own"
	echo "version of lapack."
	echo " "
	echo "The environment variable TOOLSBLAS may be set to the library"
	echo "containing the blas; in this case, install will not build its"
	echo "own version of the blas."
	echo " "
	echo "If either TOOLSLAPACK or TOOLSBLAS are used, you may need to"
	echo "modify the file in bmake that corresponds to your computer's"
	echo "architecture."
        exit 1
    endif
endif
# Check that TOOLSDIR exists, or that the default is ok
if ($?TOOLSDIR == 0) then
    setenv TOOLSDIR  `pwd`
    if (! -e $TOOLSDIR) then
        echo "setenv TOOLSDIR to the directory containing the tools"
    endif
endif
#
# Find the architecture.  See if it is supported and if it requires 
# cross-compiling.  Note: if a specific architecture is selected on the 
# command line, only that one is done (no cross-compilation is used)
set LSUBARCH = ""
# if the first argument starts with a "-", assume that it isn't an
# architecture
if ($#argv > 0) then
   set leadchar = `expr "$1" : '-.*'`
   if ("$leadchar" == 0) then
#    set leadchar = `expr substr "$1" 1 1`
#    if ("$1" != "-libs" && "$1" != "-finaldir" && "$1" != ) then
#    if ("$leadchar" != "-") then
        set LARCH = $1
        set ALLOW_CROSS = 0
        shift
    endif
endif

if ($?LARCH == 0) then 
    set LARCH = `bin/tarch`
endif
if ($?LARCH == 0) then
    echo "Can not determine architecture type"
    exit(0)
endif
#
# Make sure that we're at the top of the directory
if (! -e bmake) then
    echo "$0 must be run from the top of the tools directory"
    exit(0)
endif
#
# Check for supported architecture by seeing if the name is known in bmake
if (! -e bmake/$LARCH) then
    echo "$LARCH is not a supported architecture."
    if (-e bmake && -d bmake) then
        echo "Supported architectures are"
        foreach F (bmake/*.g)
            set f = $F:t
            echo $f:r
        end
    else
        echo "You may not have created the directory structure needed by"
        echo "this installation script.  Try again."
   endif
   exit(0)
endif
#
# Eat the remaining arguments
set MOPTS = "$MOPTSDEF"
set BOPTS = "O"
set MAKELIBLINE = "libbuild"
while ($#argv > 0)
    switch ($1) 
    case -finaldir:
    shift
    set FINALDIR = $1
    shift
    breaksw

    case -update:
    shift
    set ISUPDATE = 1
    breaksw

    case -noman:
    shift
    set BUILDMAN = 0
    breaksw

    case -nofort:
    shift
    set BUILDFORT = 0
    breaksw

    case -nolint:
    shift
    set BUILDLINT = 0
    breaksw

    case -nocross:
    shift
    set ALLOW_CROSS = 0
    breaksw

    case -complete:
    shift
    set MAKELIBLINE = "liball"
    breaksw

    case -libs:
    shift
    set MOPTS = $1
    shift
    switch ($MOPTS)
	case all: 
	set MOPTS = '"-g" "-O" "-Opg"'
	set BOPTS = "O"
	breaksw
	case g  : 
	set MOPTS = "-g"
	set BOPTS = "g"
	breaksw
	case O  : 
	set MOPTS = "-O"
	set BOPTS = "O"
	breaksw
	case pg : 
	set MOPTS = "-Opg"
	set BOPTS = "Opg"
	breaksw
	default   : 
	echo "Unknown value for libs $MOPTS"
	exit 1
	breaksw
	endsw
    breaksw

    default:
    set LSUBARCH = -D$1
    shift
    breaksw
    endsw
end
#
#
# Check that the programs that are needed are in fact available.  This
# includes the CC, FC, make.  Among the known things to test for are
# PGI - intelnx compiler
set INTELCROSS = ""
if ($ALLOW_CROSS && $?PGI) then
   # See if the cross compiler directory exists.  This works for sun3 and sun4
   # versions; it may need to be changed for other sites or architectures.
   if (-e $PGI/i860/bin.$LARCH) then
       set INTELCROSS = "intelnx"
   endif
   # Now, see if we are building a DELTA version (some communication files
   # and macros are slightly different, and some routines, such as nodedim,
   # are not supported on the Delta).  Since some sites may have both a cube
   # and a mesh, we use the following code:
   if ($#argv > 0) then
        set LSUBARCH = -D$1
	# Add this to the basic definition in intelnx
	
   endif
endif

endif    
# 
# Make sure that buildlib is executable
if (! -e ./bin/buildlib || ! -x ./bin/buildlib) then
    echo "The buildlib program either does not exist or is not executable"
    if (-e ./bin/buildlib) then
        chmod a+x ./bin/buildlib
    else
	echo "Exiting"
        exit(0);
    endif
endif
#
# P4 - p4 versions
# Get the location of p4.  There is an additional commplication.  The
# standard installation of p4 use /usr/local and assumes no cross-compilers.
# We'd also like to use our form (basedir/arch/lib/libp4.a).  For now, 
# just set the P4DIR appropriately.
if ($?P4ARCH == 0) set P4ARCH = Unknown
if ($?P4DIR == 0) then
    # This should really try to find p4 versions and say something when
    # it finds several
    set P4DIR = $P4DEFDIR
endif
set P4 = ""
if (-e $P4DIR) then
    # Convert from our arch to P4's arch
    set P4ARCH = "U"
    switch ($LARCH)
    case sun4: 
    set P4ARCH = SUN
    breaksw
    case IRIX:
    set P4ARCH = SGI
    breaksw
    case NeXT:
    set P4ARCH = NEXT
    breaksw
    case intelnx:
    set P4ARCH = IPSC860
    breaksw
    case tc2000:
    set P4ARCH = TC_2000
    breaksw
    case rs6000:
    set P4ARCH = RS6000
    breaksw
    case symmetry:
    set P4ARCH = SYMM
    breaksw
    case dec5000:
    set P4ARCH = DEC5000
    breaksw
    default:
    echo "$LARCH is not a known P4 architecture type"
    endsw
endif
if ($?P4LIBDIR == 0) then
    set P4LIBDIR = $P4DIR/$P4ARCH
endif
if (-e $P4LIBDIR/lib/libp4.a) then
    set P4 = "p4"
    if ($CHANGEPATHS == 1) then
	sed s%/usr/local/p4-1.0%$P4LIBDIR% bmake/generic.p4 >! bmake/$LARCH.p4
	sed s%/usr/local/p4-1.1%$P4LIBDIR% comm/daemons/p4start.gn >! comm/daemons/p4start
    endif
else if (-e $P4DIR/lib/libp4.a) then
    set P4LIBDIR = $P4DIR
    # This is the "old" location for p4 libraries.
    set P4 = "p4"
    if ($CHANGEPATHS == 1) then
	sed s%/usr/local/p4-1.0%$P4DIR/% bmake/generic.p4 >! bmake/$LARCH.p4
	sed s%/usr/local/p4-1.1%$P4DIR/% comm/daemons/p4start.gn >! comm/daemons/p4start
    endif
endif
#
# PVM - pvm version
# Get the location of pvm
if ($?PVMDIR == 0) then
    set PVMDIR = $PVMDEFDIR
endif
set PVM = ""
if (-e $PVMDIR) then
    # Convert from our arch to PVM's arch
    set PVMARCH = "U"
    switch ($LARCH)
    case sun4: 
    set PVMARCH = SUN4
    breaksw
    case IRIX:
    set PVMARCH = SGI
    breaksw
    case NeXT:
    set PVMARCH = NEXT
    breaksw
    case intelnx:
    set PVMARCH = I860
    breaksw
    case tc2000:
    set PVMARCH = BFLY
    breaksw
    case rs6000:
    set PVMARCH = RIOS
    breaksw
    case symmetry:
    set PVMARCH = SYMM
    breaksw
    default:
    echo "$LARCH is not a known PVM architecture type"
    endsw

    if ($?PVMLIBDIR == 0) then
        set PVMLIBDIR = $PVMDIR/src/$PVMARCH
    endif

    if ($PVMARCH != "U" && -e $PVMLIBDIR/libpvm.a) then
        set PVM = "pvm"
	# We should really edit the bmake files to point to the 
	# correct directories
	if ($CHANGEPATHS == 1) then
	    sed s%/home/gropp/pvm/src/GENERIC%$PVMLIBDIR% bmake/generic.pvm >! bmake/$LARCH.pvm
	endif
    endif
endif

#
# EUI - IBM - eui version
# See if EUI is available
set EUI = ""
if ($LARCH == rs6000 && -e /usr/lpp/poe/include/mp_eui.h) then
   set EUI = "eui"
endif

# Files to check: 
# graphs/dispsp.c graphs/dispmsh.c
# solids/makefile solids/send.c solids/receive.c
if ($CHANGEPATHS == 1) then
    sed s%/home/gropp/tools.n%$FINALDIR% bin/toolman.gn >! bin/toolman
    chmod a+x bin/toolman
    if (-e comm/hosts.gn) then 
        sed s%/home/gropp/tools.n%$FINALDIR% comm/hosts.gn >! comm/hosts.h
    endif
    if (-e tex/header.tex) then
        sed s%/home/gropp/tools.n%$FINALDIR% tex/header.gn >! tex/header.tex
    endif
    # Need to introduce a path so that the bmake/$LARCH files can find the
    # blas that come with tools
endif
#
# 
# Further horrible things.  On Suns, there are lots of incompatible versions
# of the compilers, particularly the Fortran compiler (if any!).  We need
# some code to test for that here.
setenv SUNVER ""
if ($LARCH == "sun4") then
    # See if the OS version is old
    if (! -e /usr/include/sys/utsname.h) setenv SUNVER "-Dsun4Pre41"
endif
if ($LARCH == "rs6000") then
    # See if the compiler supports -O3
    xlc -O3 >& .xlctest
    set vtest = `cat .xlctest`
    /bin/rm .xlctest
    if ("$vtest" != "") then
	# Check to see if we have a problem...
	grep -s '^BASEOPT = -O3' bmake/rs6000.O
	if ($status == 0) then
         echo "xlc does not support -O3; edit bmake/rs6000.O and rerun install"
 	 exit 1
        endif
    endif
endif
#
# See if we should build the blas.
setenv TOOLSNOBLAS "NO"
grep -s '^BLAS1 = \$(ITOOLSDIR)' bmake/$LARCH
if ($status == 1) setenv TOOLSNOBLAS "YES"
#
# See if we should build LAPACK
setenv TOOLSNOLAPACK "NO"
grep -s '^LAPACK = \$(ITOOLSDIR)' bmake/$LARCH
if ($status == 1) setenv TOOLSNOLAPACK "YES"
#
# See if X is available
# NEED TO FIX THIS FOR X11 INCLUDE IN AN UNEXPECTED PLACE (UIUC DOES THIS)
setenv TOOLSNOX "NO"
if (! -e /usr/include/X11) setenv TOOLSNOX "YES"
#
# See if lapack or blas are available elsewhere.  If so, don't build them.
if ($?TOOLSLAPACK) then
    if (-e $TOOLSLAPACK) then
        # need to edit the bmake file
	if ($CHANGEPATHS == 1) then
            cp bmake/$LARCH bmake/$LARCH.bak
	    sed "s%^LAPACK = .*%LAPACK = $TOOLSLAPACK%" bmake/$LARCH.bak > bmake/$LARCH
	    setenv TOOLSNOLAPACK YES
 	endif
    else
	unsetenv TOOLSLAPACK
    endif
endif
if ($?TOOLSBLAS) then
    if (-e $TOOLSBLAS) then
	if ($CHANGEPATHS == 1) then
  	    cp bmake/$LARCH bmake/$LARCH.bak
	    sed 's%\$(ITOOLSDIR)/libs/libs\$(BOPT)/sun4/blas.a%'$TOOLSBLAS% bmake/$LARCH.bak > bmake/$LARCH
	    setenv TOOLSNOBLAS YES
	endif
    else
	unsetenv TOOLSBLAS
    endif
endif
# 
# Save the values of toolsnox and toolsnoblas
set TNOX = "$TOOLSNOX"
set TNOB = "$TOOLSNOBLAS"
#
#
# Build EVERYTHING, including all of the libraries
# We also need to specify each of these fields as variables so that they
# can be properly set up.
# Also, on systems with the blas, we shouldn't build the blas from the
# Fortran versions.
echo "Object libraries will now be built.  This will take some time."
if ("$INTELCROSS" != "") then
    echo "Cross-compilation for the Intel parallel machines will be done."
endif
foreach MCOMM ( "" $P4 $PVM $EUI)
    if ($MCOMM == "") then
	set OPTLINE = "OPT=$LSUBARCH"
    else
        echo "Communication version $MCOMM"
	# Used to set "OPT=-D$MCOMM"; no longer needed because $LARCH.$COMM
	# sets it (and other things)
	set OPTLINE = "OPT=$LSUBARCH"
    endif
    foreach MARCH ( $LARCH $INTELCROSS )
	# Note that pvm doesn't run inside an intel (it uses sockets for
        # everything)
        if ($MCOMM != "pvm" || $MARCH != "intelnx") then
            echo "Architecture $MARCH"
	    if ($MARCH == "intelnx") then
	        setenv TOOLSNOX "YES"
		setenv TOOLSNOBLAS "YES"
#            else
#                unsetenv TOOLSNOX
	    endif
            foreach MOPT ( $MOPTS )
		if ("$MOPT" != "-Opg" || -e ./bmake/$MARCH.Opg) then
 	            echo "Optimization level $MOPT"
                    ./bin/buildlib $MOPT ARCH $MARCH $MAKELIBLINE COMM=$MCOMM $OPTLINE 
		endif
            end
	endif
    end
end
#
# Try to build upshot.
# Before we do this, it is necessary to run imake or xmkmf
if (-e $TOOLSDIR/upshot) then
    echo "upshot"
    echo "To build upshot, cd to $TOOLSDIR/upshot and use xmkmf or imake,"
    echo "followed by make clean and then make.  Finally, "
    echo "do cd src ; mv -f upshot xhist xsum xedge ../../bin/$LARCH"
#    if (! -d bin/$LARCH ) mkdir bin/$LARCH
#    ( unalias cd ; cd $TOOLSDIR/upshot ; xmkmf ; make clean ; make ; \
#       cd src ; mv -f upshot xhist xsum xedge ../../bin/$LARCH ; )
endif
#
# Add the symbolic links for BOPT not specified
if (! -e $TOOLSDIR/libs/libs)  \
	ln -s $TOOLSDIR/libs/libsO   $TOOLSDIR/libs/libs 
if (! -e $TOOLSDIR/libs/libspg) \
	ln -s $TOOLSDIR/libs/libsOpg $TOOLSDIR/libs/libspg 
foreach MARCH ( $LARCH $INTELCROSS )
    if (! -e $TOOLSDIR/bmake/$MARCH.) \
	ln -s $TOOLSDIR/bmake/$MARCH.O   $TOOLSDIR/bmake/$MARCH.   	
    if (! -e $TOOLSDIR/bmake/$MARCH.pg && -e $TOOLSDIR/bmake/$MARCH.Opg) \
	ln -s $TOOLSDIR/bmake/$MARCH.Opg $TOOLSDIR/bmake/$MARCH.pg 
end   
#
# These should be done unless -update is specified 
# Also, don't do these for cross-compilation 
# Yet also, make sure that these are locked against other installs
if ($LARCH == "intelnx") set ISUPDATE = 1
if ( ! $ISUPDATE ) then 
    if ("$TOOLSLOCK" == "YES" ) then
        while ( `$TOOLSDIR/bin/lockdir $TOOLSDIR/bin $LARCH` ) 
            echo "Waiting for directory $TOOLSDIR to unlock for update of "
	    echo "interfaces and man pages."
            sleep 60
        end
    endif
    # 
    # The cm5 is a special case.  Again, like the intelnx, it is realy a
    # cross-compile.  If all you are doing IS the cross-compile, then
    # some things will break.  I need to fix this (a separate build-enough-
    # to-run-these-routines step).
    if ($LARCH == "cm5") then
	echo "The cm5 is really a cross-compilation.  To get the man pages"
	echo "Fortran interface, and lint file, build the sun4 version first."
    endif
    if ( $BUILDMAN ) then 
        #
        # Generate the man pages if we need to.
        echo "Building manual pages..."
        ./bin/buildman $LARCH
    endif
    if ($BUILDFORT) then
        #
        # Generate the Fortran interface.  Must handle the case of no X11 for
        # some versions (in buildfrt).  Do intel cross-compilation first, 
        # so that the fullest source (including X code) will be left in the
        # directory
	echo "Building the Fortran interface..."
        foreach MARCH ( $INTELCROSS $LARCH )
	    # restore the TOOLSNO... values
    	    setenv TOOLSNOX $TNOX
	    setenv TOOLSNOBLAS $TNOB
	    if ($MARCH == "intelnx") then
	        setenv TOOLSNOX "YES"
	        setenv TOOLSNOBLAS "YES"
            else
                unsetenv TOOLSNOX
	    endif
            ./bin/buildfrt $MARCH
        end
    endif
    #
    if ($BUILDLINT) then
        # Generate the lint library
	echo "Building the Lint library..."
        (cd ./lint ; make ARCH=$LARCH BOPT=$BOPTS )
        ./bin/buildlnt
    endif
    if ("$TOOLSLOCK" == "YES" ) then
        $TOOLSDIR/bin/unlockdir $TOOLSDIR/bin
    endif
endif
#
echo " "
echo "Run $TOOLSDIR/bin/buildex to build and run the example programs"
#
exit 0
