#!/bin/bash
# http://www.cs.bham.ac.uk/research/projects/poplog/v15.65/get-and-install-pre-linked-v15.65-poplog
# Aaron Sloman
# http://www.cs.bham.ac.uk/~axs/

## 10 Oct 2015
## Get and install prelinked version of poplog

## This can be run by either of
##
##  get-prelinked-fedora-poplog
##  get-prelinked-ubuntu-poplog

## after they have installed distro-specific libraries needed, they run
## this, which fetches the tar file
##
## http://www.cs.bham.ac.uk/research/projects/poplog/prelinked-v15.65-linux-poplog.tar.gz
##
##   That provides a pre-linked basepop11, which has to be produced on another
##   system (e.g. an older linux, or on 32 bit linux?). Starting from basepop11
##   it allows all the required saved images (e.g. for xved, prolog, lisp,
##   standard ML, and some demos to be created, ready for use).
##
##   However the lack of a working linker means that this package is not suitable
##   for core poplog development requiring a newly linked basepop11. That has to
##   be done in another environment.


####HISTORY
# 26 Dec 2011
#   Version for v15.65
# 11 Aug 2010
#   Version for v15.6.4
# 5 Aug 2009
#   Version for v15.6301
# 29 Jun 2009
#   Version for v15.63
# 3 Dec 2008
# Altered for poplog version 15.62 ('setarch' no longer needed)
# 7 Jun 2008
# Altered to redirect all error output to install.log
# 11 Apr 2007
###

# Either run this script or run the steps separately. It should
# fetch and install poplog in /usr/local/poplog, and put some
# easily removable links in /usr/local/bin and /usr/local/man
# so you need write access to those two directories (or you
# can run this as super user).

echo "Running: get-and-install-prelinked-v15.65-poplog"

## CHANGE THIS FOR NEW VERSIONS
popversion=v15.65
export popversion

popversionmajor=prelinked-v15.65
export popversionmajor

echo popversion $popversion

echo popversionmajor  $popversionmajor

# Find a suitable place to download stuff from the free poplog site
# when you run this it will create a sub-directory $popversionmajor
# of /usr/local/poplog and install poplog there.

echo ""
echo ""

echo "Make sure /usr/local/poplog/ exists"

if [ -d /usr/local/poplog ] ;

then
    echo "Directory /usr/local/poplog exists"

else
    echo "mkdir /usr/local/poplog"
    mkdir /usr/local/poplog
fi

## Rename an existing installation of poplog, if it exists

if [ -d /usr/local/poplog/$popversionmajor ] ;
then
    echo "old version installed, so rename it"
    echo "mv /usr/local/poplog/$popversionmajor /usr/local/poplog/old-{$popversionmajor}"
    mv /usr/local/poplog/$popversionmajor /usr/local/poplog/old-$popversionmajor

    echo ""
    echo "ls -l /usr/local/poplog"

    ls -l /usr/local/poplog
fi


## Now create directory for the new installation

echo "mkdir /usr/local/poplog/$popversionmajor"

mkdir /usr/local/poplog/$popversionmajor

echo "ls -ltr"

ls -ltr

echo ""
echo "Make a link and a local/lockfile directory"

echo "ln -s $popversionmajor /usr/local/poplog/current-poplog"
ln -s $popversionmajor /usr/local/poplog/current-poplog

echo "mkdir -p /usr/local/poplog/local/lockfile"

mkdir -p /usr/local/poplog/local/lockfile

echo ""
echo ""

echo "Fetching documentation file"
##echo "wget http://www.cs.bham.ac.uk/research/projects/poplog/latest-poplog/$popversionmajor/AREADME.txt"
echo "wget http://www.cs.bham.ac.uk/research/projects/poplog/latest-poplog/AREADME.txt"

## remove old version if present
rm -f AREADME.txt

wget http://www.cs.bham.ac.uk/research/projects/poplog/latest-poplog/AREADME.txt

echo ""
echo ""

ls -l AREADME.txt

echo "FETCHING CHECKING SHELL SCRIPT"

## remove old version if present
rm -f CHECK_LINUX_FACILITIES

echo "wget http://www.cs.bham.ac.uk/research/projects/poplog/latest-poplog/CHECK_LINUX_FACILITIES"

