#!/bin/sh

###########################################################################
# SPICE OPUS installation script
# Written by Arpad Buermen (arpadb@fides.fe.uni-lj.si)
# Faculty of Electrical Engineering, Ljubljana, Slovenia (www.fe.uni-lj.si)
###########################################################################

if [ $# -eq 0 ]
then
  echo
  echo "SPICE OPUS installation utility"
  echo "(C)1999,2006 Arpad Burmen"
  echo "  arpadb@fides.fe.uni-lj.si"
  echo
  echo "Usage: install install_prefix"
  echo
  echo "Files will be installed in install_prefix/bin"
  echo "                           install_prefix/lib/spiceopus"
  echo "                           install_prefix/src/spiceopus"
  echo "Reccomended install_prefix is /usr/local"
  echo
  echo "Warning .... previous installation in"
  echo "  install_prefix/bin"
  echo "  install_prefix/lib/spiceopus"
  echo "  install_prefix/src/spiceopus"
  echo "will be removed."
  echo "All files from installations before 2006 will also be removed."
  echo
  exit 1
fi

INSTALL_PREFIX=$1

if [ -d $1 ]
then
  echo "$1 is a directory. OK."
else
  echo
  echo "$1 is not a directory."
  echo "Type install for usage information."
  echo
  exit 1
fi

if which install
then
  echo "'install' command found."
else
  echo "'install' command not found, can't install."
  exit 1
fi

curdir=`pwd`

cd $INSTALL_PREFIX
instdir=`pwd`
cd $curdir

if [ "$curdir" == "$instdir" ]
then
  echo "ERROR: install_prefix may not be the current directory."
  echo "Run install with no options to get help." 
  exit 1
fi

UMASK_TEMP=`umask`
umask 022

echo "Installing in $INSTALL_PREFIX."

echo "  Checking for previous version of SPICE OPUS."
# Old installation files (before 2006)
if [ -d $INSTALL_PREFIX/lib/spice ]
then
  echo "    Found $INSTALL_PREFIX/lib/spice. Removing it..."
  rm -rf $INSTALL_PREFIX/lib/spice
fi
for binfile in cmpp cmpp.bin multidec multidec.bin proc2mod proc2mod.bin spice3 spice3.bin spice-config
do
  if [ -f $INSTALL_PREFIX/bin/$binfile ]
  then
    echo "    Found $INSTALL_PREFIX/bin/$binfile. Removing it..."
    rm -f $INSTALL_PREFIX/bin/$binfile
  fi
done
# New installations files
if [ -d $INSTALL_PREFIX/lib/spiceopus ]
then
  echo "    Found $INSTALL_PREFIX/lib/spiceopus. Removing it..."
  rm -rf $INSTALL_PREFIX/lib/spiceopus
fi
if [ -d $INSTALL_PREFIX/src/spiceopus ]
then
  echo "    Found $INSTALL_PREFIX/src/spiceopus. Removing it..."
  rm -rf $INSTALL_PREFIX/src/spiceopus
fi
for binfile in spiceopus.bin opus_gui.bin cmpp.bin spiceopus opus_gui cmpp spiceopus-config
do
  if [ -f $INSTALL_PREFIX/bin/$binfile ]
  then
    echo "    Found $INSTALL_PREFIX/bin/$binfile. Removing it..."
    rm -f $INSTALL_PREFIX/bin/$binfile
  fi
done


echo "Creating directories..."
install -d -m u=rwx,g=rx,o=rx $INSTALL_PREFIX/bin
chown -R root $INSTALL_PREFIX/bin
chgrp -R root $INSTALL_PREFIX/bin
install -d -m u=rwx,g=rx,o=rx $INSTALL_PREFIX/lib/spiceopus
chown -R root $INSTALL_PREFIX/lib/spiceopus
chgrp -R root $INSTALL_PREFIX/lib/spiceopus
install -d -m u=rwx,g=rx,o=rx $INSTALL_PREFIX/src/spiceopus
chown -R root $INSTALL_PREFIX/src/spiceopus
chgrp -R root $INSTALL_PREFIX/src/spiceopus


echo "Configuring..."
cat skel/spice-config.prefix >bin/spiceopus-config
echo "" >>bin/spiceopus-config
echo "SPICE_INSTALL_PREFIX=$INSTALL_PREFIX" >>bin/spiceopus-config
echo "" >>bin/spiceopus-config
cat skel/spice-config.postfix >>bin/spiceopus-config
chown root bin/*
chgrp root bin/*
chmod u=rwx-s,g=rx-s,o=rx-s bin/*
chown -R root lib
chgrp -R root lib
chmod -R u+rw-s,g+r-s,o+r-s lib
chown -R root src
chgrp -R root src
chmod -R u+rw-s,g+r-s,o+r-s src

echo "Installing spice..."
tar cf - bin | ( cd $INSTALL_PREFIX/; tar xf -; )
tar cf - lib | ( cd $INSTALL_PREFIX/; tar xf -; )
tar cf - src | ( cd $INSTALL_PREFIX/; tar xf -; )
if [ -f bin/opus_gui.bin ]
then
  cd $INSTALL_PREFIX/bin
  ln -s spiceopus-config opus_gui
else
  cd $INSTALL_PREFIX/bin
fi
ln -s spiceopus-config spiceopus
ln -s spiceopus-config cmpp

echo "Done."
echo
echo "Add $INSTALL_PREFIX/bin to your path."
echo "To start spice, type 'spiceopus'."
echo "Some info about SPICE OPUS can be found in"
echo "  $INSTALL_PREFIX/lib/spiceopus/documentation"
echo "Spice lib directory is"
echo "  $INSTALL_PREFIX/lib/spiceopus"
echo "Makefiles, includes and sources for building .cm files are in"
echo "  $INSTALL_PREFIX/src/spiceopus"
echo "Before building .cm files, edit Makefile.rules."
echo "SPICE OPUS will on cmload command start searching for the .cm file in"
echo "  $INSTALL_PREFIX/lib/spiceopus/cm"
echo ".cm files that are distributed with SPICE OPUS were installed there."
echo "For a .cm file loading example see:"
echo "  $INSTALL_PREFIX/lib/spiceopus/scripts/spinit"

umask $UMASK_TEMP
