#!/bin/bash

BASE_VERSION='1.0.0b36'
WEB_VERSION='1.0.0b18'
SQL_VERSION='1.0.0b12'
FORMATS_VERSION='1.0.0b6'
GRAPH_VERSION='1.0.0b1'
GRID_VERSION='1.0.0b1'
CORPUS_VERSION='1.0.0b1'
DATAMINING_VERSION='1.0.0a1'
TEXTMINING_VERSION='1.0.0a1'

HAVEWGET=`wget --version | grep "GNU Wget"`

if [ ! "$HAVEWGET" ]
  then
    echo "***************************************************************"
    echo ""
    echo "You do not appear to have either wget installed"
    echo "Unable to download packages for update"
    echo "Please remedy this situation before trying again"
    echo ""
    echo "***************************************************************"
    exit 1
fi

ls -1 build/cheshire3-1.0.0*.tar.gz > /dev/null 2>&1
if [ "$?" != "0" ]
  then
    echo "***************************************************************"
    echo ""
    echo "You appear to currently be running an older version of Cheshire3"
    echo "This cannot be updated using this script."
    echo "Please download and install the latest version from scratch."
    echo ""
    echo "***************************************************************"
    exit 1
fi

cd build

echo "Updating cheshire3 (base package)"
wget http://www.cheshire3.org/download/latest/src/cheshire3-$BASE_VERSION.tar.gz
tar -xzf cheshire3-$BASE_VERSION.tar.gz
cd cheshire3-$BASE_VERSION
python setup.py install --force
cd ..

ls -1 cheshire3.web-1.0.0*.tar.gz > /dev/null 2>&1
if [ "$?" = "0" ]
  then
    echo "Updating cheshire3.web"
    wget http://www.cheshire3.org/download/latest/src/cheshire3.web-$WEB_VERSION.tar.gz
    tar -xzf cheshire3.web-$WEB_VERSION.tar.gz
    cd cheshire3.web-$WEB_VERSION
    python setup.py install --force
    cd ..
fi

ls -1 cheshire3.sql-1.0.0*.tar.gz > /dev/null 2>&1
if [ "$?" = "0" ]
  then
    echo "Updating cheshire3.sql"
    wget http://www.cheshire3.org/download/latest/src/cheshire3.sql-$SQL_VERSION.tar.gz
    tar -xzf cheshire3.sql-$SQL_VERSION.tar.gz
    cd cheshire3.sql-$SQL_VERSION
    python setup.py install --force
    cd ..
fi

ls -1 cheshire3.formats-1.0.0*.tar.gz > /dev/null 2>&1
if [ "$?" = "0" ]
  then
    echo "Updating cheshire3.formats"
    wget http://www.cheshire3.org/download/latest/src/cheshire3.formats-$FORMATS_VERSION.tar.gz
    tar -xzf cheshire3.formats-$FORMATS_VERSION.tar.gz
    cd cheshire3.formats-$FORMATS_VERSION
    python setup.py install --force
    cd ..
fi

ls -1 cheshire3.graph-1.0.0*.tar.gz > /dev/null 2>&1
if [ "$?" = "0" ]
  then
    echo "Updating cheshire3.graph"
    wget http://www.cheshire3.org/download/latest/src/cheshire3.graph-$GRAPH_VERSION.tar.gz
    tar -xzf cheshire3.graph-$GRAPH_VERSION.tar.gz
    cd cheshire3.graph-$GRAPH_VERSION
    python setup.py install --force
    cd ..
fi

ls -1 cheshire3.grid-1.0.0*.tar.gz > /dev/null 2>&1
if [ "$?" = "0" ]
  then
    echo "Updating cheshire3.grid"
    wget http://www.cheshire3.org/download/latest/src/cheshire3.grid-$GRID_VERSION.tar.gz
    tar -xzf cheshire3.grid-$GRID_VERSION.tar.gz
    cd cheshire3.grid-$GRID_VERSION
    python setup.py install --force
    cd ..
fi

ls -1 cheshire3.corpus-1.0.0*.tar.gz > /dev/null 2>&1
if [ "$?" = "0" ]
  then
    echo "Updating cheshire3.corpus"
    wget http://www.cheshire3.org/download/latest/src/cheshire3.corpus-$CORPUS_VERSION.tar.gz
    tar -xzf cheshire3.corpus-$CORPUS_VERSION.tar.gz
    cd cheshire3.corpus-$CORPUS_VERSION
    python setup.py install --force
    cd ..
fi

ls -1 cheshire3.datamining-1.0.0*.tar.gz > /dev/null 2>&1
if [ "$?" = "0" ]
  then
    echo "Updating cheshire3.datamining"
    wget http://www.cheshire3.org/download/latest/src/cheshire3.datamining-$DATAMINING_VERSION.tar.gz
    tar -xzf cheshire3.datamining-$DATAMINING_VERSION.tar.gz
    cd cheshire3.datamining-$DATAMINING_VERSION
    python setup.py install --force
    cd ..
fi

ls -1 cheshire3.textmining-1.0.0*.tar.gz > /dev/null 2>&1
if [ "$?" = "0" ];
  then
    echo "Updating cheshire3.textmining"
    wget http://www.cheshire3.org/download/latest/src/cheshire3.textmining-$TEXTMINING_VERSION.tar.gz
    tar -xzf cheshire3.textmining-$TEXTMINING_VERSION.tar.gz
    cd cheshire3.textmining-$TEXTMINING_VERSION
    python setup.py install --force
    cd ..
fi

echo "****************************************************************"
echo
echo "Update Complete."
echo
echo "If you have updated cheshire3.web (check above) you should now restart"
echo "Apache HTTPD to ensure that older code is dropped from memory."
echo
echo "***************************************************************"

