#!/bin/bash

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

. /etc/netflow/install.conf

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

mydb_ok=0
while (( !$mydb_ok ))
do
    if [ ! -f /netflow/single ]; then
      cat <<EOF1
Now enter hostname of primary database. In default do not enter any
value, because the primary database is on the same machine as the
web server. Next parameter to enter is the database port number
(the default value is empty). Next enter the username and password,
use the same username and password as are configured in the primary
database.

EOF1
      read -p "Type hostname for access to primary mysql database []:" hostname    
      read -p "Type port for access to primary mysql database []:" port    
      read -p "Type username for access to primary mysql database [root]:" username
      read -p "Type password for access to primary mysql database []:" password
    fi
    MYSQLOPTION="$MYSQLOPT"
    if [ -z "$username" ]; then
	username="root"
	MYSQLOPTION="$MYSQLOPTION -u $username"
    fi
    if [ -n "$hostname" ]; then
	MYSQLOPTION="$MYSQLOPTION --host=$hostname"
    fi
    if [ -n "$port" ]; then
	MYSQLOPTION="$MYSQLOPTION --port=$port"
    fi
    if [ -n "$password" ]; then
	MYSQLOPTION="$MYSQLOPTION --password=$password"
    fi

    MYSQL="$MYSQL_BIN $MYSQLOPTION"
    MYSQLDUMP="$MYSQLDUMP_BIN $MYSQLOPTION"
    echo "show tables;" | $MYSQL -s $DATABASE &> /dev/null
    mydb_ok=$(( ! $? ))
done

unit_id="0"
if [ ! -f /netflow/single ]; then
  cat <<EOF2
Now you will see a list of configured units. Each unit is a corresponding
server on which you can run one or more collectors. Enter the unit ID on
the installation computer. This unit ID is unique and can be used by only
one server! In other words, each server has unique unit ID. For example
press 1<ENTER>.

EOF2
  while (( "$unit_id" == "0" )); do
    echo "select id,name FROM x_unit ORDER BY id" | $MYSQL $DATABASE
    read -p "Select ID of currently installing unit:" unit_id    
    if echo $unit_id | egrep -v "^[0-9]+$" &> /dev/null; then 
      unit_id="0"
    fi
  done
else
  unit_id="1"
fi

echo "Currently selected unit ID is $unit_id."

if [ -n "$hostname" ]; then
  CONF_HOSTNAME="hostname=$hostname"
fi

if [ -n "$port" ]; then
  CONF_PORT="port=$port"
fi

if [ -n "$username" ]; then
  CONF_USERNAME="username=$username"
else
  CONF_USERNAME="username=root"
fi

if [ -n "$password" ]; then
  CONF_PASSWORD="password=$password"
fi

if [ -n "$database" ]; then
  CONF_DATABASE="database=$database"
else
  CONF_DATABASE="database=nfx"
fi


if [ -f "$NFC_CONFIG" ]; then
  OPT_RMEM_MAX=`cat "$NFC_CONFIG"|grep '^[[:blank:]]*rmem_max'`
  OPT_WRITE_ROWS=`cat "$NFC_CONFIG"|grep '^[[:blank:]]*write_rows'`
  OPT_SHM_SIZE=`cat "$NFC_CONFIG"|grep '^[[:blank:]]*shm_size'`
  OPT_INDEXING=`cat "$NFC_CONFIG"|grep '^[[:blank:]]*indexing'`
  OPT_LOG_TYPE=`cat "$NFC_CONFIG"|grep '^[[:blank:]]*type'`
  OPT_LOG_FACILITY=`cat "$NFC_CONFIG"|grep '^[[:blank:]]*facility'`
  OPT_LOG_LEVEL=`cat "$NFC_CONFIG"|grep '^[[:blank:]]*level'`
fi

if [ -z "$OPT_RMEM_MAX" ]; then
  OPT_RMEM_MAX="rmem_max=5000000"
fi

if [ -z "$OPT_WRITE_ROWS" ]; then
  OPT_WRITE_ROWS="write_rows=2000"
fi

if [ -z "$OPT_SHM_SIZE" ]; then
  OPT_SHM_SIZE="shm_size=100000000"
fi

if [ -z "$OPT_INDEXING" ]; then
  OPT_INDEXING="indexing=1"
fi

if [ -z "$OPT_LOG_TYPE" ]; then
  OPT_LOG_TYPE="type=SYSLOG"
fi

if [ -z "$OPT_LOG_FACILITY" ]; then
  OPT_LOG_FACILITY="facility=LOG_DAEMON"
fi

if [ -z "$OPT_LOG_LEVEL" ]; then
  OPT_LOG_LEVEL="level=LOG_NOTICE"
fi



echo "Creating config file $NFC_CONFIG ..."


cat <<EOF > $NFC_CONFIG
###
### Configuration file for NetFlow Collector
### Run $0 to reconfigure
###

