#!/bin/bash
P=`dirname $0`
. ${P}/dutils.sh

# can be run for fresh installation or within a legacy Webobs migration
# can be run any time after linux dependencies installation
# current directory (pwd) MUST be the target webob's CONF/ directory

# webobs user passed as 1st argument, defaults to 'webobs'
# legacy input filename passed as 2nd argument, defaults to './OPERATEURS.conf'
# Format of 'OPERATEURS.conf'
#    id|fullname|6|login|email||0

if [ $# -eq 0 ]; then US='webobs'; IN='./OPERATEURS.conf';fi
if [ $# -ge 1 ]; then US=$1; IN='./OPERATEURS.conf';fi
if [ $# -eq 2 ]; then IN=$2;fi

if [[ ! -f $IN ]]
then
	echo "File $IN not found: no legacy data will be imported."
	L=false
else 
	L=true
	if ! $(confirm "confirm using data from $IN [y/n] ? ") ; then
		echo "OK, no legacy will be imported."
		L=false
	fi
fi

# create the new tables
echo "Creating users,groups,auth*,notifications tables ..."
sqlite3 WEBOBSUSERS.db <<EOF
BEGIN TRANSACTION;
drop table if exists users;
create table users (UID text NOT NULL, FULLNAME text NOT NULL, LOGIN text NOT NULL, EMAIL text, PRIMARY KEY(UID,LOGIN));
drop table if exists authprocs;
create table authprocs (UID text NOT NULL, RESOURCE text NOT NULL, AUTH int NOT NULL DEFAULT 1, unique(UID,RESOURCE,AUTH));
drop table if exists authviews;
create table authviews (UID text NOT NULL, RESOURCE text NOT NULL, AUTH int NOT NULL DEFAULT 1, unique(UID,RESOURCE,AUTH));
drop table if exists authforms;
create table authforms (UID text NOT NULL, RESOURCE text NOT NULL, AUTH int NOT NULL DEFAULT 1, unique(UID,RESOURCE,AUTH));
drop table if exists authwikis;
create table authwikis (UID text NOT NULL, RESOURCE text NOT NULL, AUTH int NOT NULL DEFAULT 1, unique(UID,RESOURCE,AUTH));
drop table if exists authmisc;
create table authmisc (UID text NOT NULL, RESOURCE text NOT NULL, AUTH int NOT NULL DEFAULT 1, unique(UID,RESOURCE,AUTH));
drop table if exists groups;
create table groups (GID text NOT NULL, UID text NOT NULL, unique(GID,UID));
drop table if exists notifications;
create table notifications (EVENT text NOT NULL, 
                            VALIDITY NOT NULL default 'Y', 
							MAILID text default '-', 
							MAILSUBJECT text default '-', 
							MAILATTACH text default '-', 
							ACTION text default '-' ) ;
COMMIT;
insert into notifications values('eventnode','Y','!','webobs node event','-','-');
EOF

# create a temporary table 'tmpusers' to import legacy user definitions
# and use it to import/create admin users (users that had userlevel 6) 
if $L ; then
echo "Populating users,auth* tables from ${IN} ..."
sqlite3 WEBOBSUSERS.db <<EOF
drop table if exists tmpusers;
create table tmpusers (UID text NOT NULL, FULLNAME text NOT NULL, USERLEVEL integer NOT NULL, LOGIN text NOT NULL, EMAIL text ,BIRTHDAY date, NOTIFY integer DEFAULT 0, PRIMARY KEY(UID,LOGIN));
.separator |
.import ${IN} tmpusers
delete from tmpusers where UID in ('?','!');
insert into users select UID, FULLNAME, LOGIN, EMAIL FROM tmpusers;
insert into authprocs select uid,'*',4 from tmpusers where userlevel>=5;
insert into authviews select uid,'*',4 from tmpusers where userlevel>=5;
insert into authforms select uid,'*',4 from tmpusers where userlevel>=5;
insert into authwikis select uid,'*',4 from tmpusers where userlevel>=5;
insert into authmisc  select uid,'*',4 from tmpusers where userlevel>=5;
drop table if exists tmpusers;
EOF
fi

# now populate tables with default values (on top of legacy data if any)
echo "Populating auth*,groups,notifications tables with defaults ..."
sqlite3 WEBOBSUSERS.db <<EOF
BEGIN TRANSACTION;
insert into users values ('?','- Guest -','guest','');
insert into users values ('!','WebObs Owner','${US}','${US}@localhost');
insert into authprocs select uid,'*',4 from users where uid='!';
insert into authviews select uid,'*',4 from users where uid='!';
insert into authforms select uid,'*',4 from users where uid='!';
insert into authwikis select uid,'*',4 from users where uid='!';
insert into authmisc  select uid,'*',4 from users where uid='!';

insert into "authmisc" values('?','HEBDO',1);
insert into "authmisc" values('?','HEBDOTout',1);
insert into "authmisc" values('?','HEBDOToutReseaux',1);
insert into "authmisc" values('?','HEBDOAstreinte',1);
insert into "authmisc" values('?','HEBDOMission',1);
insert into "authmisc" values('?','HEBDOAbsence',1);
insert into "authmisc" values('?','HEBDOTerrain',1);
insert into "authmisc" values('?','HEBDOReunion',1);
insert into "authmisc" values('?','HEBDOMedia',1);
insert into "authmisc" values('?','HEBDOVulgarisation',1);
insert into "authmisc" values('?','HEBDOEnseignement',1);
insert into "authmisc" values('?','HEBDOVisiteurs',1);
insert into "authmisc" values('?','HEBDOStage',1);
insert into "authmisc" values('?','HEBDODivers',1);
insert into "authmisc" values('?','HEBDOReseaux',1);

insert into groups select '+BASE',UID from users;
insert into authwikis values('+BASE','*',1);
insert into authmisc values('+BASE','*',1);

insert into notifications (EVENT,MAILID,MAILSUBJECT) values('scheduler.warning','!','scheduler warning');
insert into notifications (EVENT,MAILID,MAILATTACH) values('scheduler.alert','!','pp/ff.txt');

create trigger deluid after delete on users for each row
begin 
  delete from groups where uid = OLD.uid; 
  delete from authviews where uid = OLD.uid; 
  delete from authprocs where uid = OLD.uid; 
  delete from authforms where uid = OLD.uid; 
  delete from authwikis where uid = OLD.uid; 
  delete from authmisc  where uid = OLD.uid; 
  delete from notifications where uid = OLD.uid; 
end;

create trigger delgid after delete on groups for each row 
when (select count(*) from groups where gid=OLD.gid) == 0 
begin 
  delete from authviews where uid = OLD.gid;
  delete from authprocs where uid = OLD.gid; 
  delete from authforms where uid = OLD.gid; 
  delete from authwikis where uid = OLD.gid; 
  delete from authmisc  where uid = OLD.gid; 
  delete from notifications where uid = OLD.gid; 
end;

COMMIT;
EOF

echo "Done."
exit 0
