#!/bin/bash

CONFIGFILE="./login.cfg"
source ${CONFIGFILE}

DEBUG=echo
#DEBUG=:


#define functions

#generate desktop icon, ${1} is the name of the desktop icon, it must end with
#.$FILESYSTEM.desktop as on logout icons are destroyed which match this name ending,
#${2} is the mount point under $HOME/ where the icon points to, also this is the 
#icon name which appears on the desktop. ${3} is the type of icon
#Maybe we don't have to do this for OsX, finder does it automatically.
generate_icon()
{
#GenIcon 1 ${1} 2 ${2} 3 ${3}"
case "${3}" in 
cifs)
	ICON=/usr/share/icons/win-folder_open.png;;
sshfs)
	ICON=/usr/share/icons/tux-folder_open.png;;
nfs)
	ICON=/usr/share/icons/nfs-folder_open.png;;
ncp)
	ICON=/usr/share/icons/ncp-folder_open.png;;
esac

if [ "${DESKTOP_SESSION}" == "kde" ] 
then
cat <<EOF >${HOME}/Desktop/${1}.${3}.desktop
#!/usr/bin/env xdg-open
[Desktop Entry]
Encoding=UTF-8
Name=${1}:
Name[${LANG}]=${1}:
GenericName=${2}
GenericName[${LANG}]=${2}
Icon=${ICON}
Type=Link
URL=${HOME}/${1}/
EOF


elif [ "${DESKTOP_SESSION}" == "gnome" ] 
then
cat <<EOF >${HOME}/Desktop/${1}.${3}.desktop
#!/usr/bin/env xdg-open
[Desktop Entry]
Encoding=UTF-8
Name=${1}:
Name[${LANG}]=${1}:
GenericName=${2}
GenericName[${LANG}]=${2}
Icon=${ICON}
Type=Link
URL=${HOME}/${1}/
EOF
	

else
${DEBUG} "Desktop Type is unknown, your Desktop Icon for ${3} may not work."
cat <<EOF >${HOME}/Desktop/${1}.${3}.desktop
#!/usr/bin/env xdg-open
[Desktop Entry]
Encoding=UTF-8
Name=${1}:
Name[${LANG}]=${1}:
GenericName=${2}
GenericName[${LANG}]=${2}
Icon=${ICON}
Type=Link
URL=${HOME}/${1}/
EOF
        

fi

#cat <<EOF >${HOME}/Desktop/${1}.${3}.desktop
##!/usr/bin/env xdg-open
#[Desktop Entry]
#Encoding=UTF-8
#Name=${1}:
#Name[${LANG}]=${1}:
#GenericName=${2}
#GenericName[${LANG}]=${2}
#Icon=${ICON}
#Type=Link
#URL=${HOME}/${1}/
#EOF

#End of Icon Create function
}

#Test if mount point exists, need to add code to check if success or fail and if mount is readable
#or will overwrite existing data. Dir created with "mkdir -m 700 test" for drwx------
test_dir_exists()
{
${DEBUG} ""
${DEBUG} "=>Checking if mount point exists"
if [ -d ${1} ]; then
        ${DEBUG} "==>Mount point ${1} exists"
	test_dir_empty ${1}
else
	${DEBUG} "==>Creating mount point ${1}"
        mkdir -m 700 ${1}
fi
}

