#!/bin/bash

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

. /etc/netflow/install.conf


read -p "Do you want drop database $DATABASE and remove all data tables? [y/N]:" dropdb

if [ "x$dropdb" == "xy" -o "x$dropdb" == "xY" ]; then
  mydb_create=0
  while (( !$mydb_create ))
  do
    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 root username for access to mysql database [root]:" username
    read -p "Type root password for access to mysql database []:" password

    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 "CREATE DATABASE IF NOT EXISTS $DATABASE;" | $MYSQL -s
    mydb_create=$(( ! $? ))
  done
  echo -n "Removing database $DATABASE and all data tables. Please, wait a moment...";
  echo "DROP DATABASE IF EXISTS $DATABASE;" | $MYSQL -s
  echo "DONE."
fi


if [ -n $NETFLOWDIR ]; then
  rm -rf "$NETFLOWDIR/backup" &> /dev/null
fi


find /etc/ | grep netflow.conf | while read FILE; do
  rm "$FILE" &> /dev/null
done

if [ -d /etc/apache ]; then
  find /etc/apache -type f | while read FILE; do
    cat "$FILE" | grep -v "netflow.conf" > /tmp/netflow_remove.bak
    mv /tmp/netflow_remove.bak "$FILE" &> /dev/null
  done
fi

if [ -d /etc/httpd ]; then
  find /etc/httpd -type f | while read FILE; do
    cat "$FILE" | grep -v "netflow.conf" > /tmp/netflow_remove.bak
    mv /tmp/netflow_remove.bak "$FILE" &> /dev/null
  done
fi

if [ -x /usr/sbin/update-rc.d ]; then
  /usr/sbin/update-rc.d -f nfcd remove
fi

if [ -x /sbin/chkconfig ]; then
  /sbin/chkconfig --del nfcd &> /dev/null
fi

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

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

if [ -x /etc/init.d/apache-ssl ]; then
  /etc/init.d/apache-ssl restart
fi

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

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

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