#!/usr/bin/perl -w
use strict;
use lib qw(../);
use Thraxil;

# !!!!!!! this script must be run from outside the scripts directory
# or it WILL NOT WORK !!!!!!!

my $db_username = shift;
# make sure there isn't already a database there named thraxil

print "dropping old db if it exists\n";
`dropdb thraxil -U $db_username`;
`rm -rf users/*`;
# make a new database

print "creating the database\n";
`createdb thraxil -U $db_username`;

# adding tables to database

print "adding tables to database\n";
`psql thraxil < sql/thraxil.sql -U $db_username`;

# create a default user
print "first user username: ";
my $username = <>;
print "first user password: ";
my $password = <>;
print "email: ";
my $email = <>;
print "adding default user\n";

chomp $username;
chomp $password;
chomp $email;

my $thraxil = Thraxil->new("","");
$thraxil->add_user($username,$password,$password,$username,$email);
$thraxil->user($username)->bootstrap_authorize();

# don't forget to set some permissions
#`/bin/chmod a+w -R users/`;
#`/bin/chmod a+w -R thraxil_uploads/`;
#`/bin/chmod a+w -R user_cache/`;

exit 0;


