#!/bin/sh
# $Id: setup,v 1.11 2009/10/12 19:31:10 bhockney Exp $
# Webfwlog (C) 2003-2009 by Bob Hockney <zeus@ix.netcom.com>
#
# MySQL setup script for webfwlog
#

DB="MySQL"
PROG="mysql"

SERVER="localhost"
PORT="3306"
ADMIN_USER="root"
ADMIN_PASS=""
USER="webfwlog@localhost"
PASS="password"
WFWL_DB="webfwlog"
WFWL_OLD_DB="ulogd"
ULOG_DB="ulogd"
ULOG_TABLE="ulog"
ULOG_OLD_DB="webfwlog"
ULOG_OLD_TABLE="ulog"
ULOGD_USER="ulogd@localhost"

HAVE_WFWL_DB=0
HAVE_WFWL_REPORTS=0
HAVE_WFWL_HOSTNAMES=0
HAVE_WFWL_SERVICES=0
HAVE_WFWL_OLD_REPORTS=0
HAVE_WFWL_OLD_HOSTNAMES=0
HAVE_WFWL_OLD_SERVICES=0
HAVE_ULOG_DB=0
HAVE_ULOG_TABLE=0
HAVE_ULOG_OLD_TABLE=0

COPY_WFWL_TABLES=0
GRANTS=0
ADD_EXAMPLES=0
EXAMPLES_DIR="../examples"
OUTFILE="mysql_setup"

if (printf "")
then
	ECHO="printf %b"
else
	echo
	echo "The printf program could not be found."
	echo "printf is required in order to run setup."
	echo
	exit 1
fi

SCRIPT=""

if test -d scripts
then :
else
	$ECHO "Scripts directory not found\n"
	$ECHO "Please change the working directory to the directory containing setup\n"
	exit 1 
fi

# test client program existance
if ($PROG --version) > /dev/null 2>&1
then :
else
	$ECHO "Client program $PROG not found\n"
	$ECHO "Exiting...\n"
	exit 1
fi

get_param () {
	read N
	if test -n "$N"
	then
		RET="$N"
	else
		RET="$1"
	fi
	$ECHO "$RET"
	return 0
}

get_yes_or_no () {
	read N
	if test -z "$N"
	then
		RET=1
	else
		N=`$ECHO "$N" | sed 's/^[Yy].*/y/'`
		if test "$N" = "y"
		then
			RET=1
		else
			RET=0
		fi
	fi
	$ECHO "$RET"
	return 0
}

$ECHO "
Welcome to the webfwlog setup program for $DB.  You will be prompted
to answer some questions and for input needed to set up your $DB server
for use with webfwlog.  You will also be prompted before any changes are made.

PREREQUISITES

Before running this script you should have your $DB server installed and
running, and you will need to have login credentials for an admin user
for $DB such as 'root' (separate from the system root user).

Also, if you want to use webfwlog with ulog data you need to set up ulog
to log packets to your $DB server before setting up webfwlog.  See the
documentation for ulogd for details on setting up a ulog table.


First, the location of the $DB server and the admin user login
credentials.  If the server is not on localhost then enter the host name or
IP address where the $DB server is running.
\n"

# get database server location
# get admin user credentials

$ECHO "Enter location of $DB server (<cr>=$SERVER): "
SERVER="`get_param "$SERVER"`"

if test "$SERVER" != "localhost"
then
	$ECHO "Enter port for $DB server (<cr>=$PORT): "
	PORT="`get_param "$PORT"`"
fi

$ECHO "Enter $DB admin user (<cr>=$ADMIN_USER): "
ADMIN_USER="`get_param "$ADMIN_USER"`"

$ECHO "Enter $DB admin user password (<cr>=<blank>): "
stty -echo
ADMIN_PASS="`get_param "$ADMIN_PASS"`"
stty echo
$ECHO "\n"

# test database server existence and version

if test "$SERVER" != "localhost"
then
	PROG="$PROG -h $SERVER -P $PORT"
fi
PROG="$PROG -u $ADMIN_USER"
if test -n "$ADMIN_PASS"
then
	PROG="$PROG -p$ADMIN_PASS"
fi

