#! /bin/sh
#    Copyright (C) 2006,2007 The Debian Hebrew Team                                 
#                       <debian-hebrew-common@lists.alioth.debian.org>
#
#    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 St, Fifth Floor, Boston, MA  02110-1301 USA

set +e

# check for root
if [ `id -u` != 0 ]; then
	echo "$0: you need to be root to run this script"
	exit 1
fi

# Parse arguments
enabled=0
timezone=1

while [ $# -gt 0 ]
do
    case "$1" in
        --enabled)  enabled=1;;
	--notimezone) timezone=0;;
	*)
            echo >&2 "usage: $0 [--enabled] [--notimezone]"
	    exit 1;;
    esac
    shift
done

# reconfigure time zone
echo "Reconfigure the systems time zone..."
if [ -f /usr/share/zoneinfo/Asia/Jerusalem ]; then
	if [[ $timezone == 1 ]]; then
		debconf-set-selections << __END__
		# timezone config preseed
		tzdata tzdata/Areas Asia
		tzdata tzdata/Zones/Asia Jerusalem
__END__

		sh /var/lib/dpkg/info/tzdata.postinst configure
		echo "Done."
	else
		echo "Unchanged."
	fi
else
	echo "Not installed."
fi

# reconfigure locales
echo "Reconfigure the locales package..."
if [ -e /var/lib/dpkg/info/locales.postinst ]; then
	if [[ $enabled == 0 ]]; then
		debconf-set-selections << __END__

		# locales preseed
		locales locales/default_environment_locale string he_IL.UTF-8
		locales locales/locales_to_be_generated multiselect he_IL.UTF-8 UTF-8, en_US.UTF-8 UTF-8, ru_RU.UTF-8 UTF-8, ar_JO.UTF-8 UTF-8, am_ET.UTF-8 UTF-8

__END__
	else
		debconf-set-selections << __END__

		# locales preseed for enabled system
		locales locales/default_environment_locale string en_US.UTF-8
		locales locales/locales_to_be_generated multiselect he_IL.UTF-8 UTF-8, en_US.UTF-8 UTF-8, ru_RU.UTF-8 UTF-8, ar_JO.UTF-8 UTF-8, am_ET.UTF-8 UTF-8

__END__
	fi

	sh /var/lib/dpkg/info/locales.postinst configure
	echo "Done."
else
	echo "Not installed."
fi

# reconfigure xserver-xorg
echo "Reconfigure xorg X server..."
if [ -e /var/lib/dpkg/info/xserver-xorg.postinst ]; then
	debconf-set-selections << __END__

	# xserver-xorg preseed
	xserver-xorg xserver-xorg/config/inputdevice/keyboard/layout string us,il
	xserver-xorg xserver-xorg/config/inputdevice/keyboard/variant string lyx
	xserver-xorg xserver-xorg/config/inputdevice/keyboard/options string grp:switch,grp:alt_shift_toggle

__END__

	sh /var/lib/dpkg/info/xserver-xorg.postinst configure

	# Set keyboard for current session
	setxkbmap -option grp:switch,grp:alt_shift_toggle -layout us,il -variant lyx
	echo "Done."
else
	echo "Not installed."
fi

