#!/bin/csh -f

## this script creates a database for testing dvopsps

if ($#argv < 3) goto usage;

if ("$1" == "user") then
  if ($#argv != 4) goto usage;
  set dbhost = $2
  set dbuser = $3
  set dbpass = $4

  mysql -h $dbhost -u root -p -e "grant all on *.* to $dbuser@"$dbhost" identified by '$dbpass'"
  exit 0;
endif

if ("$1" == "user.sudo") then
  if ($#argv != 4) goto usage;
  set dbhost = $2
  set dbuser = $3
  set dbpass = $4

  sudo mysql -h $dbhost -u root -p -e "grant all on *.* to $dbuser@"$dbhost" identified by '$dbpass'"
  exit 0;
endif

if ("$1" == "create") then
  if ($#argv < 4) goto usage;
  set dbhost = $2
  set dbname = $3
  set dbuser = $4
  if ($#argv == 5) then
    set dbpass = "-p$5"
  else 
    set dbpass = "-p"
  endif

  # create a database with completely minimal tables needed to test
  mysql -h $dbhost -u $dbuser $dbpass -e "CREATE DATABASE $dbname"
  if ($status) then
    echo "ERROR: failed to create mysql database $dbname ($0)"
    echo " NOTE: before running this test, you must have a mysql user:"
    echo "       mkgpc1.sh user localhost dvo dvo (requires root password)"
    exit 1
  endif
  mysql -h $dbhost -u $dbuser $dbpass $dbname < gpc1schema.sql
  exit 0;
endif

if ("$1" == "delete") then
  if ($#argv < 4) goto usage;
  set dbhost = $2
  set dbname = $3
  set dbuser = $4
  if ($#argv == 5) then
    set dbpass = "-p$5"
  else 
    set dbpass = "-p"
  endif

  (mysql -B -h $dbhost -u $dbuser $dbpass -e "show databases" | grep ^$dbname\$) >& /dev/null
  if ($status) then
    echo "INFO: database $dbname does not yet exist ($0)"
    exit 0
  endif

  echo mysql -B -h $dbhost -u $dbuser $dbpass $dbname -e "show tables"
  mysql -h $dbhost -u $dbuser $dbpass $dbname -e "show tables"
  mysql -h $dbhost -u $dbuser $dbpass $dbname -e "describe ippToPspsFake" >& /dev/null
  if ($status) then
    echo "database does not contain ippToPspsFake, not deleting"
    exit 1;
  endif

  set ntable = `mysql -B -h $dbhost -u $dbuser $dbpass $dbname -e "show tables" | wc -l`
  echo ntable: $ntable
  if ($ntable != 28) then
    echo "expected 28 lines (27 tables), but got $ntable, not deleting"
    exit 2
  endif

  echo "database is valid, deleting"
  mysql -h $dbhost -u $dbuser $dbpass -e "drop database $dbname"
  exit 0;
endif

if ("$1" == "forcedelete") then
  if ($#argv < 4) goto usage;
  set dbhost = $2
  set dbname = $3
  set dbuser = $4
  if ($#argv == 5) then
    set dbpass = "-p$5"
  else 
    set dbpass = "-p"
  endif

  (mysql -B -h $dbhost -u $dbuser $dbpass -e "show databases" | grep ^$dbname\$) >& /dev/null
  if ($status) then
    echo "database $dbname does not yet exist"
    exit 0
  endif

  mysql -h $dbhost -u $dbuser $dbpass $dbname -e "describe ippToPspsFake" >& /dev/null
  if ($status) then
    echo "database does not contain ippToPspsFake, not deleting"
    exit 1;
  endif

  echo mysql -B -h $dbhost -u $dbuser $dbpass $dbname -e "show tables"
  set ntable = `mysql -B -h $dbhost -u $dbuser $dbpass $dbname -e "show tables" | wc -l`
  echo ntable: $ntable
  if ($ntable != 28) then
    echo "warning: expected 28 lines (27 tables), but got $ntable; still deleting"
  else
    echo "database is valid, deleting"
  endif

  mysql -h $dbhost -u $dbuser $dbpass -e "drop database $dbname"
  exit 0;
endif

usage:
  echo "USAGE: mkgpc1.sh (options)"
  echo ""
  echo "  mkgpc1.sh create (dbhost) (dbname) (dbuser) [dbpass]"
  echo "      create a new test db"
  echo ""
  echo "  mkgpc1.sh delete (dbhost) (dbname) (dbuser) [dbpass]"
  echo "      delete an existing test db (enter pass 3x)"
  echo ""
  echo "  mkgpc1.sh forcedelete (dbhost) (dbname) (dbuser) [dbpass]"
  echo "      delete an existing test db (enter pass 3x), even if the number of tables is wrong"
  echo ""
  echo "  mkgpc1.sh user (dbhost) (dbuser) (password)"
  echo "      create a new user and password for the test db"
  echo ""
  echo "  mkgpc1.sh user.sudo (dbhost) (dbuser) (password)"
  echo "      create a new user and password for the test db, using sudo mysql"
  echo "      (this option is needed if your mysql installation uses the auth_socket plugin"
  echo ""
  exit 2