if VERSION=`$PROG --batch -N -e "SELECT version() AS version"`
then :
else
	$ECHO "\n"
	$ECHO "Could not get $DB server version\n"
	$ECHO "Is the server running on $SERVER?\n"
	$ECHO "Are the credentials for $DB admin user \`$ADMIN_USER\` correct?\n"
	$ECHO "Exiting...\n\n"
	exit 1
fi

$ECHO "\n"
$ECHO "$DB server version $VERSION found on $SERVER!\n"

MAJOR=`$ECHO "$VERSION\n" | cut -d. -f1`
MINOR=`$ECHO "$VERSION\n" | cut -d. -f2`
EXTRA=`$ECHO "$VERSION\n" | cut -d. -f3 | sed -e 's/\(^[0-9]\{1,\}\).*/\1/'` 

if test $MAJOR -lt 3 \
	-o \( $MAJOR -eq 3 -a $MINOR -lt 23 \) -o \( $MAJOR -eq 3 -a $MINOR -eq 23 -a $EXTRA -lt 52 \) \
	-o \( $MAJOR -eq 4 -a $MINOR -eq 0 -a $EXTRA -lt 12 \) \
	-o \( $MAJOR -eq 4 -a $MINOR -eq 1 -a $EXTRA -lt 7 \) \
	-o \( $MAJOR -eq 5 -a $MINOR -eq 0 -a $EXTRA -lt 15 \) \
	-o \( $MAJOR -eq 5 -a $MINOR -gt 0 \) \
	-o $MAJOR -gt 5
then
	$ECHO "\n"
	$ECHO "$DB version 3.23.xx >= 3.23.52 or version 4.0.x >= 4.0.12\n"
	$ECHO "        or version 4.1.x >= 4.1.7 or version 5.0 >= 5.0.15\n"
	$ECHO "        is required for use with webfwlog\n"
	$ECHO "Exiting...\n\n"
	exit 1
fi

# query re: data layout desired (database and table names)

$ECHO "
Enter the database you want to use for the webfwlog tables.  It is recomended
to put the webfwlog tables in a separate database such as 'webfwlog'.  If the
database does not exist it will be created.  If the database exists and
existing webfwlog tables are found they will be used, otherwise new webfwlog
tables will be created in this database.
\n"
$ECHO "Enter the database to use for the webfwlog tables (<cr>=$WFWL_DB): "
WFWL_DB="`get_param "$WFWL_DB"`"

if test -n "`$PROG --batch -e "SELECT 1;" $WFWL_DB 2> /dev/null`"
then 
	$ECHO "\n"
	$ECHO "Using existing database $WFWL_DB for webfwlog tables\n"
	HAVE_WFWL_DB=1
else
	$ECHO "\n"
	$ECHO "Creating new database $WFWL_DB for webfwlog tables\n"
	HAVE_WFWL_DB=0
fi

WFWL_REPORTS="$WFWL_DB.reports"
WFWL_HOSTNAMES="$WFWL_DB.hostnames"
WFWL_SERVICES="$WFWL_DB.services"

if ($PROG -e "SELECT count(*) FROM $WFWL_REPORTS;") > /dev/null 2>&1
then 
	$ECHO "Using existing table $WFWL_REPORTS for webfwlog report definitions\n"
	HAVE_WFWL_REPORTS=1
fi

if ($PROG -e "SELECT count(*) FROM $WFWL_HOSTNAMES;") > /dev/null 2>&1
then 
	$ECHO "Using existing table $WFWL_HOSTNAMES for webfwlog hostnames cache\n"
	HAVE_WFWL_HOSTNAMES=1
fi

if ($PROG -e "SELECT count(*) FROM $WFWL_SERVICES;") > /dev/null 2>&1
then 
	$ECHO "Using existing table $WFWL_SERVICES for webfwlog services cache\n"
	HAVE_WFWL_SERVICES=1
fi

if test $HAVE_WFWL_DB -eq 0
then
	SCRIPT="${SCRIPT}--\n-- Create new database $WFWL_DB\n--\nCREATE DATABASE $WFWL_DB;\n"
fi

