#!/bin/sh

## $usepop/pop/com/install_package
## http://www.cs.bham.ac.uk/research/poplog/com/install_package
## Install a linux poplog package
## Aaron Sloman ( 20 Sep 1999 )
## 8 Jan 2005
##      Changed to use $usepop/pop/packages
##      as target directory
##      Creates $usepop/pop/packages/lib if necessary
##
## 3 Jan 2005
##      Changed
##          to use bash,
##          to allow full file name,  (including .tar.gz)
##      and to allow a list of files to be installed
##
## 31 Oct 2003
##      Changed to assume that $poplocal already set so that it can be
##      used with different locations for poplog.
## 12 Feb 2003
##      Changed to give a warning if run without an argument
##      or with '-help' argument
## 17 Nov 2002
##      altered header to be more accurate. Now mentioned in
##      http://www.cs.bham.ac.uk/research/poplog/freepoplog.html
## 23 Aug 2001 altered to use gunzip -c instead of zcat, because
##             some sun sites have a zcat that only handles .Z files
##             Also altered to check if poplocal exists, and abort otherwise
##             Creates $poplocal/local if necessary


if [[ ! ${1}  ||  "x$1" == "x-help"  ]]; then

    echo ""
    echo "To install foo.tar.gz give the command:"
    echo ""
    echo "   install_package foo"
    echo ""
    echo " (Multiple packages can be specified.)"

    exit
fi

tardir=`pwd`

if [[ ! ${usepop} ]]; then
    echo ""
    echo "CANNOT INSTALL $1: the usepop environment variable is not set"
    echo ""

    exit 0
else
    echo "Installing $1 in $usepop/pop/packages"
fi

if [ -d $usepop/pop/packages ]; then

    echo "$usepop/pop/packages exists"

else
    echo "creating $usepop/pop/packages"
    mkdir -p $usepop/pop/packages
    # make sure others can read it
    chmod 755 $usepop/pop/packages
fi


cd $usepop/pop/packages

if [ ! -d lib ]; then
    echo "creating packages/lib directory"
    mkdir lib
    # making it world readable
    chmod 755 lib
fi

echo ""

while [[ ${1} ]] ;
do
    nosuffix="unknown"

    if [ -f $tardir/$1 ]; then

        nosuffix="false"

        echo "Unpacking $1"
        gunzip -c $tardir/$1 | tar xf -

    elif [ -f $tardir/$1.tar.gz ] ; then

        nosuffix="true"
        echo "Unpacking $1.tar.gz"
        gunzip -c $tardir/$1.tar.gz | tar xf -

    else
        echo ""
        echo "No such file as $1.tar.gz"
        echo ""
        exit 0
    fi

    ## Should now check if $usepop/pop/packages/$1/*.p exists, and if so
    ## link the file(s) to $usepop/pop/packages/lib/

    if [ $nosuffix == "true" ]; then
        echo 'contents of $usepop/pop/packages/'$1

        ls -l $1

        if [ -e $1/$1.p ]; then
            echo ""
            echo linking "$1/$1.p to packages/lib directory (links may already exist)"

            ln -s ../$1/$1.p lib

            echo ""
        fi
    fi

    echo "$1 installed"
    echo ""

    shift
done


echo "======= install_package finished ======="
echo ""
