#!/bin/sh
#

# gg78@star.le.ac.uk October 2007
#
# This script takes a .csv file as an argument which contains a list of people who need an AstroGrid account
# with a particular community (the url, admin name and password need to be set below) and a TWiki account.
#
# The csv file should be of the form Firstname,Lastname,email@ddress with the first letters of the names capitalised
# No checking of the file is done - so make sure you have it properly formatted first.
# 
# The script tests for the existence of both Community and TWiki accounts and if they don't already exist will create 
# the community account and a text file to be copy&pasted into the TWiki Bulk Registration page.
#
# An email will be sent to the new users informing them of their account id and password. BEWARE - this is sent from
# the account at the machine the script is run on, spoofing isn't possible (or is tricky) with mailx, maybe it should 
# be redone in perl. There is an 'astrogrid_signup' account on uluru.star.le.ac.uk for this: mail from the account 
# comes from:
# astrogrid_signup@star.le.ac.uk
#
# Some degree of logging dumps what the script has done to a file, but overall this is really just a quick and dirty 
# way to register large amounts of people for AstroGrid workshops. 

COMMUNITY_USER=*******
COMMUNITY_PASS=*******
COMMUNITY_URL=corazon.star.le.ac.uk:8080/astrogrid-community-le/admin/AccountAdmin.jsp
TWIKIUSERSPAGE=http://wiki.astrogrid.org/bin/view/Main/TWikiUsers
NEWTWIKIUSERS=twiki.txt
LOGFILE=added.log

# echo $1

for i in `cat $1`

  do FIRSTNAME=`echo $i | awk -F "," '{ print $1 }' `
     SURNAME=`echo $i | awk -F "," '{ print $2 }' `
     EMAIL=`echo $i | awk -F "," '{ print $3 }' `

     LOGINNAME=$FIRSTNAME$SURNAME
     DISPLAYNAME=$FIRSTNAME" "$SURNAME
     PASSWORD=$LOGINNAME
     DESCRIPTION=""

     # primitive logging
     echo -e "\n"  >> $LOGFILE
     date  >> $LOGFILE

#     echo "firstname:   "$FIRSTNAME
#     echo "surname:     "$SURNAME
#     echo "email:       "$EMAIL
#     echo "login-name:  "$LOGINNAME
#     echo "displayname: "$DISPLAYNAME
#     echo "password:    "$PASSWORD
#     echo "description: "$DESCRIPTION


####
#Add user account to community (after testing it doesn't already exist)


if [ `curl --url http://${COMMUNITY_URL} \
     --user ${COMMUNITY_USER}:${COMMUNITY_PASS} \
            | grep -c "name=\"userLoginName\" value=\"ivo://uk\.ac.le.star/${LOGINNAME}"` = 0 ]
then
    echo "${LOGINNAME} to be added to community" >> $LOGFILE
        if [ `curl --url http://${COMMUNITY_URL} \
             --user ${COMMUNITY_USER}:${COMMUNITY_PASS} \
             --data "userLoginName=${LOGINNAME}" \
             --data "userCommonName=${DISPLAYNAME}" \
             --data "userPassword=${PASSWORD}" \
             --data "description=${DESCRIPTION}" \
             --data "email=${EMAIL}" \
             --data "homespace=" \
             --data "AddAccount=true" \
             --data "AddAccountSubmit=Add" \
             | grep -c "Account was added for id = ${LOGINNAME} And password set"` = 1 ]
        then
           echo "${LOGINNAME} succesfully added to community" >> $LOGFILE

            # email login info to ne wuser
            echo -e "Dear ${FIRSTNAME},\n\
\n\
Your AstroGrid account has been created:\n\
\n\
username: ${LOGINNAME}\n\
password: ${LOGINNAME}\n\
community: uk.ac.le.star\n\
\n\
You can access the AstroGrid workbench from the link at astrogrid.org/launch\n\
\n\
Regards\n\
\n\
The AstroGrid Team\n\" \
|    mail ${EMAIL} -s "Astrogrid account creation"

        else
           echo "${LOGINNAME} not added to community - error"  >> $LOGFILE
        fi

else

    echo "${LOGINNAME} already member of community" >> $LOGFILE
fi 

# Accounts have now been added where needed


# Now output TWiki list
if [ `curl --url ${TWIKIUSERSPAGE} \
      | grep -c "<a href=\"/bin/view/Main/${LOGINNAME}\" class=\"twikiLink\">"` == 0 ]
then
   echo "${LOGINNAME} to be added to wiki" >> $LOGFILE
   echo "| " ${FIRSTNAME} " | " ${SURNAME} " | " ${EMAIL} " | " ${LOGINNAME} " |" >> ${NEWTWIKIUSERS}
else
   echo "${LOGINNAME} already in wiki" >> $LOGFILE
fi
# Twiki list done


# end of csv file
done
