#!/bin/bash

C3HOME=`pwd`

# Check for C compiler - nothing works without this
export HAVECC=`cc --version | grep "[34]\.[0-9]\.[0-9]"`
if [ ! "$HAVECC" ]
  then
    echo "***************************************************************"
    echo ""
    echo "You do not appear to have a C compiler."
    echo "Unable to install Cheshire3 pre-requisites"
    echo "Please remedy this situation before trying to install again"
    echo ""
    echo "***************************************************************"
    exit 1
fi

mkdir cheshire3
mkdir build
mkdir -p install/bin install/lib install/include
cd build
tar -zxvf ../cheshire3-base.tgz || exit 1

export LD_LIBRARY_PATH=$C3HOME/install/lib:$LD_LIBRARY_PATH
export LDFLAGS=-L$C3HOME/install/lib
export CPPFLAGS=-I$C3HOME/install/include
export PATH=$C3HOME/install/bin:$PATH


# FC4 has 5.2.1, earlier than this has big efficiency bug
export HAVESORT=`sort --version | grep "5.2.1"`

# Fedora Core 5 has even newer version of sort
# Versions: 5.9[1234]
# Latest version is 6.9 
# Fedora Core 7 has 6.9 by default

if [ ! "$HAVESORT" ]
then
  export HAVESORT=`sort --version | grep "[56].[79]"`    
fi

if [ ! "$HAVESORT" ]
then
  tar -zxvf coreutils-6.9.tar.gz || exit 1
  cd coreutils-6.9
  ./configure --prefix=$C3HOME/install
  make
  cd src
  cp sort ../../install/bin/
  cd ..
  cd ..
else
  echo "Found recent sort utility, not building."
fi

FORCEBDB=`echo "$@" | grep "\-\-force\-bdb";`
export HAVEBDB=`locate libdb-4`

# 4.5.20 doesn't compile with Python 2.5
# as Oracle removed several definitions (including DB_RECORDCOUNT)
# Doh!
# Force install Cheshire3 packaged version
# Python 2.5.2 seems to have resolved the problem
#export HAVEBDB=""

if [ "$FORCEBDB" -o ! "$HAVEBDB" ]
then
  tar -zxvf db-4.4.20.tar.gz || exit 1
  cd db-4.4.20
  cd build_unix
  ../dist/configure --prefix=$C3HOME/install
  make
  make install
  cd ..
  cd ..
else
  echo "Found recent Berkeley DB install, not building."
fi

# Python 2.5.2, Feb 2008
# =ucs2 required for 64bit ?
tar -zxvf Python-2.5.2.tgz || exit 1
cd Python-2.5.2
./configure --prefix=$C3HOME/install --enable-shared --enable-unicode=ucs2
make
make install
cd ..

# latest setuptools required for lxml
# install before all other python modules

tar -zxvf setuptools-0.6c8.tar.gz || exit 1
cd setuptools-0.6c8/
python ./setup.py build
python ./setup.py install
cd ..

# 4Suite 1.0.2 Dec 2006
tar -zxvf 4Suite-XML-1.0.2.tar.gz || exit 1
cd 4Suite-XML-1.0.2
python ./setup.py config --prefix=$C3HOME/install
python ./setup.py build
python ./setup.py install
cd ..

# LibXML, LibXSLT, lxml python interface

tar -zxvf libxml2-2.6.30.tar.gz || exit 1
cd libxml2-2.6.30
./configure --prefix=$C3HOME/install
make
make install
cd ..

tar -zxvf libxslt-1.1.22.tar.gz || exit 1
cd libxslt-1.1.22
./configure --prefix=$C3HOME/install
make
make install
cd ..

tar -zxvf lxml-2.0.2.tar.gz || exit 1
cd lxml-2.0.2
python ./setup.py build
python ./setup.py install
cd ..

# Numarray 1.5.2, 2006 
### Can use numpy with numpy.numarray compatability layer
### Orange still uses numeric :/ (which is not longer supported at all)

tar -zxvf numarray-1.5.2.tar.gz || exit 1
cd numarray-1.5.2
sed -e 's/raise ValueError("Type %s has already been registered" % name)/pass/' Lib/numerictypes.py > numerictypes.py.TEMP
mv numerictypes.py.TEMP Lib/numerictypes.py
python ./setup.py config build
python ./setup.py config install
cd ..


