#!/bin/sh

#
# Script creates necessary MySQL databases, tables and users for a
# standard RTG installation.
#

if [ $# -ne 1 ]; then
   echo 1>&2 Usage: $0 mysql_root_password 
   exit 127
fi
MYSQLBIN="/usr/local/bin"
ROOTPASS=$1
HOST="localhost"
RTGPASS="rtgdefault"
DATABASE="rtg"
USER="snmp"
MYSQL_FILE=$(mktemp -q /tmp/mysql.XXXXXX)
RTG_FILE=$(mktemp -q /tmp/rtg.XXXXXX)

echo ""
echo "$0 setting up MySQL database for RTG."
echo "Using MySQL binaries in $MYSQLBIN."
echo ""

# Create the necessary SQL in two /tmp files
cat <<EOT >${MYSQL_FILE}
CREATE USER '$USER'@'$HOST' IDENTIFIED BY '$RTG_PASS';
GRANT ALL ON '$DATABASE'.* TO '$USER'@'$HOST';
EOT

cat <<EOT >${RTG_FILE}
#
# Table structure for table 'router'
#

CREATE TABLE `router` (
  `rid` int(11) unsigned NOT NULL auto_increment,
  `name` char(120) NOT NULL default '',
  `pop` char(10) NOT NULL default '',
  `popid` tinyint(3) unsigned NOT NULL default '0',
  PRIMARY KEY (`rid`)
);

#
# Table structure for table 'interface'
#

CREATE TABLE `interface` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `name` char(255) NOT NULL default '',
  `rid` int(11) NOT NULL default '0',
  `speed` bigint(11) default NULL,
  `description` char(255) default NULL,
  `status` enum('active','inactive') default 'active',
  PRIMARY KEY (`id`)
);

#
# Table structure for table 'ifInOctets'
#

CREATE TABLE `ifInOctets` (
  `id` int(11) NOT NULL default '0',
  `dtime` datetime NOT NULL,
  `counter` bigint(20) NOT NULL,
  PRIMARY KEY (`id`,`dtime`,`counter`),
  KEY `ifInOctets_idx` (`dtime`),
  KEY `id_dtime_idx` (`id`,`dtime`)
);

#
# Table structure for table 'ifOutOctets'
#

CREATE TABLE `ifOutOctets` (
  `id` int(11) NOT NULL default '0',
  `dtime` datetime NOT NULL,
  `counter` bigint(20) NOT NULL,
  PRIMARY KEY (`id`,`dtime`,`counter`),
  KEY `ifOutOctets_idx` (`dtime`),
  KEY `id_dtime_idx` (`id`,`dtime`)
);

#
# Table structure for table 'ifInUcastPkts'
#

CREATE TABLE `ifInUcastPkts` (
  `id` int(11) NOT NULL default '0',
  `dtime` datetime NOT NULL,
  `counter` bigint(20) NOT NULL,
  PRIMARY KEY (`id`,`dtime`,`counter`),
  KEY `ifInUcastPkts_idx` (`dtime`),
  KEY `id_dtime_idx` (`id`,`dtime`)
);

#
# Table structure for table 'ifOutUcastPkts'
#

CREATE TABLE `ifOutUcastPkts` (
  `id` int(11) NOT NULL default '0',
  `dtime` datetime NOT NULL,
  `counter` bigint(20) NOT NULL,
  PRIMARY KEY (`id`,`dtime`,`counter`),
  KEY `ifOutUcastPkts_idx` (`dtime`),
  KEY `id_dtime_idx` (`id`,`dtime`)
);

#
# Table structure for table 'ifInErrors'
#

CREATE TABLE `ifInErrors` (
  `id` int(11) NOT NULL default '0',
  `dtime` datetime NOT NULL,
  `counter` bigint(20) NOT NULL,
  PRIMARY KEY (`id`,`dtime`,`counter`),
  KEY `ifInErrors_idx` (`dtime`),
  KEY `id_dtime_idx` (`id`,`dtime`)
);
EOT

echo "Adding user \"$USER\" to MySQL database..."
cat ${MYSQL_FILE} | $MYSQLBIN/mysql -u root -p$ROOTPASS mysql
echo "Creating RTG database \"$DATABASE\"..."
$MYSQLBIN/mysqladmin -u root -p$ROOTPASS create $DATABASE
echo "Creating RTG tables..."
cat ${RTG_FILE} | $MYSQLBIN/mysql -u $USER -p$RTGPASS $DATABASE
echo "Done."
echo "Removing temporary files..."
rm ${MYSQL_FILE} ${RTG_FILE}

