#!/bin/sh

AUSER="root"
APWD=""
USER="snort"
PWD="porkpie"
ARCH_DB="snort_archive"
SENSOR="localhost"

if [ "$1" != "OK" ];
then
echo ""
echo "Check the variables at the beginning of this script before running."
echo "The AUSER and APWD are for the adminstrator account of MySQL to drop"
echo "and create/recreate the archive db."
echo ""
echo "Be aware that the database \"$ARCH_DB\" will be removed and recreated"
echo "if this script is allowed to run. If this is OK with you, rerun this"
echo "script with the single argument of \"OK\""
echo ""
exit 1
fi

echo ""
echo -n "Dropping and recreating $ARCH_DB database..."
if [ "$APWD" != "" ];
then
cat <<++ | mysql -f -u $AUSER -p$APWD
USE mysql;
DROP DATABASE $ARCH_DB;
GRANT ALL PRIVILEGES ON $ARCH_DB.* TO $USER@$SENSOR IDENTIFIED BY '$PWD' WITH GRANT OPTION;
FLUSH PRIVILEGES;
CREATE DATABASE $ARCH_DB;
++
else
cat <<++ | mysql -f -u $AUSER
USE mysql;
DROP DATABASE $ARCH_DB;
GRANT ALL PRIVILEGES ON $ARCH_DB.* TO $USER@$SENSOR IDENTIFIED BY '$PWD' WITH GRANT OPTION;
FLUSH PRIVILEGES;
CREATE DATABASE $ARCH_DB;
++
fi
echo "Done."
echo ""

echo -n "Setting up snort schema in $ARCH_DB database..."
zcat snort_mysql.gz | mysql -u $USER -p"$PWD" snort_archive
echo "Done."
echo ""

# You can comment this section if you are doing a lot
# of repeated testing...
echo -n "Loading snortdb-extra in $ARCH_DB database (takes a while)..."
zcat snortdb-extra.gz | mysql -u $USER -p"$PWD" snort_archive
echo "Done."
echo ""

exit 0
