#!/bin/bash

# Install file for PyXPlot version 0.5.3
# Dominic Ford
# 27/06/2006

VERSION="0.5.3"
DATE="27/06/2006"

echo " ____       __  ______  _       _      PYXPLOT"
echo "|  _ \ _   _\ \/ /  _ \| | ___ | |_    Version ${VERSION}"
echo "| |_) | | | |\  /| |_) | |/ _ \| __|   ${DATE}"
echo "|  __/| |_| |/  \|  __/| | (_) | |_"
echo "|_|    \__, /_/\_\_|   |_|\___/ \__|   Copyright (C) 2006 Dominic Ford"
echo "       |___/"

echo
echo "Installation in progress..."
echo

echo "Checking that system requirements are met..."

# Find python. Use python 2.4 if it exists. If it doesn't, then user probably
# doesn't meet system requirement, but try using python instead, just in case
# he has python 2.5.

if which python2.4 > /dev/null ; then
 export PYTHON_COMMAND=python2.4
elif which python > /dev/null ; then
 export PYTHON_COMMAND=python
else
 export PYTHON_COMMAND=false
fi

# Now find ghostview. Use gv if it exists. If it doesn't, use ggv instead.

if which gv > /dev/null ; then
 export GV_COMMAND=gv
elif which ggv > /dev/null ; then
 export GV_COMMAND=ggv
else
 export GV_COMMAND=false
 echo "WARNING: Ghostview could not be found. Installation will proceed, but X11 terminal will not be available in PyXPlot. If required, install either ghostview (gv) or Gnome-ghostview (ggv), and then re-install PyXPlot."
fi

if ${PYTHON_COMMAND} syscheck.py ; then

 echo "Unpacking PyX..."
 mkdir -p bin/py
 tar xfz PyX-0.9.tar.gz
 rm -Rf bin/py/pyx
 mv PyX-0.9/pyx bin/py
 rm -Rf PyX-0.9

 echo "Installing pyxplot..."
 cp -r src/* bin/py &> /dev/null
 echo -e "#!`which ${PYTHON_COMMAND}`"       >  bin/py/__init__.py
 echo -e "__import__('pyxplot')"             >> bin/py/__init__.py
 echo -e "VERSION='${VERSION}'"              >  bin/py/gp_version.py
 echo -e "DATE='${DATE}'"                    >> bin/py/gp_version.py
 echo -e "GHOSTVIEW='`which ${GV_COMMAND}`'" >> bin/py/gp_version.py

 chmod 755 bin/py/__init__.py
 rm -f bin/pyxplot bin/gnuplot
 ln -s `pwd`/bin/py/__init__.py bin/gnuplot
 ln -s `pwd`/bin/py/__init__.py bin/pyxplot

 cd doc
 if make -s ; then

  echo -e "\n\n* Installation successful"
  echo "* PyXPlot executable placed in directory 'bin'."
  echo "* Documentation is available in the doc directory in pdf and LaTeX formats."

 else

  echo -e "\n\n* Installation problem encountered."
  echo -e "* Please refer to error messages above for more information."

 fi

fi
