#!/bin/bash
# install-AMD64-poplog
# Aaron Sloman
# 6 Jun 2008
##
# The AMD64 version of Poplog v15.61* (currently v15.6102) is
#available from
#   http://www.cs.bham.ac.uk/research/projects/poplog/v15.61-amd64
# This script assumes you have already fetched the tar file:
#
#   http://www.cs.bham.ac.uk/research/poplog/v15.61-amd64/bham-linux-poplog-v15.61-amd64.tar.gz
#
# The script installs it in /usr/local/poplog64 and sets up links from
# /usr/local/bin and /usr/local/man/man1
#
# It assumes you are running this script in the directory containing
# the tar file.

## CHANGE THIS FOR NEW VERSIONS
popversion=v15.61-amd64
export popversion

echo "TESTING SECURITY SETTINGS USING sysctl"

## Set this false here. It may be set true below
usesetarch=false

## Start testing security settings that may required
## use of 'setarch'

## Added '-e' to suppress errors. A.S. 11 Oct 2007
vaspace=`/sbin/sysctl -n -e kernel.randomize_va_space`

execshield=`/sbin/sysctl -n -e kernel.exec-shield`

# echo $vaspace

# echo $execshield

if [ "$vaspace" == "1" ] || [ "$execshield" == "1" ]
then

    echo "making usesetarch true"

    usesetarch="true"
fi

export usesetarch

echo usesetarch  $usesetarch


## Remember where the poplog tar file is

tardir=`pwd`

echo ""
echo "Installing poplog from tar bundle in $tardir"

## Create Poplog directory and define some environment variables
## Assumes that either a directory has been passed in as argument or
## the default should be used

    rootdir=/usr/local/poplog64
## for testing
##    rootdir=/tmp/p1

mkdir $rootdir $rootdir/$popversion $rootdir/local

chmod 755 $rootdir $rootdir/$popversion $rootdir/local

export usepop=$rootdir/$popversion

## poplocalbin is probably no longer needed
## Location for local saved images
## Version specific
export poplocalbin=$usepop/poplocalbin

cd $rootdir

mkdir $poplocalbin

export poplocal=$rootdir

export local=$poplocal/local

echo "  poplocal is $poplocal"
echo "  local    is $local"
echo "  usepop   is $usepop"
#echo "  poplocalbin is $poplocalbin"


### NOW UNPACK TAR FILE

cd $rootdir

# Create a useful symbolic link in $rootdir, after first removing old one
# if it exists
rm -f current-poplog
## let 'current-poplog' be a link to version being installed
ln -s $popversion current-poplog


## not all versions of 'tar' understand the 'z' option

echo "zcat $tardir/bham-linux-poplog-v15.61-amd64.tar.gz | tar xf -"
zcat $tardir/bham-linux-poplog-v15.61-amd64.tar.gz | tar xf -

echo "UNPACKING POPLOG TAR FILE COMPLETE"

echo "Main poplog system installed in $usepop"

echo "Top-level contents of $usepop:"
ls -l $usepop
echo "======================================================================="
echo ""

echo "Now running scripts provided in $usepop/INSTALL"

cd $usepop/INSTALL

echo "Running the 'pre-check' script CHECK_LINUX_FACILITIES_64bit"

## set defaults

motif=true
startup=bham
links=1


motifok="true"
xwinok="true"

./CHECK_LINUX_FACILITIES_64bit

# now check exit status of that command
#  0 = X11 stuff and Motif etc. all available
#  1 = X11 stuff OK but not Motif
#  2 = X11 stuff and Motif not OK

exitstatus=$?

if [ "$exitstatus" == "0" ]
then
    echo "Good all linux facilities present"

elif [ "$exitstatus" == "1" ]
then

    # can use X window stuff, but not motif

    motifok="false"
    motif="false"

elif [ "$exitstatus" == "2" ]
then

    # cannot use X window stuff

    motifok="false"
    motif="false"
    xwinok="false"

fi

echo "Xwindow stuff OK? $xwinok ||| MotifOK? $motifok "

echo "======================================================================="
echo "Completed the 'pre-check' script CHECK_LINUX_FACILITIES64bit"

echo "Run ./CREATE_SCRIPTS $rootdir"

./CREATE_SCRIPTS $rootdir

echo "Startup scripts created in $rootdir/bin"

ls -l $usepop/bin/poplog*

echo "poplog and poplog.sh startup scripts Done"


echo "======================================================"

echo "directories in $usepop/pop "
ls -l $usepop/pop/

echo "======================================================"
echo "Now rebuilding system images"

. $usepop/pop/com/poplog.sh

export popsrc=$usepop/pop/src
export popsys=$usepop/pop/pop

