#!/bin/csh -f
## this script creates the tables needed by ippMonitor for user administration

if ($#argv < 4) goto usage;

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

  mysql -h $dbserver -u root -p <<EOF
  create database ippadmin;
  use ippadmin;
  create table users (username char(20) not null, password varchar(255));
  create table cookies (username char(20) not null, cookie varchar(255));
  create table projects (projname char(20) not null);
  grant all on *.* to $dbuser@localhost identified by '$dbpass';
EOF
    exit 0;
endif

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

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

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

  mysql -h $dbserver -u $dbuser -p ippadmin <<EOF
  insert into users (username, password) values ('$user', '$pass');
EOF
    exit 0;
endif

if ("$1" == "project") then
  if ($#argv != 4) goto usage;
  set dbserver = $2
  set dbuser = $3
  set dbname = $4

  mysql -h $dbserver -u $dbuser -p ippadmin <<EOF
   insert into projects (projname) values ('$dbname');
   create database $dbname;
EOF
    exit 0;
endif

usage:
  echo "USAGE: dbadmin (options)"
  echo ""
  echo "  dbadmin init (dbserver) (dbuser) (dbpassword)"
  echo "      creates admin tables, basic db user -- requires root password"
  echo ""
  echo "  dbadmin client (dbserver) (dbuser) (dbpassword) (client)"
  echo "      add client name or regex"
  echo ""
  echo "  dbadmin user (dbserver) (dbuser) (username) (password)"
  echo "      create a new www user and password"
  echo ""
  echo "  dbadmin project (dbserver) (dbuser) (dbname)"
  echo "      create a new ipp project (database)"
  echo ""
  exit 2
