#!/bin/csh

# Variables used in this script:

set dms=~tld/Sites/dms
set sql="$dms/sql"
set users="$dms/users"
set psqlbin=/usr/local/bin

# The list of registered users:

set usrs=(\
         tld \
         lpk \
         )

# Drop the old user databases.

foreach usr ( $usrs ) 
   $psqlbin/dropdb "$usr" --user=postgres
   end

# Drop the old template database.

$psqlbin/dropdb "dms" --user=postgres

# Create a new template database.

$psqlbin/createdb "dms" --user=postgres
$psqlbin/createlang "plpgsql" "dms" --user=postgres
$psqlbin/psql "dms" --user=postgres < $sql/dms.sql

# Add usernames and passwords for registered users.

$psqlbin/psql "dms" --user=postgres < $sql/pswds.sql

# Give the sql scripts a chance to complete.

sleep 2

# Create new user databases using this template.

foreach usr ( $usrs ) 
   $psqlbin/createdb -T "dms" "$usr" --user=postgres
   end

# Initialize the user directories.

foreach usr ( $usrs ) 
   if ( ! -e $users/$usr/ ) /bin/mkdir $users/$usr/
   if ( -r $users/$usr/entered-words.txt ) /bin/rm -f $users/$usr/entered-words.txt
   touch $users/$usr/entered-words.txt
   end

# Following this use 'chgrp' and 'chmod' to fix permissions (see ../README).