#!/bin/csh -f

set path = ($path /usr/local/techinfo/etc )
set PROVIDER_FILE = /var/ti/admin/pips.providers
set SOURCESFILE   = /var/ti/admin/pips.sources
set ROOT_DIR      = /mit/ti_data

echo  '\
    Creating a provider account.  Please anwser all\
    questions.  You will be asked for the source (group),\
    userid, and password of the provider.  All of\
    these are mandatory.  If you have problems or\
    suggestions contact Scott or Steve.\
\
    For source name and userid:\
    ONLY use the following characters:\
    letters, underscore, period, plus, minus, and digits\
    A-Z a-z  _ . + - and 0-9\
\
'
echo "So far, Sources are:"
awk -F: '{print $1}' < $PROVIDER_FILE | sort -u | awk '{ if (NR %4 == 1) printf(
"\n"); printf ("%-20s", $0); }'
echo "\
"

echo  -n "What is the Source name for the new provider? "
set source = $<
set source = `echo $source | tr " " "_"`

echo -n "What is the Username for ${source}? "
set userid = $<
set userid = `echo $userid | tr " " "_"`

grep -s "^${source}:${userid}:" $PROVIDER_FILE
if ($status == 0) then
        echo "$userid is already a provider for $source in\
$PROVIDER_FILE"
        exit
endif

if ("$source" == "" || "$userid" == "" ) then
        echo "Source and userid are mandatory."
        echo "Provider was not added.  Try again. "
        exit
endif

set cr = "`crypt`"
if ($status != 0) then
        echo "Password was not good"
        exit -1
endif

echo "${source}:${userid}:${cr}"  >> $PROVIDER_FILE
if ("${source}" == "All") goto THEEND

mkdir "$ROOT_DIR/$source"
chgrp pi "$ROOT_DIR/$source"
chmod ug=rwx,o=rx "$ROOT_DIR/$source"

set em = `grep "^${source}:" $SOURCESFILE | awk -F: '{print $5}'`
if ("$em" != "") then
        echo Mail addresses for $source are $em in $SOURCESFILE
else
        echo -n "\
No email addresses are listed for $source in\
$SOURCESFILE.\
Please provide email address(es) for $source.\
Separate multiple addresses with commas: "
        set em = "$<"
        if ("$em" != "") then
                echo "${source}::::${em}" >> $SOURCESFILE
                echo "Email addresses for $source are "
                grep "^${source}:" $SOURCESFILE | awk -F: '{print $5}'
        endif
endif

THEEND:
echo "Account for the provider '$userid' has been added."
exit

# TO REMOVE A PROVIDER (BUT NOT A SOURCE):
# * backup $PROVIDER_FILE (just in case!)
# * delete their username and password from $PROVIDER_FILE
#
# TO REMOVE A SOURCE:
# * backup $PROVIDER_FILE (just in case!)
# * delete users in the source group
# * delete all nodes that belong to this source
#   * shutdown server (ask it to write web first)
#   * backup web file
#   * update web file (remove nodes associated with this source)
#   * restart server
# * remove any pointers to the nodes that were removed
#   * update web file (remove nodes associated with this source)
#   * restart server
# * remove any pointers to the nodes that were removed
# * rm -r (or mv to some place else) $ROOT_DIR/$SOURCE
