#!/bin/bash
# DATE: 	Dec 29, 2011
# AUTHOR: 	Kyle Marcus

# Sets up the necessary iRDOS files in user's home directories so
# that users that log into a hub environment that do not already 
# have a iRODS account can read from the public share in iRODS.

# Creates the .irods/ directory and .irods/.irodsEnv file in the 
# users home directory if it is not already there. If the .irods/
# directory already exists, it does not modify anything. The 
# script will also modify the .bash_profile file so that iRODS 
# can be mounted with fuse automatically.

# This script could be set up with a cron job so that new users
# added to a hub system can access the public share on iRODS.
# Run every 5 mins (/etc/cron.d/irods_public):
# */5 * * * * root /root/irods_public.sh

# NOTE: If there are any other users that you want to avoid, 
# besides the default ones in this script, add their user names
# to the avoidUsers array in the VARS section. Also, in order
# to change this script to work with any hub, you need to 
# change the variables in the VARS section. The main things 
# you would need to change are the homeDir, irodsHost, and
# irodsZone.


#------------------------
#----------VARS----------

homeDir="/home/vhub/"
irodsDir=".irods/"
irodsEnv=".irodsEnv"
bashProfile=".bash_profile"

irodsHost="irods.ccr.buffalo.edu"
irodsPort="1247"
irodsDefResource="vhub-ccr-buffalo"
irodsHome="/vhub/home/public"
irodsCwd="/vhub/home/public"
irodsUserName="anonymous"
irodsZone="vhub"

avoidUsers=('<admin>' '<apps>');



#------------------------
#----------MAIN----------

#work in the home dir
cd $homeDir

#list all dir in the home dir
for user in $(ls -d */)
do
    #check if its a dir
    if [ -d $user ]
    then
        #only proceed if user is NOT in avoidUsers array
        found=$(echo ${avoidUsers[*]} | grep "<${user%?}>")
        if [ "$found" == "" ]
        then
            #check if .irods dir !(already exists)
            if [ ! -d ${user}${irodsDir} ]
            then
                #create .irods dir in home folder
                su ${user%?} -c "mkdir ${user}${irodsDir}"
                #create & fill in .irodsEnv file in .irods dir
                irodsEnvFile=${user}${irodsDir}${irodsEnv}
                su ${user%?} -c "touch $irodsEnvFile"
                echo "# iRODS personal configuration file." >> $irodsEnvFile
                echo "# Created $(date)" >> $irodsEnvFile
                echo >> $irodsEnvFile
                echo "# iRODS server host name:" >> $irodsEnvFile
                echo "irodsHost '$irodsHost'" >> $irodsEnvFile
                echo "# iRODS server port number:" >> $irodsEnvFile
                echo "irodsPort '$irodsPort'" >> $irodsEnvFile
                echo >> $irodsEnvFile
                echo "# Default storage resource name:" >> $irodsEnvFile
                echo "irodsDefResource '$irodsDefResource'" >> $irodsEnvFile
                echo "# Home directory in iRODS:" >> $irodsEnvFile
                echo "irodsHome '$irodsHome'" >> $irodsEnvFile
                echo "# Current directory in iRODS:" >> $irodsEnvFile
                echo "irodsCwd '$irodsCwd'" >> $irodsEnvFile
                echo "# Account name:" >> $irodsEnvFile
                echo "irodsUserName '$irodsUserName'" >> $irodsEnvFile
                echo "# Zone:" >> $irodsEnvFile
                echo "irodsZone '$irodsZone'" >> $irodsEnvFile
				#mount iRODS automatically
				profile=${user}${bashProfile}
				if [ ! -e $profile ]
				then
					su ${user%?} -c "touch $profile"
				fi
				echo >> $profile
				echo "# Mount iRODS" >> $profile
				echo "if [ -n \$IRODS_MOUNT -a -e \$IRODS_MOUNT ]; then" >> $profile
				echo "\$IRODS_MOUNT" >> $profile
				echo "fi" >> $profile
            fi
        fi
    fi
done
