#!/bin/bash

# author dal
# mail dal_ml@me.com
# version 0.2 -> SECOND SHOT, still a QUICK HACK!

# this script mounts a owncloud-drive depending on the current
# network-situation.
# once mounted two rsyncs are performed.

# the mac-routines use an ordinary mount.
# the linux-routines depend on gvfs to be installed.
#	an ordinary mount should possible as long a the /etc/fstab is modifable
#	a third option could be fuse.

clear

########################################USER-SETTINGS

USER=username
SMB_SHARE=${USER}"/"											#in my setup the sharename equals the username.
OWNCLOUDHOME=~/owncloud/
SMB_HOST=host													#local domain name or ip
SMB_URI=${USER}"@"${SMB_HOST}"/"${SMB_SHARE}					#the smb uri
WEBDAV_HOST=my.tld												#my.tld or ip
WEBDAV_URI=${USER}"@"${WEBDAV_HOST}"/cloud/files/webdav.php"	#webdav uri

MOBILE_APN=internet.eplus.de									#mobile network apn
# The apn of the mobile-net-provider. This should only be reachable
# from within the mobile network itself. In this case sync will
# be aborted, because of possible fees.
#for germany see http://www.nethosting24.de/faq/questions/139/APN-Einstellungen-f%FCr-deutsche-Provider-%3F


########################################END OF USER-SETTINGS

######################################## HELPY HELPERS
#will be set later when we know which OS we're dealing with.
WEBDAV_PROTO=
SMB_PROTO=
OCMOUNTPOINT=
INDICATOR=.time			#the indicator FILE will be touched whenever a sync happens.
						#FIXME this concept can't work.
GVFSHOME=~/.gvfs/

#give the spoken/printed words some space
function print (){
	echo
	echo $1
	echo
}
######################################## END OF HELPY HELPERS
######################## mac-specific

#decides which protocol should be used and mounts the according device.
macMount(){
	#mobile-network? -> exit!
	if ping -c 1 ${MOBILE_APN} ; 
		then
			print "aborting you're on a mobile net; this measure will save your quota."
			exit 0
	fi
	
	OCMOUNTPOINT=~/.owncloudtmp/
	#local network? -> samba!
	if ping -c 1 ${SMB_HOST} ; 
	then
		mkdir ${OCMOUNTPOINT}
		print ${SMB_PROTO}${SMB_URI}
		mount -t smbfs ${SMB_PROTO}${SMB_URI} ${OCMOUNTPOINT}
	els
		#remote network? -> webdav!
		if ping -c 1 ${WEBDAV_HOST} ;
		then
			mkdir ${OCMOUNTPOINT}
			mount_webdav -i ${WEBDAV_PROTO}${WEBDAV_URI} ${OCMOUNTPOINT}
		else
			print "remote not reachable"
			exit 0
	fi
fi
}

######################## lin-specific

# gvfs has an rude behaviour.
# it mounts volumes into ~/.gvfs/ and creates volume-names depending
# on a) username and b) server-path.
# this means the OCMOUNTPOINT is barely predictable and must be retreived afterwards.
linMountSamba(){
		print "mounting ${SMB_PROTO}${SMB_URI}"
		#TODO can someone please solve this more elegant? with awk/sed/... whatever?!
		gvfs-mount ${SMB_PROTO}${SMB_URI}
		cmd="gvfs-mount -l | grep "${SMB_PROTO}${SMB_URI}
		DAVMOUNT=`eval $cmd`
		#we need param 2,3,4
		#cmd= "echo $DAVMOUNT | awk -F' ' '{ print \$2}'"
		#VOLUMEPART1= `eval $cmd`
		#OCMOUNTPOINT=${GVFSHOME}$VOLUMEPART1" "
		#echo $DAVMOUNT
		#echo $VOLUME
		#echo ${OCMOUNTPOINT}
}

#WILL HAVE THE SAME ISSUE WITH PARAM 2,3,4,5
linMountWebDAV(){
	gvfs-mount ${WEBDAV_PROTO}${WEBDAV_URI}
}

