#!/bin/sh
#   Copyright (C) 1998, 1999, 2000, 2002  Los Alamos National Laboratory,
#   Copyright (C) 1998, 1999, 2000, 2002  CodeSourcery, LLC
#
#   This file is part of FreePOOMA.
#
#   FreePOOMA is free software; you can redistribute it and/or modify it
#   under the terms of the Expat license.
#
#   This program is distributed in the hope that it will be useful, but
#   WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Expat
#   license for more details.
#
#   You should have received a copy of the Expat license along with
#   FreePOOMA; see the file LICENSE.

##########################################################################
# POOMA script to install POOMA
#
# Usage:
#
#   makeinstall <SUITE> <installdir> <arch> [<extensions>]
#
# where
#
#   <SUITE> = the suite where files should be read from.
#   <installdir> = the directory where files should be installed.
#   <arch> = the architecture subdir where libs should be installed.
#   <extensions> = the extensions to install
#
# NOTE: This must be invoked from the top-level of the POOMA tree.
##########################################################################


### Get our current directory, and make sure it is the top-level dir

topdir=`pwd`

if [ ! -d $topdir/src -o ! -d $topdir/lib ]; then
  echo "Error: This script must be run from the top level of the POOMA tree."
  exit 1
fi


### Make sure we have the right arguments

if [ "$#" != "4" -a "$#" != "5" ]; then
  echo "Usage: $0 <SUITE> <installdir> <arch> <libext> [<extensions>]"
  exit 1
fi

suite=$1
libext=$4
libbase=pooma
libname=lib$libbase
libfull=$libname.$libext

extensions=""
if [ "$#" != "4" ]; then
  extensions=$5
fi

libfilename=$libname$extensions.$libext

if [ ! -d lib/$suite -o ! -f lib/$suite/$libfilename ]; then
  echo "Error: Cannot find lib/$suite suite directory or $libfilename library file."
  exit 1
fi


### Create installation directory tree

installdir=$2
srcdir=$installdir/src
htmldir=$installdir/html

echo "Creating installation directory tree ..."
if [ ! -d $installdir ]; then
  mkdir -p $installdir
  if [ ! -d $installdir ]; then
    echo "Error: Cannot create installation directory $installdir."
    exit 1
  fi
fi

arch=$3
archdir=$installdir/$arch
libdir=$archdir/lib
bindir=$archdir/bin

for d in $srcdir $htmldir $archdir $libdir $bindir; do
  if [ ! -d $d ]; then
    mkdir $d
    if [ ! -d $d ]; then
      echo "Error: Cannot create directory $d."
      exit 1
    fi
  fi
done

cd $archdir
for d in src html ; do
  if [ ! -d $d ]; then
    ln -s ../$d $d
  fi
done
cd $topdir


### Copy source files to src directory

echo "Copying src files to $installdir ..."
srclisth=`find src -name \*.h -print | grep -v tests | grep -v CVS | grep -v arch`
srclistc=`find src -name \*.cpp -print | grep -v tests | grep -v CVS | grep -v arch`
tar cf - $srclisth $srclistc | (cd $installdir ; tar xvf - )


### Copy library files to lib directory

echo "Copying lib files to $libdir ..."

makefilename=Makefile.$libbase$extensions
hdrdirname=PoomaConfiguration$extensions
configfilename=PoomaConfiguration.h

rm -rf $libdir/$makefilename $libdir/$libfilename $libdir/$hdrdirname
cp lib/$suite/$makefilename $libdir/$makefilename
cp lib/$suite/$libfilename $libdir/$libfilename
mkdir $libdir/$hdrdirname
cp lib/$suite/$configfilename $libdir/$hdrdirname/$configfilename


### Copy binary files to bin directory

# none yet


### Create links to default version of library, if necessary

cd $libdir
if [ ! -f $libfull ]; then
  echo "Creating link from $libfull --> $libfilename ..."
  ln -s $libfilename $libfull
fi
if [ ! -f Makefile.$libbase ]; then
  echo "Creating link from Makefile.$libbase --> $makefilename ..."
  ln -s $makefilename Makefile.$libbase
fi
if [ ! -r PoomaConfiguration ]; then
  echo "Creating link from PoomaConfiguration --> $hdrdirname ..."
  ln -s $hdrdirname PoomaConfiguration
fi

# ACL:rcsinfo
#  ----------------------------------------------------------------------
#  $RCSfile: makeinstall,v $   $Author: richi $
#  $Revision: 1.9 $   $Date: 2004/11/02 15:56:19 $
#  ----------------------------------------------------------------------
# ACL:rcsinfo
