#!/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


echo "Creating config file $NFW_CONFIG ..."



if [ -n "$hostname" ]; then
  CONF_HOSTNAME="definehostname=$hostname"
else
  CONF_HOSTNAME="#hostname=netflow.mydomain.com"
fi

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

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


cat <<EOF > $NFW_CONFIG
<?php
###
### Configuration file for NetFlow Web Interface part
### Run $0 to reconfigure...
###

define('PRG_DBHOST',		'$hostname');
define('PRG_DBPORT',		'$port');
define('PRG_DBUSER',		'$username');
define('PRG_DBPASS',		'$password');
define('PRG_DBNAME',		'$DATABASE');
define('PRG_DBPERSIST',		'');

#$hide_logo=1;		// don't show logo
#$hide_menu=1;		// don't show menu

#define('CUSTOM_LOGIN_ERRPAGE','');	// static image or page
#define('CUSTOM_LOGIN_ERRLINK','');	// dynamic page id=error text

define('CUSTOM_TRENDS_PER_INTERFACE',1);

?>
EOF

#Make symlink (In the TGZ&RPM versions is missing this link)
ln -s "$NFW_CONFIG" "$NETFLOWDIR/web/core/config.php" &>/dev/null


if [ -f /usr/bin/nf_imexd.cron ]; then
  echo -n "Activating import/export functionality (cron daemon script)..."
  mv /usr/bin/nf_imexd.cron /etc/cron.d/nf_imexd &> /dev/null
  echo "DONE."
fi


PHPTEMPFILE="/tmp/phptempfile"


echo -n "Finding php extensions (this operation can take longer time)..."
find /usr/lib | grep '/php' > "$PHPTEMPFILE"
echo "DONE."


APACHE=""
if [ -d "/etc/apache/conf.d" ]; then
  echo "Making Apache configuration for /etc/apache"
  ln -s /etc/netflow/apache.conf /etc/apache/conf.d/netflow.conf &> /dev/null
  APACHE="1"
fi

if [ -d "/etc/apache2/conf.d" ]; then
  echo "Making Apache2 configuration for /etc/apache2"
  ln -s /etc/netflow/apache.conf /etc/apache2/conf.d/netflow.conf &> /dev/null
  APACHE="1"
fi

if [ -d "/etc/apache-ssl/conf.d" ]; then
  echo "Making Apache configuration for /etc/apache-ssl"
  ln -s /etc/netflow/apache.conf /etc/apache-ssl/conf.d/netflow.conf &> /dev/null
  APACHE="1"
fi

if [ -d "/etc/httpd/conf.d" ]; then
  echo "Making HTTPd configuration for /etc/httpd"
  ln -s /etc/netflow/apache.conf /etc/httpd/conf.d/netflow.conf &> /dev/null
  APACHE="1"
fi

if [ -z $APACHE ]; then
  if [ -d "/etc/apache/httpd.conf" ]; then
    if ! cat /etc/apache/httpd.conf | grep "Include /etc/netflow/apache.conf"; then
      echo "Adding include option to /etc/apache/httpd.conf file"
      echo "Include /etc/netflow/apache.conf" >> /etc/apache/httpd.conf &> /dev/null
    fi
    APACHE="1"
  fi
fi

if [ -z $APACHE ]; then
  if [ -d "/etc/httpd/httpd.conf" ]; then
    if ! cat /etc/httpd/httpd.conf | grep "Include /etc/netflow/apache.conf"; then
      echo "Adding include option to /etc/httpd/httpd.conf file"
      echo "Include /etc/netflow/apache.conf" >> /etc/httpd/httpd.conf &> /dev/null
    fi
    APACHE="1"
  fi
fi

if [ -z $APACHE ]; then
  echo ""
  echo "Apache configuration not found!"
  echo "Add line 'Include /etc/netflow/apache.conf' manually to apache's httpd.conf file."
  echo "Restart apache web server..."
else
  if [ -x /etc/init.d/apache ]; then
    echo "Restarting Apache daemon ..."
    /etc/init.d/apache restart
  fi
  if [ -x /etc/init.d/apache2 ]; then
    echo "Restarting Apache2 daemon ..."
    /etc/init.d/apache2 restart
  fi
  if [ -x /etc/init.d/apache ]; then
    echo "Restarting Apache-ssl daemon ..."  
    /etc/init.d/apache-ssl restart
  fi  
  if [ -x /etc/init.d/httpd ]; then
    echo "Restarting HTTPd daemon ..."  
    /etc/init.d/httpd restart
  fi  
fi

MYSQLMODULE="`cat \"$PHPTEMPFILE\"|grep 'mysql.so'`"
SNMPMODULE="`cat \"$PHPTEMPFILE\"|grep 'snmp.so'`"

if [ -z "$MYSQLMODULE" -a ! -f /etc/php.d/mysql.ini ]; then
  echo "Warning: Can't find PHP mysql.so extension... Please, install and enable this extension."
fi


if [ -z "$SNMPMODULE" -a ! -f /etc/php.d/snmp.ini ]; then
  echo "Warning: Can't find PHP snmp.so extension... Please, install and enable this extension."
fi


#In the RedHat gd.so is compiled directly into PHP library...
#if [ -z `cat "$PHPTEMPFILE"|grep 'gd.so'` ]; then
#  echo "Warning: Can't find PHP gd.so extension... Please, install and enable this extension."
#fi

rm "$PHPTEMPFILE" &> /dev/null

if [ -f '/usr/bin/nf_auth_pam' ]; then
  #SET SUID bit for external authentication tool
  chmod u+s '/usr/bin/nf_auth_pam' &>/dev/null || true
fi