#decides which protocol should be used and mounts the according device..
linMount(){
	#mobile-network? -> exit!
	if ping -c 1 ${MOBILE_APN} ; 
	then
		print "aborting you're on a mobile net; this measure will save your quota."
		exit 0
	fi
	
	#local network? -> samba!
	if ping -c 1 ${SMB_HOST} ; 
	then
		linMountSamba
	else
		#remote network? -> webdav!
		if ping -c 1 ${WEBDAV_HOST} ;
		then
			linMountWebDAV
		else
			print "remote not reachable"
			exit 0
		fi
	fi
}

######################### which plattform?

#decides which procedure mac/lin to call.
mountDrive(){
	SYSTEM=$(uname)
	if [ $SYSTEM = "Darwin" ] ;
	then
		WEBDAV_PROTO=https://
		SMB_PROTO=//
		macMount
	elif [ $SYSTEM = "Linux" ] ;
	then
		WEBDAV_PROTO=davs://
		SMB_PROTO=smb://
		linMount
	else
		print "your system is not supported, yet?!"
		exit 0
	fi
}

unMountDrive(){
	SYSTEM=$(uname)
	if [ $SYSTEM = "Darwin" ] ;
	then
		umount ${OCMOUNTPOINT}
		rm -rf ${OCMOUNTPOINT}
	elif [ $SYSTEM = "Linux" ] ;
	then
		#TODO lookup if SAMBA or WebDAV was used.
		# till then this message will occur: Error finding enclosing mount:
		gvfs-mount -u ${WEBDAV_PROTO}${WEBDAV_URI}
		gvfs-mount -u ${SMB_PROTO}${SMB_URI}			
	else
		print "your system is not supported, yet?!"
		exit 0
	fi
}



#TODO still need some help on this one.
syncRemoteAndLocal(){
	# r = recursive
	# v = verbose
	# c = check based on checksum         
	# a = archive
	# u = skip files that are newer on the receiver
	# l = copy symlinks as symlinks
	# m = prune-empty-dirs
	# z = compress files during transfer
	# P = keep Partial ...
	#TODO params not elaborated, yet. any rsync-experts out there?
	# t = preserve modification times
	PARAMS=-rumzPt
	#v
	#TODO seems not to work propper.
	#TODO read excludes from file?
	#EXCLUDETHIS="--exclude '*.bak' --exclude '*.~' --exclude '.DS_Store' --exclude '.TemporaryItems' --exclude '._.DS_Store' --exclude '._.TemporaryItems'"

	#determine the syncing order (which directory is newer?)
	#TODO is it possible that the creation of the OCMOUNTPOINT affects the timestamp?
	#last few syncs echoed "remote files newer".
	rm ${OWNCLOUDHOME}${INDICATOR}
	touch ${OWNCLOUDHOME}${INDICATOR}
	if [ ${OWNCLOUDHOME}${INDICATOR} -nt ${OCMOUNTPOINT}${INDICATOR} ] ;
	then
		print "local files are newer, syncing local to remote first."
		#TODO can rsync syn bidirectional in one command?
		print ${OWNCLOUDHOME}
		print ${OCMOUNTPOINT}
		
		cmd="rsync $PARAMS $EXCLUDETHIS ${OWNCLOUDHOME} ${OCMOUNTPOINT}"
		eval $cmd
		cmd="rsync $PARAMS $EXCLUDETHIS ${OCMOUNTPOINT} ${OWNCLOUDHOME}"
		print "the other way around."
		eval $cmd
	elif [ ${OWNCLOUDHOME} -ot ${OCMOUNTPOINT} ] ;
	then
		print "remote files are newer, syncing remote to local first."
		cmd="rsync $PARAMS $EXCLUDETHIS ${OCMOUNTPOINT} ${OWNCLOUDHOME}"
		eval $cmd
		cmd="rsync $PARAMS $EXCLUDETHIS ${OWNCLOUDHOME} ${OCMOUNTPOINT}"
		eval $cmd
	else
		exit 0
	fi
}

############################################################################################## entry
mountDrive
syncRemoteAndLocal
#unMountDrive


