#!/bin/bash

if [[ ! -d ~/.config/hubiC/ ]]; then
	echo "~/.config/hubiC/ does not exist, hubic is not configured?"
elif [[ $1 == "start" ]]; then
	if [[ ! -e /tmp/hubic.dbus ]]; then
		# Stocker une nouvelle session DBUS dans un fichier
		dbus-daemon --session --fork --print-address > /tmp/hubic.dbus

		# Charger la session DBUS
		export DBUS_SESSION_BUS_ADDRESS=`cat /tmp/hubic.dbus`

		# Démarrer de HubiC
		hubic start
	else
		echo "/tmp/hubic.dbus already exist"
	fi
elif [[ $1 == "stop" ]]; then
	if [[ -e /tmp/hubic.dbus ]]; then
		# Charger la session DBUS
		export DBUS_SESSION_BUS_ADDRESS=`cat /tmp/hubic.dbus`

		# Arrêt de hubiC
		hubic stop

		# Suppression de le fichier contenant la session DBUS
		rm /tmp/hubic.dbus
	else
		echo "/tmp/hubic.dbus does not exist"
	fi
elif [[ $1 == "keepalive" ]]; then
	if [[ -e /tmp/hubic.dbus ]]; then
		# Charger la session DBUS
		export DBUS_SESSION_BUS_ADDRESS=`cat /tmp/hubic.dbus`

		# Forcer la synchronisation
		hubic synchronize
	else
		echo "/tmp/hubic.dbus does not exist"
	fi
else
	echo "Script to simplify management of hubiC."
	echo
	echo "This script needs one of the following settings to work:"
	echo -e "\t- start\t: start hubiC in background "
	echo -e "\t- stop\t: stop hubiC"
fi
