#!/bin/bash
# DATE: 	Jan 13, 2012
# AUTHOR:	Kyle Marcus

# Sets up the correct irods files in the user's home directory so
# that they will be able to access iRODS through the i-commands.
# It also initalizes the users irods password and mounts their 
# home directory in the ~/irods directory.

# Creates the ~/.irods directory if it does not already exist and
# then creates the ~/.irods/.irodsEnv configuration file. Then it
# appends to the bash profile configuration file the necessary 
# commands to load iRODS with fuse when the user logs into the 
# hub environment. It runs the 'use irods' command to allow the
# user to use the i-commands to interact with iRODS. Finally it
# initalizes the irods password and mounts the users home dir in
# irods to ~/irods.

# NOTE: By just running the script, it will set up iRODS
# configuration with the correct setup to view the public dir
# for the vhub iRODS server. To change this to use your own 
# username or different server, edit the 'VARS' section or use
# the '-i' command line argument to run the script in interactive
# mode and answer the questions asked by the script.

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

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"


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

if [ "$1" == "-i" ]
then
	
	clear
	
	echo "Answer the following questions to configure iRODS"
	echo "Press ENTER for the default setting for each question"
	echo

	echo -ne "iRODS server name [$irodsHost]: "
	read input
	if [ ! -z "$input" ]
	then
		irodsHost=$input
	fi

	echo -ne "iRODS port [$irodsPort]: "
	read input
	if [ ! -z "$input" ]
	then
		irodsPort=$input
	fi

	echo -ne "iRODS username [$irodsUserName]: "
	read input
	if [ ! -z "$input" ]
	then
		irodsUserName=$input
	fi

	echo -ne "iRODS zone [$irodsZone]: "
	read input
	if [ ! -z "$input" ]
	then
		irodsZone=$input
	fi

	echo -ne "iRODS resource name [$irodsDefResource]: "
	read input
	if [ ! -z "$input" ]
	then
		irodsDefResource=$input
	fi
	
	if [ "$irodsUserName" == "anonymous" ]
	then
		echo -ne "iRODS home dir [/${irodsZone}/home/public]: "
	else
		echo -ne "iRODS home dir [/${irodsZone}/home/${irodsUserName}]: "
	fi
	read input
	if [ ! -z "$input" ]
	then
		irodsHome=$input
	else
		if [ "$irodsUserName" == "anonymous" ]
		then
			irodsHome="/${irodsZone}/home/public"
		else
			irodsHome="/${irodsZone}/home/${irodsUserName}"
		fi
	fi

	echo -ne "iRODS current working dir [$irodsHome]: "
	read input
	if [ ! -z "$input" ]
	then
		irodsCwd=$input
	else
		irodsCwd=$irodsHome
	fi
	
fi

cd ~

if [ ! -d $irodsDir ]
then
	#create .irods dir in home folder
	mkdir $irodsDir
fi

irodsEnvFile=${irodsDir}${irodsEnv}
if [ ! -e $irodsEnvFile ]
then
	#create & fill in .irodsEnv file in .irods dir
    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
	if [ ! -e $bashProfile ]
	then
		touch $bashProfile
	fi
	echo >> $bashProfile
	echo "# Mount iRODS" >> $bashProfile
	echo "if [ -n \$IRODS_MOUNT -a -e \$IRODS_MOUNT ]; then" >> $bashProfile
	echo "\$IRODS_MOUNT" >> $bashProfile
	echo "fi" >> $bashProfile
else
	echo "The $irodsEnvFile file alrady exists"
fi

#use irods
eval `/etc/environ sh use irods`

#save irods password
iinit

#mount irods home dir to ~/irods
source $bashProfile