## Now link poplog.
## Changed 2 Apr 2007
## There are different stages. The first stage just selects the
## appropriate command to copy to $popcom/mkstartup. It chooses
## either $usepop/pop/com/mkstartup.bham
## or     $usepop/pop/com/mkstartup.core
## This used to be done by running a script to do the linking.
## Instead it is now done 'inline' below.
## After that the linking is done by invoking
## LINK_USING_NEWPOP with argument motif, nomotif or nox

echo "going to INSTALL dir $usepop/INSTALL "
cd $usepop/INSTALL

echo "Choose version of mkstartup"

if [ $xwinok == "false" ]
then
    echo "X unusable. Cannot use bham startup, so switch to 'original'."
    startup="orig"
fi

if [ "$startup" == "bham" ]
then

    echo "Selecting 'bham' version of mkstartup"

    ##    ./LINK_LIKE_BHAM

    ## Set up bham script for making saved images
    cp -p $usepop/pop/com/mkstartup.bham  $usepop/pop/com/mkstartup

    echo "Bham version of \$usepop/pop/com/mkstartup set up"

else

    echo "Selecting 'core' version of mkstartup"

    ##    ./LINK_LIKE_CORE

    ## Restore core script for making saved images
    cp -p $usepop/pop/com/mkstartup.core $usepop/pop/com/mkstartup

    echo "Original version of \$usepop/pop/com/mkstartup set up"

fi

date

if [ $motif == "true" ]
then

    ### echo " Run LINK_MOTIF_POPLOG"
    ### ./LINK_MOTIF_POPLOG

    echo running "./LINK_USING_NEWPOP motif"

    if [ $usesetarch == "true" ]
    then

         setarch i386 -R ./LINK_USING_NEWPOP motif

    else
         ./LINK_USING_NEWPOP motif

    fi

    echo "done LINK MOTIF POPLOG"

elif [ $xwinok == "true" ]
then
    echo "Cannot use motif, but will try linking against X"
    echo running "./LINK_USING_NEWPOP nomotif"

    if [ $usesetarch == "true" ]
    then

         setarch i386 -R ./LINK_USING_NEWPOP nomotif

    else

        ./LINK_USING_NEWPOP nomotif

    fi

    echo "finished ./LINK_USING_NEWPOP nomotif"
    ### not needed any more
    ###echo "Run .LINK_NOMOTIF_POPLOG"
    ###./LINK_NOMOTIF_POPLOG

else

    echo "Cannot link with Xwindow graphical facilites"
    echo running "./LINK_USING_NEWPOP nox"

    if [ $usesetarch == "true" ]
    then

         setarch i386 -R ./LINK_USING_NEWPOP nox

    else

        ./LINK_USING_NEWPOP nox

    fi


    echo "finished ./LINK_USING_NEWPOP nox"

fi
    echo "==================================================="
    echo ""

date


## Check for success of LINK command
if [ -f $usepop/pop/lib/psv/startup.psv ]
then
    echo "Install $1 $2 $3 $4 worked OK"
    echo "Saved images in $usepop/pop/lib/psv/"
else
    echo "PROBLEM: Install $1 $2 $3 $4 did not work"
    echo "This could be due to a problem accessing motif libraries"
    echo "Try repeating installation using 'nomotif' instead of 'motif'"

    echo "Exiting installation"

    exit 1
fi

date

if [ "$links" == "1" ]
then
    echo "Installing link for running poplog"

    ### create directories if they do not exist

    mkdir -p /usr/local/bin /usr/local/man/man1


    rm -f /usr/local/bin/poplog


    ln -s $usepop/bin/poplog.sh /usr/local/bin/poplog

    ls -l /usr/local/bin/poplog

    echo ""
    date
    echo "Installing man files"

    cd /usr/local/man/man1
    rm -f clisp.1 poplog.1 xved.1 pml.1 pop11.1 pop_lisp.1 pop_prolog.1 ved.1 clisp.p poplog.html poplog.unix.medium.gif
    ln -s $usepop/man/man1/*.1 .
    ln -s $usepop/man/man1/poplog.html .
    ln -s $usepop/man/man1/poplog.unix.medium.gif .

    ls -lt | head -12 | grep lrw
    echo ""

else
    echo "Executable and man files not linked"

fi

echo "done INSTALL POPLOG FOR LINUX + PC in $rootdir using $1 $2"

if [ $usesetarch == "true" ]
then

    echo "============================================================"
    echo ""
    echo "WARNING: ON THIS LINUX SYSTEM IT IS NECESSARY TO USE THE PREFIX"
    echo "      setarch i386 -R  "
    echo "WHEN RUNNING POPLOG COMMANDS"
    echo "THE MAIN COMMANDS (pop11, ved, xved, clisp, prolog, pml)"
    echo "ARE ALIASED TO DO THIS"
    echo "FOR MORE INFORMATION SEE"
    echo " http://www.cs.bham.ac.uk/research/poplog/freepoplog.html#fedoraprob"
    echo ""
    echo "============================================================"

fi

date
