#!/bin/csh
## $local/com/install_package
## http://www.cs.bham.ac.uk/research/poplog/com/install_package
## Install a linux poplog system to simulate the Birmingham setup
## Aaron Sloman ( 20 Sep 1999 )
## 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.
## 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 ("$1x" == "x" || "$1" == "-help" ) then

    echo ""
    echo "To install foo.tar.gz give the command:"
    echo ""
    echo "   install_package foo"
    echo ""

    exit
endif

set tardir=`pwd`


if ( $?poplocal && -d $poplocal ) then
    echo "Installing $1 in $poplocal/local"
else
echo ""
echo "CANNOT INSTALL $1: either the poplocal environment variable is not set
echo "  or the directory does not exist"
echo ""

exit 0
endif

if ( -d $poplocal/local ) then

    echo "Installing in $poplocal/local"

else
    echo "creating $poplocal/local"
    mkdir -p $poplocal/local
    # make sure others can read it
    echo "making it world readable"
    chmod 755 $poplocal/local
endif


cd $poplocal/local

if ( ! -d lib ) then
    echo "creating local/lib directory"
    mkdir lib
    echo "making it world readable"
    chmod 755 lib
endif

if ( -f $tardir/$1.tar.gz ) then
    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
endif

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

echo 'contents of $poplocal/local/'$1

ls -l $1

if ( -e $1/$1.p ) then
    echo ""
    echo linking "$1/$1.p to local/lib directory (links may already exist)"
    cd lib
    ln -s ../$1/*.p .
    echo ""
endif


echo ""
echo "$1.tar.gz" installed
echo ""
