#! /bin/sh
#    Copyright (C) 2006 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__
	
		# tzconfig preseed
		base-config tzconfig/gmt boolean false
		base-config tzconfig/geographic_area select Asia
		base-config tzconfig/select_zone/Asia select Jerusalem

__END__

		rm -f /etc/localtime
		ln -s /usr/share/zoneinfo/Asia/Jerusalem /etc/localtime
		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

	# FIXME: for some reason the postinst scripts work better from this dir,
	# this is why I copy and delete them :(.
	cp /var/lib/dpkg/info/locales.postinst ./locales.postinst.$$
	sh ./locales.postinst.$$ configure
	rm ./locales.postinst.$$

	echo "Done."
else
	echo "Not installed."
fi

# gdm is using locales to set its default language, you need to reconfigure gdm package
# after you reconfigure locales package
# reconfigure locales
echo "Reconfigure the gdm package..."
if [ -e /var/lib/dpkg/info/gdm.postinst ]; then
	# FIXME: for some reason the postinst scripts work better from this dir,
	# this is why I copy and delete them :(.
	cp /var/lib/dpkg/info/gdm.postinst ./gdm.postinst.$$
	sh ./gdm.postinst.$$ configure
	rm ./gdm.postinst.$$

	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__

	# FIXME: for some reason the postinst scripts work better from this dir,
	# this is why I copy and delete them :(.
	cp /var/lib/dpkg/info/xserver-xorg.postinst ./xserver-xorg.postinst.$$
	sh ./xserver-xorg.postinst.$$ configure
	rm ./xserver-xorg.postinst.$$

	# 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


	# On a Hebrew desktop set panel to rtl view
	# If enabled flag is on make sure desktop is set for english !
	# FIXME: this only work if the user did not set up his desktop, and he use the defaults.
	if [[ $enabled == 1 ]]; then
		# this is for latin interface 
		# we _need_ this to set an enabled system after hebrew install
		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

	else
		# this is for full hebrew desktop
		gconftool-2 --direct --config-source xml:readwrite:/etc/gconf/gconf.xml.defaults \
			--type boolean --set /apps/panel/default_setup/applets/clock/panel_right_stick false
		gconftool-2 --direct --config-source xml:readwrite:/etc/gconf/gconf.xml.defaults \
	 		--type boolean --set /apps/panel/default_setup/applets/mixer/panel_right_stick false
		gconftool-2 --direct --config-source xml:readwrite:/etc/gconf/gconf.xml.defaults \
			--type boolean --set /apps/panel/default_setup/applets/notification/panel_right_stick false
		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 false
		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 true
		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 true
		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 true
		
		gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults \
			--type boolean --set /apps/panel/default_setup/applets/hdate/panel_right_stick false
	fi
fi
echo "Done."

echo

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