#!/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/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 use the backup directory $NETFLOWDIR/backup!"
    exit 1
fi
if [ ! -d "$NETFLOWDIR/tables" ]; then
    echo "Directory $NETFLOWDIR/tables with table definition doesn'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."

sql_query() {
  message="$1"
  query="$2"
  
  ERRCODE=0
  echo -n "$message"
  echo "$query" | $MYSQL $DATABASE &>/dev/null
  ERRCODE=$?
  
  if [ $ERRCODE -eq 0 ]; then
    echo " DONE."
  else
    echo " FAILED!!!"
  fi
}


sql_import() {
  message="$1"
  impfile="$2"
  
  ERRCODE=0
  echo -n "$message"
  cat "$impfile" | $MYSQL $DATABASE &>/dev/null
  ERRCODE=$?
  
  if [ $ERRCODE -eq 0 ]; then
    echo " DONE."
  else
    echo " FAILED!!!"
  fi
}



LS1=`ls "$NETFLOWDIR/backup" | grep "\.sql$" | sed "s/\.sql$//"`
for TABLE in $LS1
do
    if [ ! -f "$NETFLOWDIR/tables/$TABLE.sql" ]
    then
	sql_query "Dropping $TABLE (unused table)..." "DROP TABLE IF EXISTS $TABLE;"
    fi
done


LS2=`ls "$NETFLOWDIR/tables" | grep "\.sql$" | sed "s/.sql$//"`
for TABLE in $LS2
do
    if [ ! -f "$NETFLOWDIR/backup/$TABLE.sql" ]
    then
	sql_import "Creating $TABLE..." "$NETFLOWDIR/tables/$TABLE.sql"
        if [ -f "$NETFLOWDIR/data/$TABLE.sql" ]
        then
	    sql_import "Creating $TABLE (data)..." "$NETFLOWDIR/data/$TABLE.sql"
        fi
    fi
done



if [ -n "$lowner" -a -n "$lkey" ]; then
  sql_query "Setting up license owner..." "REPLACE x_global SET name='license_owner',value='$lowner';"
  sql_query "Setting up license kry..." "REPLACE x_global SET name='license_id',value='$lkey';"
else
  echo "License owner and/or licence key unchanged."
fi

sql_query "Setting up version [$VERSION]..." "REPLACE x_global (name,value) VALUES ('version','$VERSION');"

#EOF
