#!/bin/sh

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:.

. /etc/netflow/install.conf

rm -rf "$NETFLOWDIR/backup" &> /dev/null
mkdir "$NETFLOWDIR/backup" &> /dev/null
mkdir "$NETFLOWDIR/patch" &> /dev/null

if [ -r /usr/share/netflow/VERSION ]; then
  VERSION=`cat /usr/share/netflow/VERSION`
fi

if [ ! -d "$NETFLOWDIR/backup" ]; then
    echo "Can't create backup directory $NETFLOWDIR/backup!"
    exit 1
fi
if [ ! -d "$NETFLOWDIR/tables" ]; then
    echo "Directory $NETFLOWDIR/tables with table definitions don't exist!"
    exit 1
fi


if [ ! -f /netflow/install.flg ]; then
  echo "Before upgrading stop all netflow processes first!!!"
  echo "Press <ENTER> to continue or Ctrl+C to break."
  read
fi

cat <<EOF1
Please enter license owner and license key. In case you want to use
a trial version request a license key on the url:

http://www.caligare.com/netflow/trial.php

The trial key will be sent immediately by email. Trial version
expires after 30 days. License owner and/or key can be changed via
web interface later.

EOF1
read -p "Type license owner []:" lowner
read -p "Type license key []:" lkey

mydb_create=0
while (( !$mydb_create ))
do
    if [ ! -f /netflow/single ]; then
      cat <<EOF2
Now enter username and password for access to primary MySQL database.
In the default installation of MySQL use username root and blank
password.

EOF2
      read -p "Type root username for access to mysql database [root]:" username
      read -p "Type root password for access to mysql database []:" password
    fi
    MYSQLOPTION="$MYSQLOPT"
    if [ -z "$username" ]; then
	MYSQLOPTION="$MYSQLOPTION -u root"
    fi
    if [ -n "$password" ]; then
	MYSQLOPTION="$MYSQLOPTION --password=$password"
    fi
    MYSQL="$MYSQL_BIN $MYSQLOPTION"
    MYSQLDUMP="$MYSQLDUMP_BIN $MYSQLOPTION"
    echo "CREATE DATABASE IF NOT EXISTS $DATABASE;" | $MYSQL -s
    mydb_create=$(( ! $? ))
done


#REPLACE INSTALL CONFIG
echo "NETFLOWDIR=\"$NETFLOWDIR\"" > /etc/netflow/install.conf
echo "MYSQL_BIN=\"$MYSQL_BIN\"" >> /etc/netflow/install.conf
echo "MYSQLDUMP_BIN=\"$MYSQLDUMP_BIN\"" >> /etc/netflow/install.conf
echo "MYSQLOPT=\"\"" >> /etc/netflow/install.conf
echo "MYSQLOPTION=\"$MYSQLOPTION\"" >> /etc/netflow/install.conf
echo "DATABASE=\"nfx\"" >> /etc/netflow/install.conf
echo "NFC_CONFIG=\"$NFC_CONFIG\"" >> /etc/netflow/install.conf
echo "NFW_CONFIG=\"$NFW_CONFIG\"" >> /etc/netflow/install.conf
echo "VERSION=\"$VERSION\"" >> /etc/netflow/install.conf



OLDVERSION=""
#TEST IF EXISTS x_global table if NO it is FIRST INSTALL
TABLECOUNT=`echo "SHOW TABLES LIKE 'x_global'" | $MYSQL -s $DATABASE`
if [ -n "$TABLECOUNT" ]; then
  OLD_VERSION=`echo "SELECT value FROM x_global WHERE name='version'" | $MYSQL -s $DATABASE`
  if [ -z "$OLD_VERSION" ]; then
    OLD_VERSION="3.0.0"
  fi
fi

export MYSQL="$MYSQL"
export MYSQLDUMP="$MYSQLDUMP"
export DATABASE="$DATABASE"
export OLD_VERSION="$OLD_VERSION"
export NETFLOWDIR="$NETFLOWDIR"

if [ -n "$OLD_VERSION" ]; then
  if [ -x "$NETFLOWDIR/patch/$OLD_VERSION.sh" ]; then
    echo -n "Patching old version $OLD_VERSION..."
    "$NETFLOWDIR/patch/$OLD_VERSION.sh"
    echo "DONE."
  fi

  if [ -x "$NETFLOWDIR/patch/any.sh" ]; then
    echo -n "Patching tables..."
    "$NETFLOWDIR/patch/any.sh"
    echo "DONE."
  fi
fi

echo -n "Tables snapshot to $NETFLOWDIR/backup "
for TABLE in `echo "SHOW TABLES like 'x%'" | $MYSQL -s $DATABASE`
do
    $MYSQLDUMP --opt $DATABASE $TABLE | grep -v "^-" &> "$NETFLOWDIR/backup/$TABLE.sql"
    echo -n "."
done
echo "DONE."




LS1=`ls "$NETFLOWDIR/backup" | grep "\.sql$" | sed "s/\.sql$//"`
for TABLE in $LS1
do
    if [ ! -f "$NETFLOWDIR/tables/$TABLE.sql" ]
    then
	echo -n "Dropping $TABLE (unused table)..."
	echo "DROP TABLE IF EXISTS $TABLE;" | $MYSQL $DATABASE &> /dev/null
	echo "DONE."
    fi
done


LS2=`ls "$NETFLOWDIR/tables" | grep "\.sql$" | sed "s/.sql$//"`
for TABLE in $LS2
do
    if [ ! -f "$NETFLOWDIR/backup/$TABLE.sql" ]
    then
	echo -n "Creating $TABLE..."
        $MYSQL $DATABASE < "$NETFLOWDIR/tables/$TABLE.sql" &> /dev/null
	echo "DONE."
        if [ -f "$NETFLOWDIR/data/$TABLE.sql" ]
        then
	    echo -n "Creating $TABLE (data)..."
    	    $MYSQL $DATABASE < "$NETFLOWDIR/data/$TABLE.sql" &> /dev/null
	    echo "DONE."
        fi
    fi
done



if [ -n "$lowner" -a -n "$lkey" ]; then
  echo -n "Setting up license owner and license key..."
  echo "REPLACE x_global SET name='license_owner',value='$lowner';" | $MYSQL -s $DATABASE &> /dev/null
  echo "REPLACE x_global SET name='license_id',value='$lkey';" | $MYSQL -s $DATABASE &> /dev/null
  echo "DONE."
else
  echo "License owner and/or licence key unchanged."
fi

echo -n "Setting up version [$VERSION]..."
echo "REPLACE x_global (name,value) VALUES ('version','$VERSION');" | $MYSQL -s $DATABASE &> /dev/null
echo "DONE."
