#!/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 -p install/bin install/lib install/include
mkdir build
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:$LDFLAGS
export CPPFLAGS=-I$C3HOME/install/include:$CPPFLAGS
export PATH=$C3HOME/install/bin:$PATH


tar -zxvf coreutils-6.12.tar.gz || exit 1
cd coreutils-6.12
./configure --prefix=$C3HOME/install
make
cd src
cp sort $C3HOME/install/bin/
cd ..
cd ..

tar -zxvf db-4.7.25.tar.gz || exit 1
cd db-4.7.25
cd build_unix
../dist/configure --prefix=$C3HOME/install
make
make install
cd ..
cd ..

tar -zxvf Python-2.7.1.tgz || exit 1
cd Python-2.7.1
./configure --prefix=$C3HOME/install --enable-shared
make
make install
cd ..

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

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

# LibXML, LibXSLT, lxml python interface

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

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

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

tar -zxvf python-dateutil-1.5.tar.gz || exit 1
cd python-dateutil-1.5
python ./setup.py build
python ./setup.py install
cd ..

# Really only want snowball stemmer, but install everything
tar -xzvf zopyx.txng3.ext-3.3.1.tar.gz
cd zopyx.txng3.ext-3.3.1
python ./setup.py build
python ./setup.py install
easy_install -UZ .

# NOTE WELL: Using patched version to fix segfault in stemmer
#tar -zxvf zopyx.textindexng3-4.0.1-cheshire3.tar.gz || exit 1
#cd zopyx.textindexng3-4.0.1
#python ./setup.py build
#python ./setup.py install
#easy_install -UZ .
#easy_install -UZ . # once more for luck ;)

echo "from zopyx.txng3.ext.stemmer import *" > $C3HOME/install/lib/python2.7/site-packages/txngstemmer.py
echo "" >> $C3HOME/install/lib/python2.7/site-packages/txngstemmer.py
cd ..

# NOTE WELL:  ZSI, SRW and PyZ moved to web package

# NOTE WELL:  Cheshire3 is now a module with setup.py
tar -xzvf cheshire3-1.0.0b36.tar.gz || exit 1
cd cheshire3-1.0.0b36
python setup.py install
cd ..

# out of build dir
cd ..

tar -xzvf build/cheshire3-base-1.0.0b36.tgz

# POST INSTALL Configuration

export SORTPATH=`which sort`

# Autoconfig some stuff
cd cheshire3
cd configs
sed -e s@/home/cheshire/install/bin/sort@$SORTPATH@ serverConfig.xml > serverConfig.xml.new
sed -e s@/home/cheshire@$C3HOME@ serverConfig.xml.new > serverConfig.xml

rm serverConfig.xml.new
cd ..
cd ..

# test existence and uncompress sub packages
if [ -f cheshire3-web.tgz ]
  then
    cd build
    tar -zxvf ../cheshire3-web.tgz  
    mv build-web.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.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.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.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.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.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 C3HOME=$C3HOME"
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 "***************************************************************"

