#!/bin/bash

# Desktop Switching Tool
# (c) 1999, 2000 Red Hat, Inc.

XSESSION="$HOME/.xsession"
XINITRC="$HOME/.xinitrc"

DATADIR="/usr/share/apps/switchdesk/"

if [ "$1" = "" ]; then
    echo "Please specify one of either KDE, GNOME, or AnotherLevel."
    exit 1
fi

# set up the .Xclients variable on a per-display basis
if [ "$DISPLAY" != "" -a "$2" = "local" ]; then
    if [ `echo $DISPLAY | cut -c 1` = ':' ]; then
	SUB_XCLIENTS="$HOME/.Xclients-$HOSTNAME$DISPLAY"
    else
        SUB_XCLIENTS="$HOME/.Xclients-$DISPLAY"
    fi
else
    SUB_XCLIENTS="$HOME/.Xclients-default"
fi

XCLIENTS="$HOME/.Xclients"

# first off, check to see if they want to revert their settings
if [ "$1" = "revert" ]; then
    mv -f $XCLIENTS.switchdesk $XCLIENTS
    echo "Desktop reverted to your personal settings."
    exit 0
fi

# otherwise, do the normal stuff.
if [ -e "$XSESSION" ]; then
    echo "warning: moving $XSESSION to $XSESSION.switchdesk"
    mv -f $XSESSION $XSESSION.switchdesk
fi

if [ -e "$XINITRC" ]; then
    echo "warning: moving $XINITRC to $XINITRC.switchdesk"
    mv -f $XINITRC $XINITRC.switchdesk
fi

if [ -e "$HOME/.Xclients" ]; then
    TEST_DIFF=`diff $DATADIR/Xclients.toplevel $XCLIENTS`
    if [ -n "$TEST_DIFF" ]; then
	echo "saving $XCLIENTS as $XCLIENTS.switchdesk"
	mv -f $XCLIENTS $XCLIENTS.switchdesk
	install "$DATADIR/Xclients.toplevel" $XCLIENTS
    fi
else
    # if they have no .Xclients, give them one
    install "$DATADIR/Xclients.toplevel" $XCLIENTS
fi

case $1 in
    gnome|GNOME|Gnome)
    DNAME="GNOME"
    NEW_XCLIENTS="$DATADIR/Xclients.gnome"
    ;;
    kde|KDE|Kde)
    DNAME="KDE"
    NEW_XCLIENTS="$DATADIR/Xclients.kde"
    ;;
    anotherlevel|ANOTHERLEVEL|AnotherLevel)
    DNAME="Another Level"    
    NEW_XCLIENTS="$DATADIR/Xclients.anotherlevel"
    ;;
    *)
    echo "Unknown desktop requested: $1"
    exit 1
    ;;
esac
    
# no need to save per-display .Xclients, no other programs
# could have touched them but us.
install $NEW_XCLIENTS $SUB_XCLIENTS

echo "Desktop now set up to run $DNAME."
echo "For system defaults, remove $XCLIENTS"
