#!/usr/local/Hughes/bin/lite


/*
** This script sets up the required databases and tables for the W3-Auth
** access control system.
*/

echo("\n");
echo("This script sets up the required databases and tables for the W3-Auth\n");
echo("access control system.\n\n");

/*
** Create the empty databases
*/

echo ("Creating the empty database ...");
if (system("/usr/local/Hughes/bin/msqladmin create w3-msql") != 0)
{
	fatal("\nError :  creating databases");
	exit(1);
}


/*
** Connect to the server
*/

echo("\nW3-mSQL Setup\n-------------\n\n");

echo("Connecting to the mSQL server ... ");
$sock = msqlConnect();
if ($sock < 0)
{
	fatal("Error : $ERRMSG\n");
}
if (msqlSelectDB($sock,"w3-msql") < 0)
{
	fatal("Error : $ERRMSG\n");
}
echo ("connected.\n");


/*
** Create the auth tables
*/

echo("Creating the namespace table ... ");
if (msqlQuery($sock, " create table namespaces (
			namespace	char(50),
			descr		char(50))" ) < 0)
{
	fatal("Error : $ERRMSG\n");
}
echo("done.\n");

		

echo("Creating the areas table ... ");
if (msqlQuery($sock, "create table areas (
			name		char(50),
			url		char(255),
			namespace	char(25))" ) < 0)
{
	fatal("Error : $ERRMSG\n");
}
echo("done.\n");



echo("Creating the access control table ... ");
if (msqlQuery($sock, "create table area_acl (
			name		char(50),
			offset		int,
			group		char(20),
			acl		char(50),
			namespace	char(25))" ) < 0)
{
	fatal("Error : $ERRMSG\n");
}
echo("done.\n");


echo("Creating the access control table index ... ");
if (msqlQuery($sock, "create index idx1 on area_acl (name)") < 0)
{
	fatal("Error : $ERRMSG\n");
}
echo("done.\n");



echo("Creating the users table ... ");
if (msqlQuery($sock, "create table users (
			namespace	char(25),
			uname		char(20),
			passwd		char(20),
			full_name 	char(50))") < 0)
{
	fatal("Error : $ERRMSG\n");
}
echo("done.\n");


echo("Creating the users table index ... ");
if (msqlQuery($sock, "create unique index idx1 on users (
			namespace,
			uname)") < 0)
{
	fatal("Error : $ERRMSG\n");
}
echo("done.\n");


echo("Creating the namespace perms table ... ");
if (msqlQuery($sock, "create table perms (
			uname		char(25),
			namespace	char(25),
			level		int)") < 0)
{
	fatal("Error : $ERRMSG\n");
}
echo("done.\n");



echo("Creating the namespace perms table index ... ");
if (msqlQuery($sock, "create index idx1 on perms ( uname )") < 0)
{
	fatal("Error : $ERRMSG\n");
}
echo("done.\n");




echo("Creating the group_members table ... ");
if (msqlQuery($sock, "create table group_members (
			namespace	char(25),
			group		char(20),
			uname		char(20))") < 0)
{
	fatal("Error : $ERRMSG\n");
}
echo("done.\n");



echo("Creating the group_members table index ... ");
if (msqlQuery($sock, "create index idx1 on group_members (
			namespace,
			group)") < 0)
{
	fatal("Error : $ERRMSG\n");
}
echo("done.\n");




echo("Creating the groups table ... ");
if (msqlQuery($sock, "create table groups (
			namespace	char(25),
			group		char(20),
			descr		char(50))") < 0)
{
	fatal("Error : $ERRMSG\n");
}
echo("done.\n");


echo("Creating the groups table index ... ");
if (msqlQuery($sock, "create index idx1 on groups (
			namespace,
			group)") < 0)
{
	fatal("Error : $ERRMSG\n");
}
echo("done.\n");





echo("Creating the group_admin table ... ");
if (msqlQuery($sock, "create table group_admin (
			namespace	char(25),
			group		char(20),
			uname		char(20))") < 0)
{
	fatal("Error : $ERRMSG\n");
}
echo("done.\n");



echo("Creating the group_admin table index ... ");
if (msqlQuery($sock, "create index idx1 on group_admin (
			namespace,
			group)") < 0)
{
	fatal("Error : $ERRMSG\n");
}
echo("done.\n");



/*
** Populate the data with some bootstrap info
*/


echo("Creating the W3-auth administrative namespace ... ");
if (msqlQuery($sock, "insert into namespaces values 
		('SuperUser', 'W3-mSQL Administrators')") < 0)
{
	fatal("Error : $ERRMSG\n");
}
echo("done.\n");


/*
** Add a "super user" to the w3-msql auth tables
*/

echo ("\nAdding a \"super user\" to the w3-msql auth tables\n\n");
echo ("Username  : ");
$user = readln($stdin);
$user = chop($user);
echo ("Full name : ");
$fullname = readln($stdin);
$fullname = chop($fullname);
echo ("Password  : ");
$passwd = readln($stdin);
$passwd = chop($passwd);

$passwd = crypt($passwd,"AA");


if (msqlQuery($sock,"insert into users values 
	('SuperUser', '$user','$passwd','$fullname')") < 0)
{
	fatal("Error : $ERRMSG\n");
}

if (msqlQuery($sock,"insert into perms values 
	('$user','SuperUser',255)") < 0)
{
	fatal("Error : $ERRMSG\n");
}

msqlClose($sock);

echo("Done.\n\nSetup of W3-mSQL is complete\n\n");
