#!/bin/sh
#
# Copyright 2006, 2007 by
# Manuel Carrasco
#
# This file is part of the LookXP project, and may only be used, modified,
# and distributed under the terms of the LookXP project license,
# LICENSE.TXT.  By continuing to use, modify, or distribute this file you
# indicate that you have read the license and understand and accept it
# fully.
#
#             http://lxp.sourceforge.net

case $LANG in
	es*)
		escritorio_rox="minino_es"
		NINGUNO="ninguno" 
	;;
	gl*)
		escritorio_rox="minino_gl"
		NINGUNO="ningún"
	;;
	*)	# Other
		escritorio_rox="minino_en"
		NINGUNO="none"
	;;
esac

packname=lxp-icewm

DESKS="lxp-icewmbg idesk  thunar xfdesktop nautilus kdesktop rox ROX-Filer pcmanfm none"

isInstalled() {
  [ -z "$1" ] && return 1
  H=`which "$1" 2>/dev/null`
  [ -n "$H" -a -x "$H" ] && return 0 || return 1
}

istalled=""
for i in $DESKS
do
	isInstalled $i && installed="$installed $i"
done

printMenu() {
    default=`cat ~/.$packname/.defaultdesktop 2>/dev/null | tr -d " "`
    for i in $installed
    do
	[ "$i" = icewmbg -o "$i" = lxp-icewmbg ] && n=$NINGUNO || n=$i
	if [ "$default" = $i ]
	then
		echo prog "$n" "desktop" $0 --set $i
	else
		echo prog "$n" "default" $0 --set $i
	fi
    done
}

Set() {
	[ -z "$1" ] && return
	killall $DESKS
	sleep 1
	WP=/usr/local/rox-filer-2.9/fondos/rojo.jpg
	case $1 in
		xfdesktop)
			xfdesktop &
			;;
		thunar)
			thunar &
			;;
		idesk)
		## Some times idesk refused to start
			idesk &
			P=$!
			(
				sleep 1
				kill -0 $! || (sleep 4; idesk &) &
			) &
			;;
		nautilus)
		        ## Some times nautilus desktop refused to start
			nautilus &
			P=$!
			(
				sleep 4
				kill -0 $P 2>/dev/null || (
				      lxp-command --error \
				      "Can not run Nautilus Desktop.\n Closing your session and relogin again will solve the problem" 
				      ) &
			) &
			;;
		pcmanfm)
			pcmanfm &
			;;
	        icewmbg)
			icewmbg &
			;;
	        lxp-icewmbg)
			lxp-icewmbg &
			;;
	        rox)
			rox -p $escritorio_rox -t $escritorio_rox &
			;;
		*)
			return
			;;
	esac
	echo $1 > ~/.$packname/.defaultdesktop
}

case $1 in
	--menu) printMenu;;
	--set)  Set $2;;
	*)      echo "Usage $0 <--menu> | <--set desktop>";;
esac

