#!/bin/sh
#
# Albert Danial Nov 1 2003
#
# An attempt to reduce the hassle of building packages needed by tops.
#
DO_SU=0

function ask ()   # {{{1
{
   #  input:
   #           1st arg = question with default answer in [ ]
   #           2nd arg = acceptable value for a positive return
   #                     default will be anything except this value
   echo $1
   read answer
   if test $answer -a $answer = $2
   then
       return 1
   else
       return 0
   fi

   # sample use:
   #    ask "This is a question?  (y/[n])"  "y"
   #    if test $? -eq 1 ; then
   #        echo "result is affirmative"
   #    else
   #        echo "result is negative"
   #    fi
} # 1}}}
function lapack ()   # {{{1
{
    VERSION=3.0
    INSTALL_DIR=$MAIN_INSTALL_DIR/lapack
    TGZ=lapack.tgz
    cd $WORK_DIR

    if test ! -r $TGZ
    then
        echo "Cannot find $TGZ; will download"
        wget http://www.netlib.org/lapack/$TGZ
    fi
    if test ! -r $TGZ
    then
        echo "Failed to build $1 -- cannot find $TGZ"
	    return
    fi
    echo "Building $1 in $INSTALL_DIR"
    tar zxfv $TGZ
    cd LAPACK
    cp INSTALL/make.inc.LINUX make.inc
    cd BLAS/SRC
    make   # build BLAS
    cd ../..
    make
} # 1}}}
function umfpack ()   # {{{1
{
    VERSION=4.3
    INSTALL_DIR=$MAIN_INSTALL_DIR/umfpack-${VERSION}
    TGZ=UMFPACKv${VERSION}.tar.gz
    cd $WORK_DIR

    echo "Building UMFPACK in $INSTALL_DIR"
    PATCH_FILE=umfpack_patch
    if test ! -r $PATCH_FILE
    then
        echo
        echo "Copy the UMFPACK patch file, tops/src/$PATCH_FILE, to $WORK_DIR"
        echo "then repeat the build command."
        exit
    fi
    if test ! -r $TGZ
    then
        echo "Cannot find $TGZ; will download"
        wget http://www.cise.ufl.edu/research/sparse/umfpack/v${VERSION}/$TGZ
    fi
    if test ! -r $TGZ
    then
        echo "Failed to build $1 -- cannot find $TGZ"
        return
    fi

    tar zxfv $TGZ
    patch -p0 < $PATCH_FILE
    cd UMFPACKv${VERSION}
    cat UMFPACK/Doc/License
    echo
    ask "Do you agree to the UMFPACK and AMD licenses?  (y/[n])"  "y"
    if test $? -eq 1 ; then
        echo
    else
        exit
    fi
    echo
    echo "Are the Goto BLAS library and its dependents (-lxerbla, -lfrtbegin,"
    echo "-lunwind) available where they will be found without specifiying "
    ask "-L<paths>?  (These are optional for better performance.)   y/[n]"
    if test $? -eq 1 ; then
        # OK, can use -lgoto
        goto='y'
        perl -pi -e 's{^(\s*CONFIG\s*=\s*-DNBLAS)}{# $1}'     \
                AMD/Make/Make.include UMFPACK/Make/Make.include
    else
        # bummer, use slow BLAS
        goto='n'
        echo "Building with plain -lblas"
        perl -pi -e 's{^#(\s*CONFIG\s*=\s*-DNBLAS)}{$1}'  \
                AMD/Make/Make.linux   UMFPACK/Make/Make.linux
        perl -pi -e 's{^#(\s*LIB\s*=\s*-lm)}{$1}'         \
                AMD/Make/Make.linux   UMFPACK/Make/Make.linux
        perl -pi -e 's{^(\s*CONFIG\s*=\s*$)}{# $1}'       \
                AMD/Make/Make.linux   UMFPACK/Make/Make.linux
        perl -pi -e 's{^(\s*LIB\s*=\s*-lgoto)}{# $1}'     \
                AMD/Make/Make.linux   UMFPACK/Make/Make.linux
    fi
    perl -pi -e 's{^#(\s*include\s+.*?linux\s*$)}{$1}' \
            AMD/Make/Make.include UMFPACK/Make/Make.include
    cd UMFPACK
    make lib  # don't presume to have matlab
    cd ..

    if test -r UMFPACK/Lib/libumfpack.a -a AMD/Lib/libamd.a
    then
        echo
        echo "Successfully built UMFPACK !"
        if [ $goto != 'y'   -a   $goto != 'Y'   ] ; then
            echo "Note:  this version built without GOTO BLAS"
        fi
    fi

    ask "Install to $INSTALL_DIR as root? y/[n]" "y"
    if test $? -eq 1 ; then
        answer='y'
    else
        answer='n'
    fi
    cd $WORK_DIR/UMFPACKv${VERSION}
    if [ $answer != 'y'   -a   $answer != 'Y'   ] ; then
               mkdir -p $INSTALL_DIR/lib
               mkdir -p $INSTALL_DIR/include
               cp UMFPACK/Lib/libumfpack.a       $INSTALL_DIR/lib 
               cp AMD/Lib/libamd.a               $INSTALL_DIR/lib
               cp UMFPACK/Include/*.h            $INSTALL_DIR/include 
               cp AMD/Include/amd.h              $INSTALL_DIR/include
    else
        echo "Supply root password to install into $INSTALL_DIR"
        su -c "mkdir -p $INSTALL_DIR/lib                              ;
               mkdir -p $INSTALL_DIR/include                          ;
               cp UMFPACK/Lib/libumfpack.a       $INSTALL_DIR/lib     ;
               cp AMD/Lib/libamd.a               $INSTALL_DIR/lib     ;
               cp UMFPACK/Include/*.h            $INSTALL_DIR/include ;
               cp AMD/Include/amd.h              $INSTALL_DIR/include"
    fi
} # 1}}}
function metis ()   # {{{1
{
    VERSION=4.0
    INSTALL_DIR=$MAIN_INSTALL_DIR/metis-$VERSION
    TGZ=metis-${VERSION}.tar.gz
    cd $WORK_DIR

    if test ! -r $TGZ
    then
        echo "Cannot find $TGZ; will download"
        wget http://www-users.cs.umn.edu/~karypis/metis/metis/files/$TGZ
    fi
    if test ! -r $TGZ
    then
        echo "Failed to build $1 -- cannot find $TGZ"
	    return
    fi
    echo "Building $1 in $INSTALL_DIR"
    tar zxfv $TGZ
    cd metis-$VERSION
    perl -pi -e 's{(CC\s+=)\s+\S+}{$1 gcc}' Makefile.in
    make

    ask "Install to $INSTALL_DIR as root? y/[n]" "y"
    if test $? -eq 1 ; then
        answer='y'
    else
        answer='n'
    fi

    command1="mkdir -p $INSTALL_DIR/bin"
    command2="mkdir -p $INSTALL_DIR/include"
    command3="mkdir -p $INSTALL_DIR/lib"
    command4="cp -p graphchk kmetis mesh2dual mesh2nodal oemetis onmetis partdmesh partnmesh pmetis $INSTALL_DIR/bin"
    command5="cp -p libmetis.a $INSTALL_DIR/lib"
    command6="cp -p Lib/*.h $INSTALL_DIR/include"
echo $command
    if [ $answer = 'y' ] ; then
        echo "Supply root password to copy executables into $INSTALL_DIR"
        su -c "$command1; $command2; $command3; $command4; $command5; $command6"
    else 
        $command1
        $command2
        $command3
        $command4
        $command5
        $command6
    fi
} # 1}}}
function arpack ()   # {{{1
{
    NAME=ARPACK
    VERSION=96

    cd $WORK_DIR
    TGZ=arpack96.tar.gz
    if test ! -r $TGZ
    then
        echo "Cannot find $TGZ; will download"
        wget http://www.caam.rice.edu/software/ARPACK/SRC/$TGZ
    fi
    if test ! -r $WORK_DIR/$TGZ
    then
        echo "Failed to build $NAME -- cannot find $TGZ"
        return
    fi

    PGZ=patch.tar.gz
    if test ! -r $PGZ
    then
        echo "Cannot find $PGZ; will download"
        wget http://www.caam.rice.edu/software/ARPACK/SRC/$PGZ
    fi
    if test ! -r $WORK_DIR/$PGZ
    then
        echo "Failed to build $NAME -- cannot find $PGZ"
        return
    fi
    tar zxfv $WORK_DIR/$TGZ
    tar zxfv $WORK_DIR/$PGZ

    export WORK_DIR=$WORK_DIR/${NAME}
    cd $WORK_DIR

    perl -pi -e 's/^(home\s+=\s+).*?$/$1 $ENV{WORK_DIR}/' ARmake.inc
    perl -pi -e 's{^(PLAT\s+=\s+).*?$}{$1 Linux}'         ARmake.inc
    perl -pi -e 's{^(FC\s+=\s+).*?$}{$1 g77}'             ARmake.inc
    perl -pi -e 's{^(FFLAGS\s+=\s+).*?$}{$1 -O}'          ARmake.inc
    perl -pi -e 's{^(MAKE\s+=\s+).*?$}{$1 make}'          ARmake.inc
    make lib
    if test ! -r libarpack_Linux.a
    then
        echo "Failed to build $NAME "
        return
    fi

    INSTALL_DIR=$MAIN_INSTALL_DIR/${NAME}  # not ~/arpack${VERSION} !

    echo "Install to $INSTALL_DIR as root? y/[n]"
    read answer
    if [ ! $answer ] ; then
        answer='n'
    fi
    if [ $answer != 'y'   -a   $answer != 'Y'   ] ; then
                mkdir -p $INSTALL_DIR
                if test ! -d $INSTALL_DIR
                then
                    echo "Unable to create directory $INSTALL_DIR"
                    exit
                fi
                cp -p libarpack_Linux.a $INSTALL_DIR
                cp -r EXAMPLES DOCUMENTS $INSTALL_DIR
    else
        echo "Supply root password to copy $NAME library into $INSTALL_DIR"
        su -c  "mkdir -p $INSTALL_DIR;                                    \
                cp -p libarpack_Linux.a $INSTALL_DIR;                     \ 
                cp -r EXAMPLES DOCUMENTS $INSTALL_DIR"
        rm -fr $NAME
    fi

} # 1}}}
function slice ()   # {{{1
{
    VERSION=1.3.8
    INSTALL_DIR=$MAIN_INSTALL_DIR/slice-${VERSION}
    TGZ=slice-${VERSION}.tar.gz
    cd $WORK_DIR

    echo "Building $1 in $INSTALL_DIR"
    if test ! -r $TGZ
    then
        echo "Cannot find $TGZ; will download"
        # wget http://www.engelschall.com/sw/slice/distrib/$TGZ
        wget http://sofea.org/dep/$TGZ
    fi
    if test ! -r $TGZ
    then
        echo "Failed to build $1 -- cannot find $TGZ"
	    return
    fi
    tar zxfv $TGZ
    cd slice-${VERSION}
    ./configure --prefix=$INSTALL_DIR
    make
    make test
    if test $DO_SU -eq 1
    then
        echo "Supply root password to install into $INSTALL_DIR"
        su -c 'make install; \
               ln -s $INSTALL_DIR/bin/slice /usr/local/bin/slice'
    fi
} # 1}}}
function sqlite ()   # {{{1
{
    VERSION=3.2.7
    INSTALL_DIR=$MAIN_INSTALL_DIR/sqlite-$VERSION
    TGZ=sqlite-${VERSION}.tar.gz
    cd $WORK_DIR

    if test ! -r $TGZ
    then
        echo "Cannot find $TGZ; will download"
        wget http://www.sqlite.org/$TGZ
    fi
    if test ! -r $TGZ
    then
        echo "Failed to build $1 -- cannot find $TGZ"
	    return
    fi
    echo "Building $1 in $INSTALL_DIR"
    tar zxfv $TGZ
    cd sqlite-$VERSION
    CFLAGS=-O3 ./configure --prefix=$INSTALL_DIR --disable-tcl -disable-shared
    make

    ask "Install to $INSTALL_DIR as root? y/[n]" "y"
    if test $? -eq 1 ; then
        answer='y'
    else
        answer='n'
    fi
    if [ $answer != 'y'   -a   $answer != 'Y'   ] ; then
        make install
    else 
        echo "Supply root password to install into $INSTALL_DIR"
        su -c 'make install'
    fi
} # 1}}}
function triangle ()   # {{{1
{
    INSTALL_DIR=$MAIN_INSTALL_DIR/bin
    TGZ=triangle.shar.gz
    cd $WORK_DIR

    mkdir triangle
    cd    triangle
    if test ! -r $TGZ
    then
        echo "Cannot find $TGZ; will download"
        wget http://cm.bell-labs.com/netlib/voronoi/$TGZ
    fi
    if test ! -r $TGZ
    then
        echo "Failed to build $1 -- cannot find $TGZ"
	    return
    fi
    echo "Installing $1 executables in $INSTALL_DIR"
    gzip -dc $TGZ | sh
    perl -pi -e 's{^(\s*CC\s+=\s+)(.*?)$}{$1 gcc}' makefile
    make
    if test ! -r triangle
    then
        echo "Failed to build triangle "
	    return
    fi
    if test $DO_SU -eq 1
    then
      echo "Supply root password to copy triangle and showme into $INSTALL_DIR"
      su -c "cp -p triangle showme $INSTALL_DIR"
    fi
} # 1}}}
function xd3d ()   # {{{1
{
    VERSION=8.0.3
    INSTALL_DIR=$MAIN_INSTALL_DIR/xd3d-$VERSION
    TGZ=xd3d-$VERSION.tar.gz
    cd $WORK_DIR

    if test ! -r $TGZ
    then
        echo "Cannot find $TGZ; will download"
        wget http://www.cmap.polytechnique.fr/~jouve/xd3d/download.php?file=$TGZ\&type=x-gzip\&computer=src
        mv "download.php?file=$TGZ&type=x-gzip&computer=src" $TGZ
    fi
    if test ! -r $TGZ
    then
        echo "Failed to build $1 -- cannot find $TGZ"
	    return
    fi
    echo "Installing $1 in $INSTALL_DIR"
    tar zxfv $TGZ
    cd xd3d-$VERSION
    cp RULES.linux RULES
    PWD=`pwd`
    perl -pi -e "s{^(XD3D_DIR\s+=\s+)(.*?)$}{\$1 $PWD}"                RULES
    perl -pi -e "s{^(INSTALL_DIR\s+=\s+)(.*?)$}{\$1 $INSTALL_DIR/bin}" RULES
    perl -pi -e "s{^(LIBX11\s+=\s+)(.*?)$}{\$1 /usr/X11R6/lib}"        RULES
    perl -pi -e "s{^(LANGUAGE\s+=\s+)(.*?)$}{\$1 ENGLISH}"             RULES
    perl -pi -e "s{^(MEMORY_XD3D\s+=\s+)(.*?)$}{\$1 64}"               RULES
    ./makedeps
    make clean
    make
    if test ! -r bin/xd3d
    then
        echo "Failed to build $1 "
        return
    fi
    if test $DO_SU -eq 1
    then
        echo "Supply root password to copy executables into $INSTALL_DIR"
        su -c "mkdir -p $INSTALL_DIR/bin;                                \
               make install;                                             \
               ln -s $INSTALL_DIR/bin/xd3d /usr/local/bin/xd3d;          \
               ln -s $INSTALL_DIR/bin/xgraphic /usr/local/bin/xgraphic;  \
               cp -r FAQ Manuals Examples $INSTALL_DIR"
    fi
} # 1}}}
function geomview ()   # {{{1
{
    VERSION=1.8.1
    INSTALL_DIR=$MAIN_INSTALL_DIR/geomview-1.8.1
    TGZ=geomview-${VERSION}.tar.gz
    cd $WORK_DIR

    if test ! -r $TGZ
    then
        echo "Cannot find $TGZ; will download"
        wget http://ftp1.sourceforge.net/geomview/$TGZ
    fi
    if test ! -r $TGZ
    then
        echo "Failed to build $1 -- cannot find $TGZ"
	    return
    fi
    echo "Building $1 in $INSTALL_DIR"
    tar zxfv $TGZ
    cd geomview-${VERSION}
    ./configure # --with-motif=/usr/local/lesstif-0.93.91
    make
    # the oscillating mesh example:
    gcc doc/example1.c -o example1 -lm
    # create the .geomview file that points to the external example1 executable
    echo '(emodule-define "jelly fish" "./example1")' > .geomview
    # to run with two static models (plus the mesh as an external model):
    # ./geomview data/geom/dodec data/geom/tetra
} # 1}}}
function fftw ()   # {{{1
{
    VERSION=3.0.1
    INSTALL_DIR=$MAIN_INSTALL_DIR/fftw-${VERSION}
    TGZ=fftw-${VERSION}.tar.gz
    cd $WORK_DIR

    if test ! -r $TGZ
    then
        echo "Cannot find $TGZ; will download"
        wget http://www.fftw.org/$TGZ
        
    fi
    if test ! -r $TGZ
    then
        echo "Failed to build $1 -- cannot find $TGZ"
	    return
    fi
    echo "Building $1 in $INSTALL_DIR"
    tar zxfv $TGZ
    cd fftw-${VERSION}
    ./configure --prefix=$INSTALL_DIR
    make
    ask "Run 'make check' (could take a half hour)?  y/[n]"  "y"
    if test $? -eq 1 ; then
        make check
    fi
    if test $DO_SU -eq 1
    then
        echo "Supply root password to install into $INSTALL_DIR"
        su -c 'make install'
    fi
} # 1}}}

# - - - - - - main - - - - - - -

if test $# -lt 1
then
    echo "Usage: $0  <package>  <build directory>  <top level install directory>"
    echo "      Downloads (with wget), configures, compiles (with GCC),"
    echo "      and installs these tops dependencies for Linux systems:"
    for P in lapack umfpack metis arpack slice sqlite triangle \
             xd3d geomview fftw
    do
        echo "                 $P"
    done
    echo 
#   echo "                 ALL      (installs all of the above)"
#   echo 
    echo "      Example:"
    echo "         $0 umfpack  /tmp  /usr/local"
    echo "      will download and untar files under /tmp then "
    echo "      install into /usr/local/umfpack-4.3/lib/*"
    echo "                   /usr/local/umfpack-4.3/include/*"
    exit
fi
if   test $# -eq 1
then
    WORK_DIR=/tmp
    MAIN_INSTALL_DIR=/tmp
elif test $# -eq 2
then
    MAIN_INSTALL_DIR=/tmp
    WORK_DIR=$2
    if test ! -d $WORK_DIR
    then
        mkdir -p $WORK_DIR
        if test ! -d $WORK_DIR
        then
            echo "Unable to create directory $WORK_DIR"
            exit
        fi
    fi
elif test $# -eq 3
then
    WORK_DIR=$2
    if test ! -d $WORK_DIR
    then
        mkdir -p $WORK_DIR
        if test ! -d $WORK_DIR
        then
            echo "Unable to create directory $WORK_DIR"
            exit
        fi
    fi

    MAIN_INSTALL_DIR=$3
    if test ! -d $MAIN_INSTALL_DIR
    then
        mkdir -p $MAIN_INSTALL_DIR
        if test ! -d $MAIN_INSTALL_DIR
        then
            echo "Unable to create directory $MAIN_INSTALL_DIR"
            exit
        fi
    fi
fi

got_it=0
for P in lapack umfpack metis arpack slice sqlite triangle \
         xd3d geomview fftw
do
    if   test $1 = $P
    then
        got_it=1
    fi
done
if test $got_it != 1
then
    echo "Do not know how to build $1"
    exit
fi

# commented out until I can figure out how to make it work right:
if test $1 = 0 #  "ALL"
then
#    DO_SU=0  # prompts for su passwd are a drag in the middle of a loop
#    for P in lapack umfpack metis arpack slice sqlite triangle \
#             xd3d geomview fftw
#    do
#        $P
#    done
    echo huh?
else
    $1
fi
