#! /bin/sh
#
#  This shell script sets up various configuration parameters.
#
#  Copyright (C) 1992  Roland W. Freund and Noel M. Nachtigal
#  All rights reserved.
#
#  This code is part of a copyrighted package.  For details, see the
#  file `cpyrit.doc' in the current directory.
#

######################################################################
#                                                                    #
#  First, a few subroutines.					     #
#                                                                    #
######################################################################

#
#  Abort in case of unpacking failure.
#
CheckOK()
{
    if test ! $? = 0; then
	echo " "
	echo "Unpacking failed, aborting."
	echo " "
	cd $top && rm -rf tmp$$
	exit 1
    fi
}

#
#  Cleanup after the script.
#
CleanUp()
{
    echo " "
    cd $top && rm -rf tmp$$
    return 0
}

#
#  Locate the file or directory in $f in the search paths in $data.
#  The result of the search is returned in $f.
#
Locate()
{
#  Loop until we either find it or give up.
#  We look for file or directory $f in the given directories.
    while test true ; do
#  Search the directories.
        for dir in $data ; do
            case "$f" in
                '.')        if test -d $dir/$f ; then
                                f="$dir/$f"
                                return 0
                            fi;;
                *)          if test -f $dir/$f ; then
                                f="$dir/$f"
                                return 0
                            fi;;
            esac
        done
#  Could not find it.  Get correct name/location from user.
        echo " "
        echo "I cannot find $f."
        dflt='y'
        echo $n "Try another name (y/n)? [$dflt] $c"
        read dflt
        if test "X$dflt" = "Xn" ; then
            return 1
        fi
        dflt="$f"
        echo $n "Enter the correct name or location [$dflt]: $c"
        read f
        if test "X$f" = "X" ; then
            f="$dflt"
        fi
#  Check whether we can find it now.
        if test -f "$f" ; then
            return 0
        fi
    done
}

#
#  Wait until the user presses Return.
#
Wait()
{
    echo " "
    echo $n "Press Return when ready to continue...$c"
    read f
    echo " "
}

######################################################################
#                                                                    #
#  Initialize the script.					     #
#                                                                    #
######################################################################

#
#  Check that we are Bourne shell.
#
PATH=".:$PATH"
export PATH || (sh $0; kill $$)

if test ! -t 0 ; then
    echo "You should say 'sh $0', not 'sh < $0'."
    exit 1
fi

#
#  Check that we are in the top directory.
#
if test ! -f MANIFEST ; then
    echo "You should run Setup in the top-level directory."
    exit 1
fi

#
#  Set up the work directory.
#
top=`pwd`
if test -d tmp$$ ; then
    rm -rf tmp$$
fi
mkdir tmp$$
if test ! $? = 0 ; then
    echo "Cannot create temporary directory."
    exit 1
fi
cd tmp$$

#
#  Set up the exit trap.
#
trap 'echo " "; cd $top; rm -rf tmp$$; exit 1' 1 2 3 15

#
#  Use `echo -n' or `echo \c'?
#
c=''
n=''
f=`echo ".\c"`
case "$f" in
    '.')	c='\c';;
    *)		n='-n';;
esac

######################################################################
#                                                                    #
#  How about a little introduction?				     #
#                                                                    #
######################################################################

cat << 'EOM'

This shell script is the first step in the installation process.  It will first
check your distribution, to ensure that it is complete.  It will then attempt
to extract some default compiler names from your 'make' program, and locate the
C preprocessor and the FORTRAN compiler.  It will also prompt you for the
default compilation flags to be used for the preprocessor and for the FORTRAN
compiler.

The script was designed to work in Unix under the Bourne shell, and uses some
hopefully standard constructs and utility programs.  It tries to be fairly
careful about checking for errors and such; for example, if it cannot extract
any of the files it is supposed to create, it will abort with an error message
'Unpacking failed, aborting.'.  It was tested on a few machines; nonetheless,
it might crash and burn on any given machine, and in particular, on yours.  If
it does, please send me a message and let me know what happened; I can be
reached by e-mail as na.nachtiga@na-net.ornl.gov.
EOM
Wait
cat << 'EOM'
You should enter input or press a key only when asked to do so, since many
machines have typeahead buffers.  Any time you are prompted for input, the
default value will be shown in brackets.  You may accept this value by pressing
'Return', or specify another value.  If you wish to specify a null value, then
enter the word 'none'.

You may abort the installation at any time by pressing the 'Break' combination
for your system (typically Control-C).  If you abort the installation, you
should rerun this script before proceeding to use the codes.

Finally, this script will hardcode various paths into the files it creates.
This means that if you are on a network and if you have different directory
structures on different machines, then the files created by this script will be
specific to the machine you ran this script on.  You may have to rerun this
script before using its files on a different machine.
EOM
Wait

