#!/bin/bash

###################################################################################
# UCK - Ubuntu Customization Kit                                                  #
# Copyright (C) 2006-2009 UCK Team                                                #
#                                                                                 #
# UCK 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 3 of the License, or               #
# (at your option) any later version.                                             #
#                                                                                 #
# UCK 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 UCK.  If not, see <http://www.gnu.org/licenses/>.                    #
###################################################################################

function failure()
{
	echo "$@"
	exit 1
}

function prepare_install()
{
	#try 2 times to avoid slow proxies failures
	apt-get update || apt-get update || failure "apt-get update failed, error=$?"
}

function install_packages()
{
	apt-get install --assume-yes --force-yes "$@" || failure "apt-get install $@ failed, error=$?"
}

function remove_packages()
{
	apt-get --purge remove --assume-yes --force-yes "$@" || failure "apt-get remove $@ failed, error=$?"
}

function run_package_manager()
{
	echo "Starting package application..."
	
	PACKAGE_APP=`which adept`
	PACKAGE_APP_OPTIONS=(--caption "UCK Package Manager")
	if [ "$PACKAGE_APP" = "" ]; then
		PACKAGE_APP=`which adept_manager`
	fi
	if [ "$PACKAGE_APP" = "" ]; then
		PACKAGE_APP=`which synaptic`
		PACKAGE_APP_OPTIONS=(-t "UCK Package manager")
	fi
	
	if [ "$PACKAGE_APP" = "" ]; then
		dialog_msgbox "Failure" "Unable to find any package manager application"
	else
		$PACKAGE_APP "${PACKAGE_APP_OPTIONS[@]}"
		RESULT=$?
	
		if [ $RESULT -ne 0 ]; then
			dialog_msgbox "Failure" "Running package application $PACKAGE_APP failed, error=$RESULT"
		fi
	fi
}

function run_console()
{
	echo "Starting console application..."
	
	CONSOLE_APP=`which konsole`
	CONSOLE_APP_OPTIONS=(--caption "UCK customization console" -e /bin/bash)
	if [ "$CONSOLE_APP" = "" ]; then
		CONSOLE_APP=`which gnome-terminal`
		CONSOLE_APP_OPTIONS=(-t "UCK customization console" -e /bin/bash)
	fi
	if [ "$CONSOLE_APP" = "" ]; then
		CONSOLE_APP=`which xfce4-terminal`
		CONSOLE_APP_OPTIONS=(-t "UCK customization console" -e /bin/bash)
	fi
	if [ "$CONSOLE_APP" = "" ]; then
		CONSOLE_APP=`which xterm`
		CONSOLE_APP_OPTIONS=(-title "UCK customization console" -e /bin/bash)
	fi
	
	if [ "$CONSOLE_APP" = "" ]; then
		dialog_msgbox "Failure" "Unable to find any console application"
	else
		$CONSOLE_APP "${CONSOLE_APP_OPTIONS[@]}"
		RESULT=$?
	
		if [ $RESULT -ne 0 ]; then
			dialog_msgbox "Failure" "Running console application $CONSOLE_APP failed, error=$RESULT"
		fi
	fi
}

SCRIPT_DIR=`dirname "$0"`
. "$SCRIPT_DIR/gui.sh"

LIVECD_LANGS=`cat "$SCRIPT_DIR/language_packs"`
RUN_MANUAL_CUSTOMIZATIONS=`cat "$SCRIPT_DIR/run_manual_customizations"`
DESKTOP_TYPE=`cat "$SCRIPT_DIR/desktop_type"`

if [ -z "$LIVECD_LANGS" ]; then
	failure "Live CD language not set, please put language packs codes (for example: en, pl, fr) into $SCRIPT_DIR/language_packs"
fi

DESKTOP_FLAVOURS=`cat "$SCRIPT_DIR/desktop_types"`

prepare_install || failure "Preparing installation failed, error=$?"

echo "Installing language packs ($LIVECD_LANGS)..."

PACKAGES_TO_INSTALL=""
LANGPACKS_CONCATENATED=""

for LANGPACK in $LIVECD_LANGS; do
	PACKAGES_TO_INSTALL="$PACKAGES_TO_INSTALL language-pack-$LANGPACK language-support-$LANGPACK"

	if [ "$DESKTOP_FLAVOURS" ]; then
		for FLAVOUR in $DESKTOP_FLAVOURS; do
			if [ $FLAVOUR == "gnome" ] || [ $FLAVOUR == "kde" ]; then
				PACKAGES_TO_INSTALL="$PACKAGES_TO_INSTALL language-pack-$FLAVOUR-$LANGPACK"
			fi
		done
	fi

	if [ -z "$LANGPACKS_CONCATENATED" ]; then
		LANGPACKS_CONCATENATED="$LANGPACK"
	else
		LANGPACKS_CONCATENATED="$LANGPACKS_CONCATENATED|$LANGPACK"
	fi
done

install_packages $PACKAGES_TO_INSTALL || failure "Installing language packs failed, error=$?"

#NOTE: we first install language pack, then remove others as installing language pack might pull packages
#which were not previously present
echo "Removing unnecessary language packages..."
REMOVED_PACKAGES=`dpkg-query --show | cut -f1 | grep -E '^(language-pack|language-support)' | grep -v -E "[-]($LANGPACKS_CONCATENATED)\>"`
remove_packages $REMOVED_PACKAGES || failure "Removing packages failed, error=$?"

if [ "$RUN_MANUAL_CUSTOMIZATIONS" = "yes" ] ; then
	#Create DBUS uuid so that KDE4 apps work.
	echo "Creating DBUS uuid"
	dbus-uuidgen --ensure
	while true ; do
		CHOICE_PACKAGE_MANAGER="Run package manager"
		CHOICE_CONSOLE="Run console application"
		CHOICE_EXIT="Continue building"
		CHOICE=`dialog_menu "Please choose customization action" "$CHOICE_PACKAGE_MANAGER" "$CHOICE_CONSOLE" "$CHOICE_EXIT"`
		RESULT=$?

		if [ $RESULT -ne 0 ] ; then
			failure "Script cancelled by user"
		fi
		#workaround for KDE bug (https://bugs.kde.org/show_bug.cgi?id=139025)
		CHOICE=`echo "$CHOICE" | grep -v -i kwrited | tail -n1`

		echo "CHOICE='$CHOICE'"

		if [ "$CHOICE" = "$CHOICE_EXIT" ] ; then
			break
		elif [ "$CHOICE" = "$CHOICE_PACKAGE_MANAGER" ] ; then
			run_package_manager
		elif [ "$CHOICE" = "$CHOICE_CONSOLE" ] ; then
			run_console
		fi
	done
fi

echo "Done"