[main]
#Identification of unit must correspond with web interface Unit ID!
unit_id=$unit_id


#Maximum allowed memory for receiving netflow socket buffer (64000-10000000)
#rmem_max=5000000
$OPT_RMEM_MAX


#Write rows buffer (used for database inserts) (1000-10000)
#write_rows=2000
$OPT_WRITE_ROWS


#Shared memory buffer (10000000-500000000)
#shm_size=100000000
$OPT_SHM_SIZE


#Hourly table indexing 0=disable 1=enable(default)
#indexing=1
$OPT_INDEXING




[database]
#Database server host name
#hostname=netflow.mydomain.com"
$CONF_HOSTNAME

#Databse port number
#port=3306
$CONF_PORT

#Database username
#username=root
$CONF_USERNAME

#Database password
#password=secret
$CONF_PASSWORD

#Database name
#database=nfx
$CONF_DATABASE



[log]
#Logging type (NONE,QUICK,STDERR,SYSLOG,CONSOLE,SYSCON)
#type=SYSLOG
$OPT_LOG_TYPE


#Logging facility (LOG_DAEMON,LOG_LOCAL0 - 7,LOG_USER)
#facility=LOG_DAEMON
$OPT_LOG_FACILITY


#Logging level (LOG_EMERG,LOG_ALERT,LOG_CRIT,LOG_ERR,LOG_WARNING,LOG_NOTICE,LOG_INFO,LOG_DEBUG)
#level=LOG_NOTICE
$OPT_LOG_LEVEL


EOF

if [ -d "$NETFLOWDIR/modules" ]; then
  LSMODULE=`ls "$NETFLOWDIR/modules" | grep "\.so$"`
  if [ "x$LSMODULE" != "x" ]; then
    for MODULE in $LSMODULE
    do
      echo "Found module $MODULE - Adding this module into $NFC_CONFIG file ..."
      echo $MODULE >> $NFC_CONFIG
    done
  fi
fi


if [ ! -f /usr/lib/libmysqlclient.so.16 ]; then
  if [ -f /usr/lib/libmysqlclient.so.20 ]; then
    echo "Creating symlink to libmysqlclient.so.16..."
    ln -s /usr/lib/libmysqlclient.so.20 /usr/lib/libmysqlclient.so.16
  fi
  if [ -f /usr/lib/libmysqlclient.so.19 ]; then
    echo "Creating symlink to libmysqlclient.so.16..."
    ln -s /usr/lib/libmysqlclient.so.19 /usr/lib/libmysqlclient.so.16
  fi
  if [ -f /usr/lib/libmysqlclient.so.18 ]; then
    echo "Creating symlink to libmysqlclient.so.16..."
    ln -s /usr/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.16
  fi
  if [ -f /usr/lib/libmysqlclient.so.17 ]; then
    echo "Creating symlink to libmysqlclient.so.16..."
    ln -s /usr/lib/libmysqlclient.so.17 /usr/lib/libmysqlclient.so.16
  fi
fi
if [ ! -f /usr/lib/mysql/libmysqlclient.so.16 ]; then
  if [ -f /usr/lib/mysql/libmysqlclient.so.20 ]; then
    echo "Creating symlink to libmysqlclient.so.16..."
    ln -s /usr/lib/mysql/libmysqlclient.so.20 /usr/lib/mysql/libmysqlclient.so.16
  fi
  if [ -f /usr/lib/mysql/libmysqlclient.so.19 ]; then
    echo "Creating symlink to libmysqlclient.so.16..."
    ln -s /usr/lib/mysql/libmysqlclient.so.19 /usr/lib/mysql/libmysqlclient.so.16
  fi
  if [ -f /usr/lib/mysql/libmysqlclient.so.18 ]; then
    echo "Creating symlink to libmysqlclient.so.16..."
    ln -s /usr/lib/mysql/libmysqlclient.so.18 /usr/lib/mysql/libmysqlclient.so.16
  fi
  if [ -f /usr/lib/mysql/libmysqlclient.so.17 ]; then
    echo "Creating symlink to libmysqlclient.so.16..."
    ln -s /usr/lib/mysql/libmysqlclient.so.17 /usr/lib/mysql/libmysqlclient.so.16
  fi
fi

if [ -x /sbin/ldconfig ]; then
  /sbin/ldconfig &> /dev/null
fi

if [ -x /usr/sbin/update-rc.d ]; then
  /usr/sbin/update-rc.d nfcd defaults 97 03
else
  if [ -x /sbin/chkconfig ]; then
    /sbin/chkconfig --add nfcd
  else
    echo "Scripts update-rc.d and/or chkconfig are not available - If you want to start nfcd "
    echo "process during boot sequence, manually create symlinks on /etc/init.d/nfcd"
    echo "from start up directories."
  fi
fi

if [ -x "/etc/init.d/nfcd" ]; then
  /etc/init.d/nfcd restart
fi