######################################################################
#                                                                    #
#  Check that we have all the files.				     #
#                                                                    #
######################################################################

echo " "
echo "Let's see if you have all the files."
echo $n "Checking the distribution...$c"
( \
cd $top; \
ls `awk '$3 != "Dir," {print $1}' MANIFEST` > /dev/null 2>>tmp$$/temp \
)

if test -s temp ; then
    echo " "
    cat temp
    CleanUp
    exit 1
fi
echo " OK."

######################################################################
#                                                                    #
#  Find the programs needed for the makefiles.			     #
#                                                                    #
######################################################################

#
#  A (rather long) list of possible paths.
#
data="/bin:/etc:/lib:/sbin:/usr/5bin:/usr/5lib:/usr/bin:/usr/contrib/bin:/usr/etc:/usr/kvm:/usr/lang:/usr/lib:/usr/local:/usr/local/bin:/usr/local/etc:/usr/local/lib:/usr/ucb:$PATH"
data=`echo $data | sed "s|\:| |g"`

#
#  Get some default values from the makefile defaults.
#
cat << 'EOM'


I will now try to locate several programs needed during compilation.  I'll try
to find them on my own, but if I can't find them, you'll have to tell me where
they are.  I will also ask you for various compilation flags.
I'll start by extracting default program names and flags from 'make'.
EOM
Wait
sed 's/^X//' << 'EOM' > 'makefile'
Xtarget:
X	echo $(FC) > f77.out
X	echo $(CF) > cf.out
X	echo $(CPPFLAGS) > cppflags.out
EOM
CheckOK
make -f makefile > /dev/null
echo "OK, defaults extracted."

#
#  Locate the library builder ar, the symbol table generator ranlib, and
#  get the ar flags.
#
cat << 'EOM'


First, I'll look for the library builder 'ar', and the symbol table generator
'ranlib'.  On some systems, 'ar' itself can also build the symbol table; this
is typically done by adding 's' to its flags.  On these systems, 'ranlib' is
not needed, nor does it usually exist.  I'll try to figure out what is the
status on your machine, but I may need your help.  If I cannot find 'ranlib',
and you know it does not exist on your system (as is the case, for example, on
some Crays), then answer 'n' when I ask for an alternate name, and ignore the
message later on about adding 'ranlib' to your PATH.  The default flags for
'ar' are 'rcv'.
EOM
missing=''
echo " "
echo "Looking for the library builder ar..."
ar=''
f='ar'
Locate
if test ! $? = 0 ; then
    missing="$missing $f"
    echo "I could not find the library builder, you're on your own."
else
    ar="$f"
    ranlib='true'
    echo "I found the library builder ar."
    echo " "
    $ar rc test.a makefile
    $ar ts test.a > /dev/null 2>&1
    if test $? = 0 ; then
	arflags='rcsv'
	echo "It seems you don't need ranlib, we'll try without it."
    else
        f='ranlib'
	arflags='rcv'
        echo "Looking for ranlib..."
        Locate
        if test ! $? = 0 ; then
            missing="$missing $f"
            echo "OK, we'll do without."
        else
            ranlib="$f"
            echo "OK, I found it."
        fi
    fi
    echo " "
    dflt="$arflags"
    echo $n "Enter any library builder flags that might be needed [$dflt]: $c"
    read f
    if test "X$f" = "X" ; then
        f="$dflt"
    fi
    if test "$f" = "none" ; then
        arflags=''
    else
        arflags="$f"
    fi
fi

#
#  Locate the C preprocessor and get its flags.
#
cat << 'EOM'


Next, I'll look for the C preprocessor and get its flags.  The source files in
this package are basically FORTRAN code, but with a few C-style '#define' macro
definitions.  Usually, the C preprocessor can accept these files as input and
produce as output the same file, with the appropriate replacements made.  The
output also typically includes line-number information, in the form of lines of
the form '#lineno ...'.  These lines will be stripped out of the output file by
using a 'sed' script.  If your C preprocessor behaves this way, then no special
compilation flags are needed; you may want to give it a try this way first.  If
this fails, then you may need to specify additional flags for 'cpp' when asked
below.
EOM
Wait
echo "Looking for the C preprocessor cpp..."
cpp=''
f='cpp'
Locate
if test ! $? = 0 ; then
    missing="$missing $f"
else
    cpp="$f"
    echo "Amazingly, I found it."
fi
dflt=`cat cppflags.out`
if test "X$dflt" = "X" ; then
    dflt='none'
fi
echo $n "Enter any C preprocessor flags that might be needed [$dflt]: $c"
read f
if test "X$f" = "X" ; then
    f="$dflt"
