#!/bin/sh
##
## $local/com/LINK_MOTIF_POPLOG
## http://www.cs.bham.ac.uk/research/poplog/com/LINK_MOTIF_POPLOG
## Included in $usepop/INSTALL/LINK_MOTIF_POPLOG
## Relink poplog and then rebuild saved images with motif.
##
## Aaron Sloman (http://www.cs.bham.ac.uk/~axs/)
## Revised
## 24 Dec 2004
##  Tried to make this more robust if the newpop command fails
##  Rename as V15.53f
##
## 17 Apr 2004
##  Changed to 'exit 1' if cannot find motif, or something else fails
##  Minor improvements to reporting.
##
## 1 Nov 2003
##  Changed to work with libXm.so link in $popexternlib
## 28 Oct 2003
##  Changed to assume $usepop $poplocal $poplocalbin all defined and created
##  Allowed to continue if relinking does not work.
## 23 Jul 2003
## New version of redhat 9 and precursors. Rename 15.53c
##
##  3 May 2003
##          added -norsv to newpop command
##
##  9 Mar 2003
##    Check for /usr/X11R6/lib/libXm.so otherwise exit

## This script links poplog with dependence on motif

## If the following does not work it may be necessary to run the script
##     relinking-linux-poplog
## available from
##      http://www.cs.bham.ac.uk/research/poplog/tools/relinking.linux.poplog
## if not included in your tar file

## Code that was here now in SETUPDIRS

echo ""
echo "Checking for motif"

### First check for motif
if [ -f /usr/X11R6/lib/libXm.so ]
then

    ln -sf /usr/X11R6/lib/libXm.so $popexternlib/libXm.so
    echo '   Motif found OK (libXm.so)'

elif [ -f /usr/X11R6/lib/libXm.so.3 ]
then

    ln -sf /usr/X11R6/lib/libXm.so.3 $popexternlib/libXm.so
    echo '   Motif libXm.so.3 found and linked to $popexternlib/libXm.so'
    ## Not sure the rest is needed
    rm -f $usepop/pop/com/popenv*
    cp -p $usepop/pop/com/motif3/* $usepop/pop/com
    echo 'Altered $usepop/pop/com/popenv* for Motif 3'


elif [ -f /usr/X11R6/lib/libXm.so.2 ]
then
    ln -sf /usr/X11R6/lib/libXm.so.2 $popexternlib/libXm.so
    echo '   Motif libXm.so.2 found and linked to $popexternlib/libXm.so'
    ## Not sure the rest is needed
    rm -f $usepop/pop/com/popenv*
    cp -p $usepop/pop/com/motif2/* $usepop/pop/com
    echo 'Altered $usepop/pop/com/popenv* for Motif 2'

else

    echo ""
    echo "CANNOT CONTINUE"
    echo "   /usr/X11R6/lib/libXm.so not found"
    echo "   Make sure openmotif is installed and that"
    echo "      /usr/X11R6/lib/libXm.so is a link to the latest of"
    echo "      /usr/X11R6/lib/libXm.so* "
    echo ""
    echo "    Otherwise try to install poplog WITHOUT using motif"
    echo ""
    exit 1

fi

cd $usepop

echo ""
echo "Now relinking and rebuilding poplog images"

. $usepop/pop/com/poplog.sh

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

##### START of bit that may not work on some systems, e.g. if gcc is
## missing

echo "relinking -- using $popsys/poplink_cmnd"

cd $popsys
    ./poplink_cmnd

echo "checking for newpop11"

ls -l newpop11

if [ -f newpop11 ]
then
    echo "copying newpop11 to basepop11"
    cp -p newpop11 basepop11
    rm newpop11
else
    echo "COULD NOT LINK NEWPOP11:"
    echo "The rest may work anyway."
    echo "If not, then try asking for help"
    echo "    via comp.lang.pop or pop-forum AT cs.bham.ac.uk"
fi
##### END of bit that may not work on some systems

### Now rebuild system images
# Extend $path (needed for mknewpop and mksyscomp)
PATH=$usepop/pop/pop:$PATH
export PATH

echo ""
echo "Try to rebuild newpop, poplink, poplibr, popc, system images"

echo "$popsrc/mknewpop"
$popsrc/mknewpop

echo "$popsrc/mksyscomp poplink poplibr popc"
$popsrc/mksyscomp poplink poplibr popc

echo ""
echo "System images made in popsys are:"
echo ""
ls -l $popsys/*.psv

if [ -f $popsys/newpop.psv ]
then
    echo "System images appear to have been created OK"
else
    echo "COULD NOT BUILD newpop.psv -- cannot continue"
    echo "Try asking for help"
    echo "    via comp.lang.pop or pop-forum AT cs.bham.ac.uk"
    echo ""
    exit 1
fi

echo ""
echo "======================================================"
echo ""
echo "Now run newpop to re-link with motif, rebuild indexes, etc."

    echo "$usepop/pop/src/newpop -link -x=-xm -norsv"
    $usepop/pop/src/newpop -link -x=-xm -norsv

### The following are done by the newpop command
## $popcom/mkstartup
##
## ## Needed for next items
## setenv pop_pop11 "-$popsavelib/startup.psv"
##
## $popcom/mkplog
## $popcom/mkclisp
## $popcom/mkpml
## $popcom/mkxved

echo ""
echo "Standard images made in popsavelib are:"
echo ""

ls -l $usepop/pop/lib/psv

if [ -f $usepop/pop/lib/psv/startup.psv ]
then
    echo "Startup saved image appears to have been created OK"
else

     if [ -x $popsys/basepop11 ]
     then
         echo "newpop did not work."
         echo "Try using old basepop11 with $popcom/mkstartup"
         $usepop/pop/com/mkstartup


        if [ -f $usepop/pop/lib/psv/startup.psv ]
        then
            echo "Startup saved image appears to have been created OK"
            echo "Try $popcom/makeimages, for prolog, lisp, ml "
            $usepop/pop/com/makeimages
            $usepop/pop/com/mkxved
            $usepop/pop/com/makeindexes
        else
             echo "COULD NOT BUILD startup.psv"
            echo "Try asking for help via comp.lang.pop or pop-forum AT cs.bham.ac.uk"
            exit 1
        fi

     else

         echo "COULD NOT BUILD startup.psv"
         echo "Try asking for help via comp.lang.pop or pop-forum AT cs.bham.ac.uk"
         exit 1
    fi
fi

echo ""
echo "Later delete any saved images you don't need"
echo ""

echo "done LINK_MOTIF_POPLOG FOR LINUX + PC"
echo "==================================================="
echo ""