# 2.0 breaks many things with SRW :/
### 1.0 slated for upgrade to latest ZSI
tar -zxvf ZSI-1.7.tar.gz || exit 1
cd ZSI-1.7
python ./setup.py build
python ./setup.py install
cd ..

tar -zxvf PyZ3950-2.07-jph.tgz || exit 1
cd PyZ3950
cp lex.py yacc.py $C3HOME/install/lib/python2.5/site-packages
python ./setup.py build
python ./setup.py install
cd ..

tar -jxvf python-dateutil-1.4.tar.bz2 || exit 1
cd python-dateutil-1.4
python ./setup.py build
python ./setup.py install
cd ..

tar -zxvf SRW-1.1.5.tgz || exit 1
cd SRW
python ./setup.py build
python ./setup.py install
cd ..

# Really only want snowball stemmer, but install everything
tar -zxvf TextIndexNG3-3.1.14.tar.gz || exit 1
cd TextIndexNG3
cd extension_modules
python ./setup.py build
python ./setup.py install
echo "from zopyx.txng3.stemmer import *" > $C3HOME/install/lib/python2.5/site-packages/txngstemmer.py
echo "" >> $C3HOME/install/lib/python2.5/site-packages/txngstemmer.py
cd ..
cd ..

# edsu's opensearch package
tar -zxvf opensearch-0.4.tar.gz || exit 1
cd opensearch-0.4
python ./setup.py build
python ./setup.py install
cd ..

# out of build dir
cd ..

tar -zxvf build/cheshire3-0.9.10-base.tgz || exit 1
export SORTPATH=`which sort`

# Autoconfig some stuff
cd cheshire3
cd configs
sed -e s@%%%C3HOME%%%@$C3HOME@ serverConfig.xml.TMPL > serverConfig.xml.new
sed -e s@%%%SORTPATH%%%@$SORTPATH@ serverConfig.xml.new > serverConfig.xml
rm serverConfig.xml.new
rm serverConfig.xml.TMPL
cd ..
cd ..

# test existence and uncompress
if [ -f cheshire3-web.tgz ]
  then
    cd build
    tar -zxvf ../cheshire3-web.tgz  
    mv build-web-0.9.10.sh ../build-web.sh
    cd ..
    chmod 755 build-web.sh
    ./build-web.sh
fi

if [ -f cheshire3-textmining.tgz ] 
  then
    cd build
    tar -zxvf ../cheshire3-textmining.tgz  
    mv build-textmining-0.9.10.sh ../build-textmining.sh
    cd ..
    chmod 755 build-textmining.sh
    ./build-textmining.sh
fi

if [ -f cheshire3-sql.tgz ]
  then
    cd build
    tar -zxvf ../cheshire3-sql.tgz  
    mv build-sql-0.9.10.sh ../build-sql.sh
    cd ..
    chmod 755 build-sql.sh
    ./build-sql.sh
fi

if [ -f cheshire3-datamining.tgz ] 
  then
    cd build
    tar -zxvf ../cheshire3-datamining.tgz  
    mv build-datamining-0.9.10.sh ../build-datamining.sh
    cd ..
    chmod 755 build-datamining.sh
    ./build-datamining.sh
fi

if [ -f cheshire3-grid.tgz ] 
  then	
    cd build
    tar -zxvf ../cheshire3-grid.tgz      
    mv build-grid-0.9.10.sh ../build-grid.sh
    cd ..
    chmod 755 build-grid.sh
    ./build-grid.sh
fi
if [ -f cheshire3-formats.tgz ]
  then
    cd build
    tar -zxvf ../cheshire3-formats.tgz  
    mv build-formats-0.9.10.sh ../build-formats.sh
    cd ..
    chmod 755 build-formats.sh
    ./build-formats.sh
fi


echo "****************************************************************"
echo
echo "Build Complete."
echo
echo "You MUST make these environment changes to your shell permanent:"
echo
echo "export LD_LIBRARY_PATH=$C3HOME/install/lib:\$LD_LIBRARY_PATH"
echo "export LD_RUN_PATH=$C3HOME/install/lib:\$LD_RUN_PATH"
echo "export PATH=$C3HOME/install/bin:\$PATH"
echo
echo "***************************************************************"