wget http://www.cs.bham.ac.uk/research/projects/poplog/latest-poplog/CHECK_LINUX_FACILITIES

echo ""

echo "Making it executable"
echo "chmod 755 CHECK_LINUX_FACILITIES"
chmod 755 CHECK_LINUX_FACILITIES

## run it

./CHECK_LINUX_FACILITIES

## need to use environment variables it sets?

sleep 3

echo ""

echo "PAUSING"
sleep 3

echo "GET TAR FILE"
echo ""

## remove old version if present (Could just re-name?)
echo "REMOVE OLD VERSION OF TAR FILE"
echo ""
echo "rm -f $popversionmajor-linux-poplog.tar.gz"
rm -f $popversionmajor-linux-poplog.tar.gz
echo ""
echo "FETCHING POPLOG TAR FILE. About 18 Mbytes. May take some time."
echo "E.g. prelinked-v15.65-linux-poplog.tar.gz"

echo "wget http://www.cs.bham.ac.uk/research/poplog/${popversionmajor}-linux-poplog.tar.gz"

wget http://www.cs.bham.ac.uk/research/poplog/${popversionmajor}-linux-poplog.tar.gz

ls -l *.tar.gz

echo "pausing"
sleep 5

olddir=`pwd`

echo "cd /usr/local/poplog/"

cd /usr/local/poplog/
pwd

echo "Unpacking Poplog Tar file into `pwd`"

echo "tar xfz $olddir/${popversionmajor}-linux-poplog.tar.gz"

tar xfz $olddir/${popversionmajor}-linux-poplog.tar.gz

echo "Set up links in /usr/local/poplog"

echo "ln -s $popversionmajor current-poplog"

ln -s $popversionmajor current-poplog

echo "ln -s current-poplog/INSTALL ."
ln -s current-poplog/INSTALL .

echo "ln -s current-poplog/bin ."
ln -s current-poplog/bin .

echo "poplog for bash, etc."
echo "ln -s current-poplog/bin/poplog.sh poplog.sh"
ln -s current-poplog/bin/poplog.sh poplog.sh

echo "poplog for csh, tcsh, etc."
echo "ln -s current-poplog/bin/poplog poplog"
ln -s current-poplog/bin/poplog poplog

sleep 3

echo "Set up Poplog environment variables"

echo "source bin/poplog.sh"

source bin/poplog.sh

echo "the 'usepop' environment should have been set now"

echo "usepop = $usepop"

sleep 3

echo "now complete linking"

echo "cd current-poplog/INSTALL"
cd current-poplog/INSTALL

echo "current directory:"
pwd

echo "Files here:"

ls -l

echo ""
echo ""

echo "popsavelib should be empty now"

echo "ls -l $ popsavelib"

ls -l $popsavelib

echo "Build the saved images:"

echo "./LINK_WITHOUT_NEWPOP"

echo "Put log of linking in $olddir/LINK.log"

./LINK_WITHOUT_NEWPOP > LINK.log

mv LINK.log $olddir

echo "Log of linking is in $olddir/LINK.log"

sleep 3

echo "popsavelib should not be empty now"

echo "ls -l $ popsavelib"

ls -l $popsavelib

sleep 3

echo "Created Poplog saved images in $popsavelib"

echo ""
echo ""
echo "FINISHED!"
echo ""
echo "You can read the install.log file if you are curious."
echo "PLEASE SEND THE install.log file IF REPORTING INSTALLATION PROBLEMS"
echo "====="
echo "There are lots of warning messages printed while compiling"
echo "common lisp, prolog and pml which you can ignore."
echo "You can email the install.log file to A.Sloman if you have"
echo "problems with installation. But please include details of hardware"
echo "and operating system, including version."
echo " ... Pausing... "
echo ""
echo ""
echo ""

###echo "NB NB NB The file 'install.log' is in the directory BELOW this one"

echo ""
echo ""
echo ""
sleep 5
echo "You can can now 'cd /usr/local/poplg/$popversionmajor/pop/current-poplog/bin/demos'"
echo "then read the file AREADME-DEMOS.txt to see  how to test poplog"
echo "E.g. run eliza, or run a graphical pop11 demo."
echo ""

echo ""
echo "Report Problems learning/using pop11 to pop-forum@cs.bham.ac.uk"
echo ""