fi
if test "$f" = "none" ; then
    cppflags=''
else
    cppflags="$f"
fi

#
#  Locate the FORTRAN compiler and get its flags.
#
fc=''
f=`cat f77.out`
if test "X$f" = "X" ; then
    f=`cat cf.out`
    if test "X$f" = "X" ; then
	f='f77'
    fi
fi
echo " "
echo "Looking for the FORTRAN compiler $f..."
Locate
if test ! $? = 0 ; then
    missing="$missing $f"
else
    fc="$f"
    echo "Wow, I found the FORTRAN compiler."
fi
dflt='none'
echo $n "Enter any FORTRAN compiler flags that might be needed [$dflt]: $c"
read f
if test "X$f" = "X" ; then
    f="$dflt"
fi
if test "$f" = "none" ; then
    fflags=''
else
    fflags="$f"
fi

######################################################################
#                                                                    #
#  Create the header for the makefiles.                              #
#                                                                    #
######################################################################

#
#  Start the header file.
#
echo " "
echo " "
outf=header.mak
echo $n "I'll make the header file for the makefiles...$c"
sed 's/^X//' << 'EOM' > $outf
X#**********************************************************************
X#
X#     Copyright (C) 1991, 1992  Roland W. Freund and Noel M. Nachtigal
X#     All rights reserved.
X#
X#     This code is part of a copyrighted package.  For details, see the
X#     file `cpyrit.doc' in the top-level directory.
X#
X#     *****************************************************************
X#     ANY USE OF  THIS CODE CONSTITUTES ACCEPTANCE OF  THE TERMS OF THE
X#                             COPYRIGHT NOTICE
X#     *****************************************************************
X#
X#**********************************************************************
X#
X#  This file is generated  by the Setup script.  It is the  header file
X#  for all makefiles, and contains  general rules, compilation programs
X#  and flags.  You need to make sure these are correct for your system.
X#
X
X#
X#  Make our own suffixes' list.
X#
X.SUFFIXES:
X.SUFFIXES:	.f	.o
X.SUFFIXES:	.src	.f
X
X#
X#  Default command.
X#
X.DEFAULT:
X	@echo "Unknown target $@, try:  make help"
X
X#
X#  Command to build .o files from .f files.
X#
X.f.o:
X	@echo Making $@ from $<
X	@$(FC) $(FFLAGS) -c $<
X
X#
X#  Command to build .f files from .src files.
X#
X.src.f:
X	@echo Making $@ from $<
X	@$(CPP) $(CPPFLAGS) $< | sed -f $(TOP)/incl/convert.sed | awk -f $(TOP)/incl/convert.awk > $@
X
X#
X#  Compilation programs and flags.  Check these for your system.
X#
X
X#
X#  The shell invoked by MAKE.
X#
XSHELL	 = /bin/sh
X
X#
X#  The linker flags.
X#
XLDFLAGS	 =
X
X#
X#  The library builder, utilities, and flags.
X#
EOM
CheckOK
echo "RANLIB	 = $ranlib" >> $outf
sed 's/^X//' << 'EOM' >> $outf
X
X#
X#  The library builder.
X#
EOM
echo "AR	 = $ar" >> $outf
echo "ARFLAGS	 = $arflags" >> $outf
sed 's/^X//' << 'EOM' >> $outf
X
X#
X#  The C preprocessor and its flags.
X#
EOM
echo "CPP	 = $cpp" >> $outf
echo "CPPFLAGS = $cppflags" >> $outf
sed 's/^X//' << 'EOM' >> $outf
X
X#
X#  The FORTRAN compiler and its compilation flags.
X#
EOM
echo "FC	 = $fc" >> $outf
echo "FFLAGS	 = $fflags" >> $outf
sed 's/^X//' << 'EOM' >> $outf
X
X#
X#  This is the top of the directory tree.
X#
EOM
echo "TOP	 = $top" >> $outf
echo " OK."

#
#  Move the header file in its final resting place.
#
mv $outf $top/header.mak

#
#  Check for any missing compilation programs.
#
if test ! "X$missing" = "X" ; then
    echo " "
    echo "Warning: I could not find:"
    for f in $missing ; do
	echo $f
    done
    echo "Make sure these are added to your PATH."
fi

######################################################################
#                                                                    #
#  Clean up.							     #
#                                                                    #
######################################################################

cd $top
CleanUp

######################################################################
#                                                                    #
#  Make all the source.						     #
#                                                                    #
######################################################################

#
#  Warn the user.
#
cat << 'EOM'

I'll make all the main source and object files.
If this is the first time, it will probably take a few minutes.
EOM
Wait

#
#  Make all the stuff.
#
make all

exit 0
