#! /bin/bash
# ********************************************************************************************* 
# IRA Istituto di Radioastronomia                                                                      
#
# This code is under GNU General Public Licence (GPL).                                              
#                                                                                                     
# Who                                when            What                                             
# Andrea Orlati(aorlati@ira.inaf.it) 12/02/2011      Creation           
# Andrea Orlati(aorlati@ira.inaf.it) 11/01/2013      Adapted to environment of escs 0.3                        
#************************************************************************
#   NAME
#               addObserver
# 
#   SYNOPSIS
# 
#   DESCRIPTION
#   This script creates a user for the observing system. It could be executed only by the system manager on the user server machine.
#   The manager must have sudoers grants for bash commands useradd and usermod. For that reason it is recommended to create 
#   the sudoers grants for manager on user server machine only.
#   The group observers must exist
#       
#   FILES
#
#   ENVIRONMENT
#
#   RETURN VALUES
#
#   CAUTIONS
#
#   EXAMPLES
#
#   SEE ALSO
#
#   BUGS     
#


function printUsage {
   echo "Creates a new user/project for the ESCS observing system, it creates all the required directory structures"
   echo ""
   echo "Usage: `basename $0` -u|--user username]"
   echo ""
   echo "-u|--user allows to give the name of the observer"
}

CL_HELP=
#CL_TREE=
CL_USER=

LONGOPTS=help,user:
SHORTOPTS=hu:

#SERVERACCOUNT=manager@192.168.1.104

getopt -n `basename $0` -Q -u -a -l $LONGOPTS $SHORTOPTS "$@" || {
   printUsage
   exit
}

set -- `getopt -u -a -l $LONGOPTS $SHORTOPTS "$@"`

#
# Iterate over getopt's output and set CL_XXX variables accordingly
#
while : 
do
        case "$1" in
        --help)             CL_HELP=true ;; 
        -h)                 CL_HELP=true ;;
	--user)             CL_USER=$2 ; shift ;;
	-u)                 CL_USER=$2 ; shift ;;
        --) break ;;
        esac
        shift
done
shift

if [ "$CL_HELP" ] ; then
   printUsage
   exit
fi

if [ ! -n "$CL_USER" ]
then
   echo "User name is mandatory!"
   echo	
   printUsage
   exit
fi


echo "adding new user......"
sudo /usr/sbin/useradd -g observers -G observers,users -m -n -s /bin/bash $CL_USER 
sudo /usr/sbin/usermod -U $CL_USER
echo "done"
echo "please choose a password......"
sudo passwd $CL_USER
echo "directory structures......"
mkdir /archive/schedules/$CL_USER
setfacl -m u:$CL_USER:rwx /archive/schedules/$CL_USER
setfacl -m d:u:manager:rwx /archive/schedules/$CL_USER 
mkdir /archive/data/$CL_USER
setfacl -m u:$CL_USER:r-x /archive/data/$CL_USER
setfacl -m d:u:manager:rwx /archive/data/$CL_USER 
setfacl -m d:u:$CL_USER:r-x /archive/data/$CL_USER
mkdir /archive/extraData/$CL_USER
setfacl -m u:$CL_USER:r-x /archive/extraData/$CL_USER
setfacl -m d:u:manager:rwx /archive/extraData/$CL_USER 
setfacl -m d:u:$CL_USER:r-x /archive/extraData/$CL_USER
echo "done"
echo "update yellow pages......"
sudo /usr/lib/yp/ypinit -m
sudo /sbin/service ypserv restart
echo "project $CL_USER added as registered project"


#
# ___oOo___