# Configure defaults
echo "Configure Debian and Gnome defaults ..."
if [ -x /usr/bin/gconftool-2 ]; then
	# Add kbd and hdate applets
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type list --set /apps/panel/default_setup/general/applet_id_list --list-type \
		string "[window_menu,mixer,clock,show_desktop_button,window_list,workspace_switcher,notification,hdate,kbd]"

	# add hdate to panel
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type string --set /apps/panel/default_setup/applets/hdate/action_type "lock"
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type string --set /apps/panel/default_setup/applets/hdate/attached_toplevel_id ""
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type string --set /apps/panel/default_setup/applets/hdate/bonobo_iid "OAFIID:HDateApplet"
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type string --set /apps/panel/default_setup/applets/hdate/custom_icon ""
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type string --set /apps/panel/default_setup/applets/hdate/launcher_location ""
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type boolean --set /apps/panel/default_setup/applets/hdate/locked true
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type string --set /apps/panel/default_setup/applets/hdate/menu_path ""
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type string --set /apps/panel/default_setup/applets/hdate/launcher_location ""
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type string --set /apps/panel/default_setup/applets/hdate/object_type "bonobo-applet"
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type boolean --set /apps/panel/default_setup/applets/hdate/panel_right_stick true
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type integer --set /apps/panel/default_setup/applets/hdate/position 3

	# make room for hdate by moving notification
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type integer --set /apps/panel/default_setup/applets/notification/position 4
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type string --set /apps/panel/default_setup/applets/hdate/tooltip ""
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type string --set /apps/panel/default_setup/applets/hdate/toplevel_id "top_panel"
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type boolean --set /apps/panel/default_setup/applets/hdate/use_custom_icon false
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type boolean --set /apps/panel/default_setup/applets/hdate/use_menu_path false
	
	# add kbd to panel
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type string --set /apps/panel/default_setup/applets/kbd/action_type "lock"
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type string --set /apps/panel/default_setup/applets/kbd/attached_toplevel_id ""
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type string --set /apps/panel/default_setup/applets/kbd/bonobo_iid "OAFIID:GNOME_KeyboardApplet"
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type string --set /apps/panel/default_setup/applets/kbd/custom_icon ""
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type string --set /apps/panel/default_setup/applets/kbd/launcher_location ""
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type boolean --set /apps/panel/default_setup/applets/kbd/locked true
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type string --set /apps/panel/default_setup/applets/kbd/menu_path ""
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type string --set /apps/panel/default_setup/applets/kbd/launcher_location ""
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type string --set /apps/panel/default_setup/applets/kbd/object_type "bonobo-applet"
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type boolean --set /apps/panel/default_setup/applets/kbd/panel_right_stick true
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type integer --set /apps/panel/default_setup/applets/kbd/position 3
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type string --set /apps/panel/default_setup/applets/kbd/tooltip ""
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type string --set /apps/panel/default_setup/applets/kbd/toplevel_id "bottom_panel"
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type boolean --set /apps/panel/default_setup/applets/kbd/use_custom_icon false
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type boolean --set /apps/panel/default_setup/applets/kbd/use_menu_path false

	# gnome-panel >= 2.18.x automatically set the panel direction to RTL for RTL locales.
	# we do not need to set the direction manually 
	# FIXME: do we need this to re-set the system after an old hebrew-settings run ?

	# start of manual direction re-set
	gconftool-2 --direct --config-source xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type boolean --set /apps/panel/default_setup/applets/clock/panel_right_stick true
	gconftool-2 --direct --config-source xml:readwrite:/etc/gconf/gconf.xml.defaults \
	 	--type boolean --set /apps/panel/default_setup/applets/mixer/panel_right_stick true
	gconftool-2 --direct --config-source xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type boolean --set /apps/panel/default_setup/applets/notification/panel_right_stick true
	gconftool-2 --direct --config-source xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type boolean --set /apps/panel/default_setup/applets/window_menu/panel_right_stick true
	gconftool-2 --direct --config-source xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type boolean --set /apps/panel/default_setup/objects/menu_bar/panel_right_stick false
	gconftool-2 --direct --config-source xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type boolean --set /apps/panel/default_setup/objects/nautilus_launcher/panel_right_stick false
	gconftool-2 --direct --config-source xml:readwrite:/etc/gconf/gconf.xml.defaults \
		--type boolean --set /apps/panel/default_setup/objects/terminal_launcher/panel_right_stick false
	gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
			--type boolean --set /apps/panel/default_setup/applets/hdate/panel_right_stick true
	# end of manual direction re-set

fi
echo "Done."

echo

echo "Everything is configured,"
echo "Settings will take effect from the next login."
exit 0
