#!/bin/sh


# Check SRW bytecompile
# Add Stemmer.py wrapper for zopyx/txng3/stemmer.so

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

export HAVESORT=`sort --version | grep "5.2.1"`

# Fedora Core 5 has even newer version of sort
# Versions: 5.9[1234]
if [ ! "$HAVESORT" ]
then
  export HAVESORT=`sort --version | grep "5.9"`    
fi

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

export HAVEBDB=`locate libdb-4`

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

# Expat no longer needed, it seems

# Python 2.4.3, March 29 2006
tar -zxvf Python-2.4.3.tgz
cd Python-2.4.3
./configure --prefix=$C3HOME/install --enable-shared --enable-unicode
make
make install
cd ..

echo `pwd`

# 4Suite 1.0b3, Dec 11 2005
tar -zxvf 4Suite-XML-1.0b3.tar.gz
cd 4Suite-1.0b3
python ./setup.py config --prefix=$C3HOME/install
python ./setup.py build
python ./setup.py install
cd ..

# Numarray 1.5.1, 2006 
tar -zxvf numarray-1.5.1.tar.gz
cd numarray-1.5.1
python ./setup.py config build
python ./setup.py config install
cd ..


# RC2 breaks many things
tar -zxvf ZSI-1.7.tar.gz
cd ZSI-1.7
python ./setup.py build
python ./setup.py install
cd ..

tar -zxvf PyZ3950-2.05.tgz
cd PyZ3950
cp lex.py yacc.py $C3HOME/install/lib/python2.4/site-packages
python ./setup.py build
python ./setup.py install
cd ..

# 2006
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-3.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.9.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.4/site-packages/txngstemmer.py
echo "" >> $C3HOME/install/lib/python2.4/site-packages/txngstemmer.py
cd ..
cd ..


# out of build dir
cd ..

tar -zxvf build/cheshire3-0.9b2-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
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-grid.tgz ] 
then	
    cd build
    tar -zxvf ../cheshire3-grid.tgz  
    mv build-grid.sh ..
    cd ..
    chmod 755 build-grid.sh
    ./build/build-grid.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/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-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 "***************************************************************"



