#!/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" 		
 ;;
 gl*)
      escritorio_rox="minino-gl"		
 ;;
   *)	# Other
      escritorio_rox="minino-en"		
 ;;
esac


packname=lxp-icewm

DESKS="lxp-icewmbg idesk xfdesktop nautilus kdesktop rox ROX-FIler pcmanfm"

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=none || 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/share/icewm/themes/WinXP-Plain/wallpaper.png
	case $1 in
		xfdesktop)
			xfdesktop &
			;;
		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" 
				      ) &
			) &
			;;
		kdesktop)
			kdesktop &
			;;
	        icewmbg)
			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