if test $HAVE_WFWL_REPORTS -eq 0
then
	$ECHO "Creating new table $WFWL_REPORTS for webfwlog report definitions\n"
	SCRIPT="$SCRIPT`awk "{ if (NR != 1) print | \\\"sed 's/\\\$WFWL_DB.reports/$WFWL_REPORTS/'\\\" }" scripts/wfwl_reports_create`\n"
fi

if test $HAVE_WFWL_HOSTNAMES -eq 0
then
	$ECHO "Creating new table $WFWL_HOSTNAMES for webfwlog hostnames cache\n"
	SCRIPT="$SCRIPT`awk "{ if (NR != 1) print | \\\"sed 's/\\\$WFWL_DB.hostnames/$WFWL_HOSTNAMES/'\\\" }" scripts/wfwl_hostnames_create`\n"
fi

if test $HAVE_WFWL_SERVICES -eq 0
then
	$ECHO "Creating new table $WFWL_HOSTNAMES for webfwlog hostnames cache\n"
	SCRIPT="$SCRIPT`awk "{ if (NR != 1) print | \\\"sed 's/\\\$WFWL_DB.services/$WFWL_SERVICES/'\\\" }" scripts/wfwl_services_create`\n"
fi

# TODO
# move data if requested/drop old tables/database

if test $HAVE_WFWL_REPORTS -eq 0
then
	$ECHO "
If you have existing Webfwlog tables in a different database and answer yes to
the next question you can copy existing report definitions and hostname and
service name caches from the tables in the old database to the tables in the new
database.  The existing tables will NOT be dropped, so be sure to update your
webfwlog.conf file to point to the new database.
\n"
	$ECHO "Do you want to copy existing webfwlog data to the new tables? [Y/n]: "
	COPY_WFWL_TABLES="`get_yes_or_no`"

	until test $COPY_WFWL_TABLES -eq 0 -o $HAVE_WFWL_OLD_REPORTS -eq 1 \
		-o $HAVE_WFWL_OLD_HOSTNAMES -eq 1 -o $HAVE_WFWL_OLD_SERVICES -eq 1
	do
		$ECHO "\nEnter database with existing webfwlog tables (<cr>=$WFWL_OLD_DB): "
		WFWL_OLD_DB="`get_param "$WFWL_OLD_DB"`"

		WFWL_OLD_REPORTS="${WFWL_OLD_DB}.reports"
		WFWL_OLD_HOSTNAMES="${WFWL_OLD_DB}.hostnames"
		WFWL_OLD_SERVICES="${WFWL_OLD_DB}.services"

		if ($PROG -e "SELECT count(*) FROM $WFWL_OLD_REPORTS;") > /dev/null 2>&1
		then 
			HAVE_WFWL_OLD_REPORTS=1
		fi
		if ($PROG -e "SELECT count(*) FROM $WFWL_OLD_HOSTNAMES;") > /dev/null 2>&1
		then 
			HAVE_WFWL_OLD_HOSTNAMES=1
		fi
		if ($PROG -e "SELECT count(*) FROM $WFWL_OLD_SERVICES;") > /dev/null 2>&1
		then 
			HAVE_WFWL_OLD_SERVICES=1
		fi
	done
fi

if test $HAVE_WFWL_OLD_REPORTS -eq 1 -a $HAVE_WFWL_REPORTS -eq 0
then R="$WFWL_OLD_REPORTS"
else R="$WFWL_REPORTS"
fi

if test $HAVE_WFWL_REPORTS -ne 1 -a $HAVE_WFWL_OLD_REPORTS -ne 1 || ($PROG -e "SELECT last_saved, last_accessed FROM $R;") > /dev/null 2>&1
then :
	SCRIPT="$SCRIPT`awk "{ if (NR != 1) print | \\\"sed 's/\\\$WFWL_DB.reports/$R/'\\\" }" scripts/wfwl_reports_altercol`\n"
else
	SCRIPT="$SCRIPT`awk "{ if (NR != 1) print | \\\"sed 's/\\\$WFWL_DB.reports/$R/'\\\" }" scripts/wfwl_reports_addcol`\n"
fi

if test $HAVE_WFWL_OLD_REPORTS -eq 1
then
	SCRIPT="${SCRIPT}--\n-- Copy webfwlog report definitions\n-- and hostname and service name caches to new tables\n--\n"
	SCRIPT="${SCRIPT}INSERT INTO $WFWL_REPORTS (code, description, definition, last_accessed, last_saved)
	SELECT code, description, definition, last_accessed, last_saved FROM $WFWL_OLD_REPORTS;\n"
fi

if test $HAVE_WFWL_OLD_HOSTNAMES -eq 1
then
	SCRIPT="${SCRIPT}INSERT INTO $WFWL_HOSTNAMES (refresh, ip_addr, hostname)
	SELECT refresh, ip_addr, hostname FROM $WFWL_OLD_HOSTNAMES;\n"
fi

if test $HAVE_WFWL_OLD_SERVICES -eq 1
then
	SCRIPT="${SCRIPT}INSERT INTO $WFWL_SERVICES (refresh, ip_protocol, port, service)
	SELECT refresh, ip_protocol, port, service FROM $WFWL_OLD_SERVICES;\n"
fi

if test $HAVE_WFWL_OLD_REPORTS -eq 1 -a $HAVE_WFWL_REPORTS -eq 0
then D="$WFWL_OLD_DB"
else D="$WFWL_DB"
fi

if ($PROG -e "SELECT count(*) from $D.protocols;") > /dev/null 2>&1
then
	SCRIPT="${SCRIPT}--\n-- Drop unused table protocols\n--\nDROP TABLE $D.protocols;\n"
fi

# query re: data source(s) (database, table name)
# create new database/move data if requested.

test_ulog_indexes () {
	$ECHO "$1\n" | while read N
	do
		N=`$ECHO "$N" | sed -e 's/^.*KEY \`\(.*\)\` (.*$/\1/'`
		if test -z "$N"; then continue; fi;

		if test "$x" = "$N"
		then
			$ECHO "0"
			return
		fi
	done

	return
}

add_ulog_indexes () {
	for x in saddr daddr tcp_dservices tcp_sservices udp_dservices udp_sservices 
	do
		if test -z "`test_ulog_indexes "$1"`"
		then
			if test "$x" = "saddr" -o "$x" = "daddr" -o "$x" = "tcp_dservices" \
				-o "$x" = "tcp_sservices" -o "$x" = "udp_dservices" -o "$x" = "udp_sservices"
			then
				if test -z `$ECHO $x | sed -e 's/.addr//'`
				then
					$ECHO "ALTER TABLE $ULOG_DB.$ULOG_TABLE ADD INDEX $x (ip_$x);\n"
				else
					I="`$ECHO $x | sed -e 's/\(.\{3\}_.\).*/\1/'`"
					$ECHO "ALTER TABLE $ULOG_DB.$ULOG_TABLE ADD INDEX $x (${I}port, ip_protocol);\n"
				fi
			fi
		fi
	done

	return 0;

}

$ECHO "
If you want to use webfwlog with ulog data answer yes to the next question.
You will then be prompted for the database and table name for your logs.

When found, the ulog table will be checked for indexes used to improve
performance with webfwlog, which will be added if not found.  The ulog table
will also be checked for the local_time and local_hostname columns used by
the LOCAL plugin of ulogd, which will also be added if not found.
The use of the LOCAL plugin of ulogd is highly recommended.

NOTE: A ulog table must already exist before it can be set up for use with
webfwlog.  See the documentation of ulogd for more information on setting
up a ulog table.

If you do not want to use webfwlog with ulog then answer no to this question.
\n"

$ECHO "Do you want to set up webfwlog for use with ulog data? [Y/n]: "
USE_ULOG="`get_yes_or_no`"

if test $USE_ULOG -eq 1
then
	$ECHO "
Enter the database you want to use for your ulog table. It is recomended to put
the ulog table in a separate database such as 'ulogd'.  Setup can move existing
ulog data in another database to a new database entered here if the new
database does not already contain a ulog table.  The database will be created
if it does not exist.

If the new database does not exist or a ulog table cannot be found in this database
you will be prompted for a database containing a ulog table, which must already
exist before you can set up webfwlog for use with ulog data.  See the
documentation of ulogd for more information on setting up a ulog table.

Before moving your ulog data you should read the README in this directory for
additional considerations.
\n"
	$ECHO "Enter database to use for the ulog tables (<cr>=$ULOG_DB): "
	ULOG_DB="`get_param "$ULOG_DB"`"

	if test -n "`$PROG --batch -e "SELECT 1;" $ULOG_DB 2> /dev/null`"
	then 
		$ECHO "\n"
		$ECHO "Using existing database $ULOG_DB for ulog\n"
		HAVE_ULOG_DB=1
	fi
fi

if test $USE_ULOG -eq 1
then
	until test $HAVE_ULOG_TABLE -eq 1 -o $HAVE_ULOG_OLD_TABLE -eq 1
	do
		$ECHO "\n"
		$ECHO "Enter name of ulog table (<cr>=$ULOG_TABLE): "
		ULOG_TABLE="`get_param "$ULOG_TABLE"`"

		ULOG="$ULOG_DB.$ULOG_TABLE"

		if ($PROG -e "SELECT count(*) FROM $ULOG;") > /dev/null 2>&1
		then 
			$ECHO "\nUsing ulog table $ULOG\n"
			HAVE_ULOG_TABLE=1
		else
			$ECHO "
Creating new ulog table $ULOG from existing ulog data.

You will be prompted for the database and table name for your current data, which
must already exist.  You will also be prompted for the $DB user name
used by the ulogd daemon, which also must already exist.

NOTE: After the table has been copied, you will need to modify your ulogd.conf
file to point to the new database and restart the ulogd daemon.
"
			until test $HAVE_ULOG_OLD_TABLE -eq 1
			do
				$ECHO "\n"
				$ECHO "Enter database for existing ulog table (<cr>=$ULOG_OLD_DB): "
				ULOG_OLD_DB="`get_param "$ULOG_OLD_DB"`"
				$ECHO "Enter name of existing ulog table (<cr>=$ULOG_OLD_TABLE): "
				ULOG_OLD_TABLE="`get_param "$ULOG_OLD_TABLE"`"

				OLD_ULOG="$ULOG_OLD_DB.$ULOG_OLD_TABLE"

				if ($PROG -e "SELECT count(*) FROM $OLD_ULOG;") > /dev/null 2>&1
				then 
					HAVE_ULOG_OLD_TABLE=1
				fi
			done
			$ECHO "\nUsing existing ulog table $OLD_ULOG\n\n"
			$ECHO "Enter $DB user name for the ulogd daemon (<cr>=$ULOGD_USER): "
			ULOGD_USER="`get_param "$ULOGD_USER"`"
			$ECHO "\nUsing $DB user $ULOGD_USER for the ulogd daemon\n"
		fi
	done

	#   test existing ulog table for local _hostname and local_time and update if needed.
	if test $HAVE_ULOG_OLD_TABLE -eq 1 -a $HAVE_ULOG_TABLE -eq 0
	then T="$OLD_ULOG"
	else T="$ULOG"
	fi

	$ECHO "\n"
	$ECHO "Checking table $T ... "

	if ($PROG -e "SELECT local_hostname, local_time FROM $T;") > /dev/null 2>&1
	then :
	else
		SCRIPT="$SCRIPT`awk "{ if (NR != 1) print | \\\"sed 's/\\\$ULOG_TABLE/$T/'\\\" }" scripts/ulog_local`\n"
	fi

	if test $HAVE_ULOG_OLD_TABLE -eq 1 -a $HAVE_ULOG_TABLE -eq 0
	then
		if test $HAVE_ULOG_DB -eq 0
		then
			SCRIPT="${SCRIPT}--\n-- Create new database $ULOG_DB\n--\nCREATE DATABASE $ULOG_DB;\n"
		fi
		SCRIPT="$SCRIPT`awk "{ if (NR != 1) print | \\\"sed 's/\\\$ULOG_TABLE/$ULOG_TABLE/g' | sed 's/\\\$ULOG_DB/$ULOG_DB/g' | sed 's/\\\$ULOG_OLD_TABLE/$ULOG_OLD_TABLE/g' | sed 's/\\\$USER/$ULOGD_USER/g' | sed 's/\\\$ULOG_OLD_DB/$ULOG_OLD_DB/g'\\\" }" scripts/ulog_copy`\n"
	fi

	#   reset indexes and column defaults on existing ulog data
	DEF=`$PROG -e "SHOW CREATE TABLE $ULOG" 2> /dev/null` > /dev/null 2>&1
	SCRIPT_ADD="`add_ulog_indexes "$DEF"`"
	if test -n "$SCRIPT_ADD"
	then
		SCRIPT="${SCRIPT}--\n-- Add indexes to ulog table\n--\n$SCRIPT_ADD\n"
	fi

	SCRIPT="${SCRIPT}--\n-- Run ANALYZE TABLE on Ulog table\n--\nANALYZE TABLE $ULOG;\n"

	$ECHO "Done!\n"

fi

# set/reset rights

$ECHO "
Answer yes to the following question set or reset the rights for the $DB
user you want to use with webfwlog.  You must do this the first time you set up
webfwlog, and it is recommended to do this if you are upgrading from a previous
version of webfwlog.  You can also use this to change the password for the
$DB user for webfwlog.  Be sure to update your webfwlog.conf file
after setting or changing the webfwlog user's password.
\n"

$ECHO "Do you want to set/reset the $DB user rights? [Y/n]: "
GRANTS="`get_yes_or_no`"

if test $GRANTS -eq 1
then
	# get webfwlog user credentials
	$ECHO "
Enter username and password you want to user for webfwlog.  Note that
\`webfwlog\` is a different user than \`webfwlog@localhost\`, which is a different
user than \`webfwlog@127.0.0.1\` as far as $DB is concerned.
"
	$ECHO "\n"
	$ECHO "Enter $DB user for webfwlog (<cr>=$USER): "
	USER="`get_param "$USER"`"

	$ECHO "Enter password for $DB user for webfwlog (<cr>=$PASS): "
	stty -echo
	PASS="`get_param "$PASS"`"
	stty echo
	PASS="`$PROG --batch -N -e "SELECT password('$PASS')"`"

	SCRIPT="${SCRIPT}--\n-- Grant usage to $DB user\n--\n"
	SCRIPT="${SCRIPT}GRANT USAGE ON *.* TO $USER IDENTIFIED BY PASSWORD '$PASS';\n"
	
	SCRIPT="$SCRIPT`awk "{ if (NR != 1) print | \\\"sed 's/\\\$USER/$USER/' | sed 's/\\\$WFWL_DB/$WFWL_DB/'\\\" }" scripts/wfwl_grants`\n"
	if test $MAJOR -ge 4
	then
		SCRIPT="$SCRIPT`awk "{ if (NR > 4) print | \\\"sed 's/\\\$USER/$USER/' | sed 's/\\\$DB/$WFWL_DB/'\\\" }" scripts/temp_grants`\n"
	fi
	
	if test $USE_ULOG -eq 1
	then
		SCRIPT="$SCRIPT`awk "{ if (NR != 1) print | \\\"sed 's/\\\$USER/$USER/' | sed 's/\\\$ULOG_DB/$ULOG_DB/'\\\" }" scripts/ulogd_grants`\n"
		if test $MAJOR -ge 4
		then
			SCRIPT="$SCRIPT`awk "{ if (NR > 4) print | \\\"sed 's/\\\$USER/$USER/' | sed 's/\\\$DB/$ULOG_DB/'\\\" }" scripts/temp_grants`\n"
		fi
	fi
	
	$ECHO "\n"
	$ECHO "Using $DB user \`$USER\`\n"
fi

# query regarding import of definitions

create_def () {
	cat $1 | while read N 
	do
		LINE=`$ECHO "$N" |sed 's/^ *#.*//'`
		if test -z "$LINE"; then return; fi
		KEY=`$ECHO "$LINE\n" | cut -d= -f1`
		VALUE=`$ECHO "$LINE\n" | cut -d= -f2`
		LEN=`$ECHO "$VALUE"|wc -c`
		LEN=`expr $LEN - 2`
		$ECHO "s:`$ECHO "$KEY"|wc -c | sed -e 's/^ *//'`:\"$KEY\";s:$LEN:$VALUE;"
	done;
}

get_def () {
	$ECHO "`grep ^description $1|cut -d= -f2|sed 's/\"//g'`\n"
	$ECHO "a:`awk "{ print }" $1 |grep -v "^ *#"|grep -v "^$"|wc -l|cut -d" " -f1`:{"
	$ECHO "`create_def $1`"}
}

$ECHO "
You can populate the webfwlog reports table with report definitions in the
examples directory by answering yes below.  Existing definitions will not
be overwritten.
\n"
$ECHO "Do you want to use populate the reports table with example reports? [Y/n]: "
ADD_EXAMPLES="`get_yes_or_no`"

if test $ADD_EXAMPLES -eq 1
then
	if test -d $EXAMPLES_DIR
	then
		$ECHO "\nCreating INSERT statements for example reports ... "
		SCRIPT="${SCRIPT}--\n-- Insert example report definitions\n--\n"
		for x in `ls $EXAMPLES_DIR`
		do
			if test -f $EXAMPLES_DIR/$x
			then
				DEF=`get_def $EXAMPLES_DIR/$x`
				SCRIPT="${SCRIPT}INSERT IGNORE INTO $WFWL_REPORTS (code, description, definition, last_saved, last_accessed)\nVALUES ('$x', '`$ECHO "$DEF"|head -n1`', '`$ECHO "$DEF"|awk "{ if (NR != 1) print }"`', unix_timestamp(), unix_timestamp());\n"
			fi
		done
		$ECHO "done!\n"
	else
		$ECHO "Could not find examples directory $EXAMPLES_DIR\n"
	fi
fi

$ECHO "\n"
$ECHO "$DB server location:     $SERVER\n"
if test "$SERVER" != "localhost"
then
	$ECHO "$DB server port:         $PORT\n"
fi
$ECHO "$DB server version:      $VERSION\n"
$ECHO "$DB admin user:          $ADMIN_USER\n"
#$ECHO "$DB admin user password: $ADMIN_PASS\n"
if test $GRANTS -eq 1
then
	$ECHO "$DB webfwlog user:       $USER\n"
	$ECHO "$DB user password hash:  $PASS\n"
fi
$ECHO "Webfwlog database:         $WFWL_DB\n"
if test $USE_ULOG -eq 1
then
	if test $HAVE_ULOG_OLD_TABLE -eq 1 -a $HAVE_ULOG_TABLE -eq 0
	then
		$ECHO "$DB Ulogd user:          $ULOGD_USER\n"
	fi
	$ECHO "Ulog database:             $ULOG_DB\n"
	$ECHO "Ulog table:                $ULOG_TABLE\n"
fi

OUTPUT="V"
until test "$OUTPUT" = "q" -o "$OUTPUT" = "Q"
do
	$ECHO "
======================================================================
The setup script for $DB is now complete.  You can do the following:

- View the script
- Save the script to a file
- Run the script
- Quit

You will be returned to this menu when done
\n"
	$ECHO "What do you want to do? [V/s/r/q]: "
	OUTPUT="`get_param "$OUTPUT"`"

	if test "$OUTPUT" = "v" -o "$OUTPUT" = "V"
	then
		$ECHO "\n" 
		$ECHO "$SCRIPT\n"
		$ECHO "Press CR to continue ..."
		read N
	fi
	
	if test "$OUTPUT" = "s" -o "$OUTPUT" = "S"
	then
		$ECHO "Enter filename (<cr>=$OUTFILE): "
		OUTFILE="`get_param "$OUTFILE"`"
		if test -d `dirname "$OUTFILE"` -a -w `dirname "$OUTFILE"` && touch "$OUTFILE"
		then
			$ECHO "$SCRIPT" > "$OUTFILE"
			$ECHO "\nWrote $DB script $OUTFILE\n"
			$ECHO "Press CR to continue ..."
			read N
		else
			$ECHO "\nCannot write to $OUTFILE\n"
			$ECHO "Press CR to continue ..."
			read N
		fi
	fi
	
	if test "$OUTPUT" = "r" -o "$OUTPUT" = "R"
	then
		if ($ECHO "$SCRIPT\n" | $PROG -v )
		then
			$ECHO "\n$DB script complete successfully!\n"
			$ECHO "Press CR to continue ..."
			read N
		else
			$ECHO "\n$DB script failed!\n"
			$ECHO "Press CR to continue ..."
			read N
		fi
	fi
done

exit 0

# vim:set ts=4:
