#!/bin/sh
#
# SliTaz tiny GUI Box for mount/unmount smb share folder
#
# note: $(gettext "text") doesn't work inside Yad so use `gettext \"text\"`
#
# Copyright (C) SliTaz GNU/Linux - GNU gpl v2
#
#  Authors: Stanislas Leduc <shann@slitaz.org>
#  Version: 0.99
#  Deps: smbclient smbfs 


# Internationalization
. /usr/bin/gettext.sh
TEXTDOMAIN='smbbox'
export TEXTDOMAIN

# Variables use by functions.
tmp="/tmp/partage"
# Icons for most windows
icon="/usr/share/pixmaps/netbox.png"

# Notification
notification () {
		if [ $(pidof smbbox-notify) ];then
		echo "nothing to do"
		else
		smbbox-notify
		fi
}
# Authentification
authentification () {
		# Request a domain name 
		domainname=`yad --entry --width=260 --height=100 --title="SmbBox - Domain Name" --text="Please enter Domain Name used for authentification (Active Directory accounts or other) (ex: JOLIG, WORKGROUP ..)"` 
		# Request username
		username=`yad --entry --width=260 --height=100 --title="SmbBox - Username" --text="Please enter authorize username to mount foldershare"` 
		# Request password 
		password=`yad --entry --width=260 --height=100 --title="SmbBox - Password" --hide-text="Please enter password for this username"` 
		# Mount share folder
		yad --info --title="Mount \"$folderdir\"" --text="We are mount \"$folderdir\" in $sharedir " && tazbox su "mount.cifs //$sharesrvname/$dir \"$sharedir\" -o user,sync,iocharset=utf8,domain=$domainname,user=$username,pass=$password,uid=1000,gid=100" && yad --info --title="SmbBox - Warning" --text="Display folders can take a few seconds, do not hesitate to refresh onglet" && pcmanfm "$sharedir" && notification && exit 0
}

# Mount share folder 
mount_smbshare() {
		# Fix exception sharefolder have space to the name
		addslashes() {
		echo "$1" | sed 's/\ /\\ /g'
		}
		dir="$(addslashes "$folderdir")"
		
		# Create own directory for share directory
		if [ -d "$tmp/$sharesrvname/$dir" ];then
		echo "nothing to do"
		else 
		mkdir -p $tmp/$sharesrvname/"$dir"
		fi
		sharedir=$tmp/$sharesrvname/"$dir"
		echo $sharedir
		
		# Save foldershare name and mount point for smbbox umount
		## addslashes x2 for umount.cifs command
		# Disque\ dur => Disque\\\ dur
		tmpdir="$(addslashes "$dir")"
		dirshare="$(addslashes "$tmpdir")"
		echo "$sharesrvname | $folderdir | $tmp/$sharesrvname/$dirshare" >> /tmp/mountshare.smbbox
		
		#Authentification
		yad --info --title="Authentification necessary ?" --text="You need to authentified for access to folder ?" --button="No:4" --button="Yes:1"
		ret=$?
		case $ret in
			1)  authentification;;
			4)	yad --info --title="Mount \"$folderdir\"" --text="We are mount \"$folderdir\" in $sharedir " && tazbox su "mount.cifs //$sharesrvname/$dir \"$sharedir\" -o user,sync,iocharset=utf8,domain=$sharesrvname,user=guest,pass=,uid=1000,gid=100" && yad --info --title="SmbBox - Warning" --text="Display folders can take a few seconds, do not hesitate to refresh onglet" && pcmanfm "$sharedir" && notification;;
		esac
}

# Catch share available and format output for GTK tree.
detect_smbshare() {
		smbclient -L $sharesrvname -U guest% --grepable | sed  -n "s/Disk|\(.*\)|\(.*\)/\1\n\2/p"
}

# Main GUI box function with pure Yad spec
smbbox_main() {
	# Request a share server name
	sharesrvname=`yad --entry --width=260 --height=100 --title="SmbBox - Share Server Name" --text="Please enter your share server name  or ip address (ex: SLITAZ, NASSIO, 192.168.1.15, ..)"` 

	# Information
	yad --info --width=260 --height=100 --title="SmbBox - Warning" --text="Display share folders can take a few seconds" 

	title=$(gettext "Smb Share")
	text=$(gettext "<b>Mount Share Folder</b> \(Double click to connect\)")
	rep=`detect_smbshare | yad --list --width=520 --height=300 \
		--center --on-top --window-icon="netbox" \
		--image="netbox" --image-on-top \
		--title="$title" --text="$text" \
		--column="$(gettext "ShareFolder Name")" --column="$(gettext "Description")" \
		--button="gtk-close:1"`
	folderdir=`echo $rep | awk -F"|" '{print $1}'`
}

#GUI box function for umount arguments
umount_smbshare() {
	umount_dir_propery() {
		sharesrv="`echo $rep | awk -F\"|\" '{print $1}'`"
		sharedir="`echo $rep | awk -F\"|\" '{print $2}'`" 
		mountpoint="`echo $rep | awk -F\"|\" '{print $3}'`" 
		tazbox su "umount.cifs `echo $mountpoint`" 
		sed -i '/^$/d' /tmp/mountshare.smbbox
		sed -i '/[$sharesrv | $sharedir]/d' /tmp/mountshare.smbbox
	}

	detect_mountsmbshare() {
		sed  -n "s/\(.*\)|\(.*\)|\(.*\)/\1\n\2\n\3/p" /tmp/mountshare.smbbox
	}
	
	title=$(gettext "Smb Share")
	text=$(gettext "<b>Mount Share Folder</b> \(Double click to disconnect\)")
	rep=`detect_mountsmbshare | yad --list --width=520 --height=300 \
		--center --on-top --window-icon="netbox" \
		--image="netbox" --image-on-top \
		--title="$title" --text="$text" \
		--column="$(gettext "Share Server Name")" --column="$(gettext "ShareFolder Name")" --column="$(gettext "Mount Point")" \
		--button="gtk-close:1"`
	
	ret=$?
	case $ret in
		1)
			echo "nothing to do";;
		*)
			umount_dir_propery;;
	esac
}

# Main function
smbbox() {
	# Store box results
	smbbox_main
	ret=$?
	# Deal with --button values
	case $ret in
		1) exit 0 ;;
		*) mount_smbshare && exit 0 ;;
	esac
}

#
# Script commands
#

case "$1" in
	usage|help|-*)
		echo "$(gettext "Usage:") $(basename $0)" ;;
	umount)
		umount_smbshare ;;
	*)
		smbbox ;;
esac

exit 0
