#!/bin/sh
#
# SliTaz Secure File Storage GTK user interface using Yad.
#
# Copyright (C) SliTaz GNU/Linux - BSD License
# Author: Christophe Lincoln <pankso@slitaz.org>
#

app=$(basename $0)
config=$HOME/.config/ssfs/client.lua
pixmap=/usr/share/pixmaps/ssfs.png

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

#
# Functions
#

info() {
	size=$(du -sh $HOME/Sync | awk '{print $1}')
	files=$(find $HOME/Sync | wc -l)
	host=$(fgrep 'host' $config | cut -d '"' -f 2)
	login=${host%@*}
	host=${host#*@}
	echo -e "Login\n$login
Host\n$host
Size\n$size
Files\n$files
RSA Key\n~/.ssh/id_rsa"
}

# Default tools GUI box function.
tools_main() {
	if [ ! -s "$config" ]; then
		$0 setup && exit 0
	fi
	[ "$opts" ] || opts="--width=520 --height=320"
	text=$(gettext "<b>Welcome to the Ssfs Client user interface</b>")
	info | yad $opts \
		--list --title="Ssfs Box" \
		--image-on-top --window-icon=$pixmap \
		--text="$text" --image=$pixmap \
		--column "Ssfs" --column "$(gettext "Value")" \
		--button="$(gettext "Setup client"):5" \
		--button="$(gettext "Edit config"):4" \
		--button="$(gettext "Browse files"):3" \
		--button="$(gettext "SSH Login"):2" \
		--button="gtk-close:1"
}

# Default tools functions.
tools() {
	# Store box results
	main=$(tools_main)
	# Deal with --button values
	case $? in
		1) exit 0 ;;
		2) terminal -e "ssfs login" ;;
		3) file-manager $HOME/Sync ;;
		4) editor $config ;;
		5) $0 setup ;;
		*) continue ;;
	esac
	case $main in
		Files*) file-manager $HOME/Sync ;;
		RSA*)
			yad --text-info --title="RSA Key" \
				--width=560 --height=420 \
				--filename=$HOME/.ssh/id_rsa \
				--button="gtk-close:1" ;;
		*)
			continue ;;
	esac
}

# Setup GUI box function.
setup_main() {
	text=$(gettext \
"<b>Welcome to the Ssfs Setup</b>\n
Any account on a server yet ? You can vist www.slitaz.org
services or setup your own server in a few minutes!\n")
	yad --form --title="Ssfs Setup" \
		--window-icon=$pixmap \
		--width=460 --height=200 --image-on-top \
		--text="$text" --image=$pixmap \
		--field="Login" --field="Server"
}

# Default tools functions.
setup() {
	# Store box results and setup.
	main=$(setup_main)
	[ $? == 1 ] && exit 0
	login=$(echo $main | cut -d '|' -f 1)
	host=$(echo $main | cut -d '|' -f 2)
	[ "$host" ] || exit 0
	rm -f $config
	terminal -hold -geometry 76x16 -T "Ssfs" \
		-e "ssfs setup --login=$login --host=$host"
}

# Notification mode.
notify() {
	export opts="--geometry=520x320-40+40 --timeout=6 --skip-taskbar"
	yad --notification --image=$pixmap --command=$0 \
		--text="$(gettext "Ssfs Live Sync storage")"
}

#
# Commands
#

case "$1" in
	help)
		echo "Usage: $app [command]" ;;
	setup)
		setup ;;
	notify)
		notify ;;
	*)
		tools ;;
esac
exit 0
