#!/bin/bash
# $Id$

# prologue_local arguments
# Script is run under uid of oar who is sudo
# argv[1] is the jobid
# argv[2] is the user's name
# argv[3] is the job script name if exists

. ~oar/lock_user.sh
USERFILE="/tmp/oar_user_list.oar.$2"
USERFILETMP="$USERFILE.tmp"

LOCKRESOURCE="oarlock"
#LOCKRESOURCE=$2

lock_file $1 $LOCKRESOURCE
if (( $? == 1 ))
then
    echo "/!\\ Lock command timed out, it overrides previous command"
fi

echo $1 >> $USERFILE

########################################
# PROLOGUE

ACCESS_FILE=/etc/security/access_cluster.conf

#ALLOWEDUSERS=`cat $ACCESS_FILE | grep -v "\#" | grep "^-" | sed s/\-:ALL\ EXCEPT\ // |  sed s/:ALL/\ $2/ | sed s/^\ *// | sed s/\ *$//`

#echo "+:ALL:LOCAL" > $ACCESS_FILE
#echo "-:ALL EXCEPT $ALLOWEDUSERS:ALL" >> $ACCESS_FILE

ACCESS_FILE_NEW=/tmp/access_cluster.conf.new 

echo "# File generated by OAR."                    > "${ACCESS_FILE_NEW}" 
echo "#  You can remove it, but don't modify it!" >> "${ACCESS_FILE_NEW}" 
echo "+:${2}:ALL"                                 >> "${ACCESS_FILE_NEW}"  
grep -v "^[-#]" <  "${ACCESS_FILE}"               >> "${ACCESS_FILE_NEW}" 
echo "-:ALL:ALL"                                  >> "${ACCESS_FILE_NEW}" 
sudo mv -f "${ACCESS_FILE_NEW}" "${ACCESS_FILE}" 

########################################


unlock_file $1 $LOCKRESOURCE
if (( $? == 1 ))
then
    echo "/!\\unlock command says that it is not our lock"
fi