#Test if mount point is empty, or will mounting overmount files, ${1} is dir to test.
test_dir_empty()
{
${DEBUG} ""
${DEBUG} "=>Checking if mount point is empty"
echo ${1}/* | grep '\*$' > 2&>1
DIRRC=$?
if [ ${DIRRC} -eq 0 ]; then
        ${DEBUG} "==>Mount point directory ${1} is empty"
else
        ${DEBUG} "==>Mount point directory ${1} is NOT empty, Mounting will hide some files."
fi
}

#Test if mount point is already mounted
test_mountpoint()
{
${DEBUG} ""
${DEBUG} "=>Checking to see if ${1} is mounted."
mount | grep ${1}  
#2&>1
MPBUSYRC=$?
if [ ${MPBUSYRC} -eq 0 ]; then 
	${DEBUG} "==>Mount point ${1} is already mounted."
	MOUNTSTATUS=0
	exit 99
else
	${DEBUG} "==>Mount point ${1} is available."	
fi
}



# Function to mount via cifs.
login_cifs() {

#Do CIFS mounts, ${1}=domain, ${2}=server, ${3}=shared dir name, ${4}=mount point, ${6}=User name, ${7}=Comment
#Useage:- /sbin/mount.cifs //10.85.0.153/Support /home/andrew/I -o dom=addm,user=bccaawsr

${DEBUG} ""
${DEBUG} "==>login_cifs"

#Test if mount point exists
test_dir_exists ${HOME}/${LOGIN_LOCALPATH[${1}]}
test_mountpoint ${HOME}/${LOGIN_LOCALPATH[${1}]}

${DEBUG} "=>Running ${CIFSPATH}/${CIFS} //${LOGIN_SERVER[${1}]}/${LOGIN_SERVPATH[${1}]} ${HOME}/${LOGIN_LOCALPATH[${1}]} -o dom=${LOGIN_DOMAIN[${1}]},user=${LOGIN_USER[${1}]} &&"

${CIFSPATH}/${CIFS} //${LOGIN_SERVER[${1}]}/${LOGIN_SERVPATH[${1}]} ${HOME}/${LOGIN_LOCALPATH[${1}]} -o dom=${LOGIN_DOMAIN[${1}]},user=${LOGIN_USER[${1}]} &&

#see if mount has succeded by testing for mounted directory
mount | grep ${HOME}/${LOGIN_LOCALPATH[${1}]} 
#2&>1
CIFSMOUNTOKRC=$?
if [ ${CIFSMOUNTOKRC} -eq 0 ]; then
	${DEBUG} "==>Mount on ${HOME}/${LOGIN_LOCALPATH[${1}]} succeeded."
	#squirt out desktop icon, name, mount, filesystem
	${DEBUG} "==>Writing out icon file."
	generate_icon ${LOGIN_LOCALPATH[${1}]} ${LOGIN_USER[${1}]} cifs
else
        ${DEBUG} "==>Mount on ${HOME}/${LOGIN_LOCALPATH[${1}]} failed."
fi
}



#Do sshfs mounts to Linux/Unix Servers
login_sshfs()
{
#Useage:- sshfs andrew@hosting2.unix.brm.pri:/home/andrew tmp/

${DEBUG} ""
${DEBUG} "==>login_sshfs"

#Test if mount point exists
test_dir_exists ${HOME}/${LOGIN_LOCALPATH[${1}]}
test_mountpoint ${HOME}/${LOGIN_LOCALPATH[${1}]}


#Using -o nonempty to allow mounting over non empty mount points.
${DEBUG} "=>Running $SSHFSPATH/$SSHFS -o nonempty ${LOGIN_USER[${1}]}@${LOGIN_SERVER[${1}]}:${LOGIN_SERVPATH[${1}]} ${HOME}/${LOGIN_LOCALPATH[${1}]}"
$SSHFSPATH/$SSHFS -o nonempty ${LOGIN_USER[${1}]}@${LOGIN_SERVER[${1}]}:${LOGIN_SERVPATH[${1}]} ${HOME}/${LOGIN_LOCALPATH[${1}]}

#see if mount has succeded by testing for mounted directory
mount | grep ${HOME}/${LOGIN_LOCALPATH[${1}]} 
#2&>1
SSHMOUNTOKRC=$?
if [ ${SSHMOUNTOKRC} -eq 0 ]; then
        ${DEBUG} "==>Mount on ${HOME}/${LOGIN_LOCALPATH[${1}]} succeeded."
        #squirt out desktop icon, name, mount, filesystem
        ${DEBUG} "==>Writing out icon file."
	#squirt out desktop icon
	generate_icon ${LOGIN_LOCALPATH[${1}]} ${LOGIN_USER[${1}]} sshfs
else
        ${DEBUG} "==>Mount on ${HOME}/${LOGIN_LOCALPATH[${1}]} failed."
fi
}


#Netware login
login_ncp() 
{

${DEBUG} ""
${DEBUG} "==>ncp login"

test_dir_exists ${HOME}/${LOGIN_LOCALPATH[${1}]}
test_mountpoint ${HOME}/${LOGIN_LOCALPATH[${1}]}

#1st login to Netware
#-S is server -U is username
${DEBUG} "=>Running ${NCPPATH}/${NCPLOGIN} -A ${LOGINSERVER} -S ${LOGINSERVER} -U ${USER$CONTEXT}"
${NCPPATH}/${NCPLOGIN} -A ${LOGINSERVER} -S ${LOGINSERVER} -U ${USER}${CONTEXT}
NCPLOGINRC=$?

#map H drive to ~/H if logged in OK.
if [ ${NCPLOGINRC} -eq 0 ]; then
	test_dir_exists ${HOME}/${LOGIN_LOCALPATH[${1}]}
	test_mountpoint ${HOME}/${LOGIN_LOCALPATH[${1}]}
	
	${NCPPATH}/${NCPMAP} -A saturn -T ${TREE} -V .SATURN_VOL1.MEDIAHUB -R users/${USER} H 
fi
#squirt out desktop icon
#generate_icon H "Personal files on Saturn"
}






#Function to get users passwords
#$ cat  login.cfg | grep LOGIN_FSTYPE
#LOGIN_FSTYPE[0]=cifs               
#LOGIN_FSTYPE[1]=sshfs              
#LOGIN_FSTYPE[2]=cifs

getpassword()
{
${DEBUG} "Passwords are not used from here yet! For testing they WILL be echoed to the screen."

if [ `cat ${CONFIGFILE} | grep 'LOGIN_FSTYPE' | grep cifs` ];then
	echo -n "CIFS password:- "
	read -s CIFSPASSWORD
	echo " "
	if [ -n "${CIFSPASSWORD}" ];then
		echo "CIFS pw is valid"
		else
		echo "CIFS pw is blank"
	fi


elif [ `cat ${CONFIGFILE} | grep LOGIN_FSTYPE | grep sshfs` ];then
	echo -n "SSHFS password:- "
	read -s SSHFSPASSWORD
	echo " "
	if [ -n "${SSHFSPASSWORD}" ];then
		echo "SSHFS pw is valid"
		else
		echo "SSHFS pw is blank"
	fi	


elif [ `cat ${CONFIGFILE} | grep LOGIN_FSTYPE | grep ncp` ];then
	echo -n "NCP password:- "
	read -s NCPPASSWORD
	echo " "
	if [ -n "${NCPPASSWORD}" ];then
		echo "NCP pw is valid"
		else
		echo "NCP pw is blank"
	fi
fi

echo "CIFS Password is >${CIFSPASSWORD}<"
echo "SSHFS Password is >${SSHFSPASSWORD}<"
echo "NCP Password is >${NCPPASSWORD}<"
}


#==========================End of functions================================




#++++++++++++++++++++++++++Main Code+++++++++++++++++++++++++++++++++++++++

${DEBUG} "==> Debug mode is ON <=="

${DEBUG} "=> You're using ${DESKTOP_SESSION} <="

#Set up command names, paths are found in osguess block
SSHFS=sshfs
CIFS=mount.cifs
NCPLOGIN=ncplogin
NCPMAP=ncpmap
NCPMOUNT=mount.ncpfs

#Try to guess the OS to set location for binaries which may not be in the $PATH
OSGUESS=`uname -a | cut -d" " -f1 -`

if [ "${OSGUESS}" == "Linux" ];then
        ${DEBUG} "OS is Linux"

	SSHFSPATH=/usr/bin
	${DEBUG} "sshfs is ${SSHFSPATH}/${SSHFS}"

	CIFSPATH=/sbin
	${DEBUG} "cifs is ${CIFSPATH}/${CIFS}"
	
	NCPPATH=/sbin
	${DEBUG} "ncpfs is ${NCPPATH}/${NCPMOUNT}"


elif [ "${OSGUESS}" == "Darwin" ];then
        ${DEBUG} "OS is Mac - OsX"

	SSHFSPATH=/Applications/sshfs/bin
	SSHFS=mount_sshfs
	${DEBUG} "sshfs is ${SSHFSPATH}/${SSHFS}"
	CIFSPATH=/sbin

	CIFS=mount_smbfs
	${DEBUG} "cifs is ${CIFSPATH}/${CIFS}"
	NCPPATH=/usr/bin

else
        ${DEBUG} "OS unsupported, but this is OpenSource, so try to fix it!"
        exit 1
fi

#Get users passwords for future use.
getpassword


# Loop though the arrays and mount each using the login_* functions.
for (( I=0; $I < ${#LOGIN_SERVER[*]}; I++ )); do
  login_${LOGIN_FSTYPE[$I]} $I
  if [ $? -ne 0 ]; then
    echo "Mount error, wrong fs type maybe?"
    exit 1
  fi
done

echo ""
#show cifs mounts
echo "********* Current CIFS Connections *********"
mount |grep cifs
echo "********************************************"
echo""
echo "********* Current SSH_FS Connections *********"
mount |grep sshfs
echo "********************************************"
echo""


exit 0


