#!/bin/sh

C3HOME=`pwd`

mkdir cheshire3
mkdir build
mkdir install
mkdir install/bin
cd build
tar -zxvf ../cheshire3-base.tgz

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 


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

if [ ! "$HAVESORT" ]
then
  tar -zxvf coreutils-6.9.tar.gz
  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

export HAVEBDB=`locate libdb-4`


# 4.5.20 doesn't compile with Python 2.5
# as Oracle removed several definitions (including DB_RECORDCOUNT)
# Doh!

if [ ! "$HAVEBDB" ] 
then
  tar -zxvf db-4.4.20.tar.gz
  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, Sept 2006
# =ucs2 required for 64bit ?
tar -zxvf Python-2.5.tgz
cd Python-2.5
./configure --prefix=$C3HOME/install --enable-shared --enable-unicode=ucs2
make
make install
cd ..

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

tar -zxvf setuptools-0.6c6.tar.gz
cd setuptools-0.6c6/
python ./setup.py build
python ./setup.py install
cd ..

# 4Suite 1.0.2 Dec 2006
tar -zxvf 4Suite-XML-1.0.2.tar.gz
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.29.tar.gz
cd libxml2-2.6.29
./configure --prefix=$C3HOME/install
make
make install
cd ..

tar -zxvf libxslt-1.1.21.tar.gz
cd libxslt-1.1.21
./configure --prefix=$C3HOME/install
make
make install
cd ..

tar -zxvf lxml-1.2.1.tgz
cd lxml-1.2.1
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
cd numarray-1.5.2
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
cd ZSI-1.7
python ./setup.py build
python ./setup.py install
cd ..

tar -zxvf PyZ3950-2.07.tgz
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.1.tar.bz2
cd python-dateutil-1.1
python ./setup.py build
python ./setup.py install
cd ..

tar -zxvf SRW-1.1.4.tgz
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
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
cd opensearch-0.4
python ./setup.py build
python ./setup.py install
cd ..

# out of build dir
cd ..

tar -zxvf build/cheshire3-0.9.9-base.tgz
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.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.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.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.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.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.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 "***************************************************************"



