#!/bin/sh

#    Back In Time
#    Copyright (C) 2008-2009 Oprea Dan
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

if [ -f backintime.py ]; then
	BACKINTIME_PATH="./"
	PYTHON_BACKINTIME_PATH=""
else
	BACKINTIME_PATH="/usr/bin/"
	PYTHON_BACKINTIME_PATH="/usr/share/backintime/"
fi

GUI="auto"

for arg in $*; do
	case $arg in
		--gnome) GUI="gnome";;
		--kde4)  GUI="kde4";;
		--kde3)  GUI="kde3";;
		--nogui) GUI="";;
	esac
done

#auto-detect GUI
if [ $GUI = "auto" ]; then
	if [ "$(pidof ksmserver)" ]; then
		KDE_VERSION=`ksmserver --version | grep KDE:`
		if [ "$(echo $KDE_VERSION | grep 'KDE: 4.')" ]; then
			GUI="kde4"
		else
			GUI="kde3"
		fi
	else
		GUI="gnome"
	fi

	echo "Autodetect GUI mode: $GUI"
fi

CMD="python ${PYTHON_BACKINTIME_PATH}backintime.py"

#check if the selected gui exists
if [ $GUI = "kde4" ]; then
#try: kde4, kde3, gnome
	if [ -f ${BACKINTIME_PATH}backintime-kde4 ]; then
		CMD="${BACKINTIME_PATH}backintime-kde4"
	elif [ -f ${BACKINTIME_PATH}backintime-kde3 ]; then
		CMD="${BACKINTIME_PATH}backintime-kde3"
	elif [ -f ${BACKINTIME_PATH}backintime-gnome ]; then
		CMD="${BACKINTIME_PATH}backintime-gnome"
	fi
elif [ $GUI = "kde3" ]; then
#try: kde3, kde4, gnome
	if [ -f ${BACKINTIME_PATH}backintime-kde3 ]; then
		CMD="${BACKINTIME_PATH}backintime-kde3"
	elif [ -f ${BACKINTIME_PATH}backintime-kde4 ]; then
		CMD="${BACKINTIME_PATH}backintime-kde4"
	elif [ -f ${BACKINTIME_PATH}backintime-gnome ]; then
		CMD="${BACKINTIME_PATH}backintime-gnome"
	fi
elif [ $GUI = "gnome" ]; then
#try: gnome, kde4, kde3
	if [ -f ${BACKINTIME_PATH}backintime-gnome ]; then
		CMD="${BACKINTIME_PATH}backintime-gnome"
	elif [ -f ${BACKINTIME_PATH}backintime-kde4 ]; then
		CMD="${BACKINTIME_PATH}backintime-kde4"
	elif [ -f ${BACKINTIME_PATH}backintime-kde3 ]; then
		CMD="${BACKINTIME_PATH}backintime-kde3"
	fi
fi

echo "CMD: $CMD"
$CMD $*

