#!/bin/bash
##
## $local/com/INSTALL_BHAM_LINUX_POPLOG
## http://www.cs.bham.ac.uk/research/poplog/com/INSTALL_BHAM_LINUX_POPLOG
## Install poplog
##      o with or without motif
##      o with original (isl/core) startup image
##      o in default directory or specified directory
##
## RUN THIS WITH THREE ARGUMENTS.
##
##      One is
##          motif or nomotif
##
##      The second is
##          bham or original
##
##      The third is the directory in which to locate poplog
##          or default
##          (default selects /usr/local/poplog)
##
## Aaron Sloman (http://www.cs.bham.ac.uk/~axs/)
## Revised 20 Jan 2005
##  John Duncan reported typo.
##      INSTALL_POPLOG should have been INSTALL_BHAM_LINUX_POPLOG throughout
##      Now fixed
##

popversion=amd64-v15.6
export popversion

if [ "x$1" == "x" ] || [  "$1" == "-help" ] || [ "$1" == "help" ]
then

   echo ""
   echo "   Three arguments are required."
   echo "   The first should be either"
   echo "       motif"
   echo "   or"
   echo "       nomotif"
   echo "   The second should be either"
   echo "       bham"
   echo "   or"
   echo "       original"
   echo "   The third should be either an installation directory or just"
   echo "       default"
   echo "   (The default installation directory is /usr/local/poplog)"
   echo "   "
   echo "   Examples:"
   echo "       ./INSTALL_BHAM_LINUX_POPLOG motif   bham     default > install.log"
   echo "       ./INSTALL_BHAM_LINUX_POPLOG nomotif bham     ~/mypop > install.log"
   echo "       ./INSTALL_BHAM_LINUX_POPLOG motif   original /usr/local/newpoplog > install.log"
   echo "   ===="
   echo ""
   echo "   Use COMMAND > install.log to save output in file install.log"
   echo ""
   echo ""

   exit
fi

## Run this in the directory in which the poplog tar file
## has been unpacked

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

motif=unset

if [ "$1" == "motif" ];
then

    motif=true

elif [ "$1" == "nomotif" ];
then
    motif=false

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

    echo "First argument missing: should be 'motif' or 'nomotif'"
    exit 1

else
     echo $1 " not accepted: should be 'motif' or 'nomotif'"
    exit 1
fi

startup=unset

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

elif [ "$2" == "original" ];
then
    startup=orig

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

    echo "Second argument missing: should be 'bham' or 'original'"
    exit 1

else
     echo $2 " not accepted: should be 'bham' or 'original'"
    exit 1
fi


if [ "$3" == "" ] || [ "$3" == "default" ] ;
then

    rootdir=/usr/local/poplog

else

    rootdir=$3
fi

echo ""
echo "Installing $1 $2 Poplog version $popversion in: $rootdir"

export rootdir

echo "Root directory for poplog set to: $rootdir"
echo "Creating directories for poplog and local extensions there."

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

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

export usepop=$rootdir/$popversion

echo usepop = $usepop

## Location for local saved images
## Version specific
export poplocalbin=$usepop/poplocalbin

mkdir $poplocalbin

export poplocal=$rootdir

export local=$poplocal/local

echo "poplocal $poplocal ; local $local ; usepop $usepop; poplocalbin $poplocalbin"


### NOW UNPACK TAR FILE

cd $rootdir

# Create a useful symbolic link, after first removing old one
# if it exists
rm -f current-poplog
ln -s $popversion current-poplog

echo "Unpacking tar file. May take some time"

# Install linux poplog with motif

tar tvfz $tardir/bham-linux-poplog-${popversion}.tar.gz | head -5

zcat $tardir/bham-linux-poplog-${popversion}.tar.gz | tar xf -

echo "UNPACKING POPLOG TAR FILE COMPLETE"

echo "main poplog system installed in $usepop"

echo "Creating startup scripts"

cd $usepop/INSTALL

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

./CHECK_LINUX_FACILITIES

./CREATE_SCRIPTS $rootdir

ls -l $usepop/bin/poplog*

echo "poplog and poplog.sh startup scripts Done"

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

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

. $usepop/pop/com/poplog.sh

export popsrc=$usepop/pop/src
export popsys=$usepop/pop/pop
export popexternlib=$usepop/pop/extern/lib
export popcom=$usepop/pop/com

## Now link poplog.

echo "Running script to recompile AMD64 poplog"

if true ; then
cd $popsys

pwd
echo "rebuilding system images"

time $popsrc/mksyscomp -d popc poplibr poplink

#$popsrc/mknewpop

ls -l *.psv

#ln -s corepop popc
#ln -s corepop poplibr
#ln -s corepop poplink

#  exit

echo "system images should be OK now"

fi

echo "======================================================"
echo "Now recompiling"


if true ; then
cd $popexternlib
pwd

echo "mklibpop"
./mklibpop

cd $popcom
pwd

echo "mkXpw"
./mkXpw -I/usr/include/X11R6

echo "mkXpm"
./mkXpm -I/usr/include/X11R6
fi

if true ; then
cd $usepop/pop/obj
    pwd
    echo 'saving library files in old'
    mkdir old
    ls -l
    mv *.* old
fi

echo 'Recompiling base system'
date +%Y.%d.%m.%T:%N
if true ; then
cd $usepop/pop/src
    pwd
    echo 'pgcomp'
    time pgcomp *.[ps]

    echo 'pglibr'
    time pglibr -c ../obj/src.wlb *.w
#    rm *.[ow]
fi
echo 'After rebuild'
date +%Y.%d.%m.%T:%N

cd $usepop/pop/ved/src/
    pwd
    echo 'pgcomp'
    time pgcomp *.[ps]
    echo 'pglibr'
    time pglibr -c ../../obj/vedsrc.wlb *.w
    rm *.[ow]
if true ; then
cd $usepop/pop/x/src/
    pwd
    echo 'pgcomp'
    time pgcomp *.[ps]
    echo 'pglibr'
    time pglibr -c ../../obj/xsrc.wlb *.w
    rm *.[ow]

fi

# link a complete system into a newpop11 image, using pglink
cd $popsys

## if true ; then
## # pglink -nox -noved
##  pglink -nox -noved
## fi

# exit
#ls -l newpop11
# newpop11 basepop11
./corepop %nort ../lib/lib/mkimage.p -entrymain ./newpop.psv ../lib/lib/newpop.p

#$usepop/pop/src/newpop -link -x=-xm -norsv -ved

echo "======================================================"
echo "Now rebuilding system images"
echo "going to INSTALL dir $usepop/INSTALL "
cd $usepop/INSTALL

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

    echo "Selecting 'bham' version of mkstartup"

    ./LINK_LIKE_BHAM

else

    echo "Selecting 'core' version of mkstartup"

    ./LINK_LIKE_CORE

fi

if [ $motif == "true" ]
then

    echo " Run LINK_MOTIF_POPLOG"
    ./LINK_MOTIF_POPLOG

else

    echo "Run .LINK_NOMOTIF_POPLOG"
    ./LINK_NOMOTIF_POPLOG

fi

## Check for success of LINK command
if [ -f $usepop/pop/lib/psv/startup.psv ]
then
    echo "Install $1 $2 worked OK"
else
    echo "Installation did not work"
    exit 1
fi

echo "Installing link for running poplog"

rm /usr/local/bin/poplog

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

ls -l /usr/local/bin/poplog

echo "Installing man files"

cd /usr/local/man/man1
ln -s $usepop/man/man1/* .


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