#!/bin/bash

# Translations only work with utf8 locales
if ! locale -k charmap|grep -q UTF-8 ; then
	LANG=C
fi

# Gettext internationalization
export TEXTDOMAIN="salix-codecs-installer"
export TEXTDOMAINDIR="/usr/share/locale"
. gettext.sh

SLAPTGET="/usr/sbin/slapt-get"
SPKG="/sbin/spkg"

# The following packages + their dependencies + their suggested packages
# will be installed
CODEC_PKGS=$( cat /usr/share/salix-codecs-installer/codecs-list.txt )

#
# First ask if you really want to do this.
#
# The message string is exactly the same as in the GTK counterpart, so
# if you change something, make sure you change it in both tools.
line1=`gettext 'Some multimedia codecs are patent encumbered and pose distribution problems in certain countries. Therefore, support for all codecs is not included by default in Salix.'`
line2=`gettext 'You will be able to play files encoded with free codecs, but you will not be able to play commercial DVDs or use some proprietary formats without these codecs.'`
line3=`gettext 'Please check the legislation in your country and select to install the codecs only if it is legal for you to do so.'`
line4=`gettext 'Click Next to update the package cache and receive a list of packages about to be installed.'`
MSG=`echo -e "${line1}\n\n${line2}\n\n${line3}\n\n${line4}"`
dialog --title "$( gettext 'Install multimedia codecs?')" \
	--yes-label "`gettext 'Next'`" \
	--no-label "`gettext 'Cancel'`" \
	--yesno "$MSG" \
	0 0
retval=$?
# if cancel is pressed, exit
if [ $retval -ne 0 ]; then
	exit 0
fi

#
# Update the package caches, we need a fresh copy
#
clear
$SLAPTGET --update
retval=$?
# if there's trouble contacting the repos, exit
if [ $retval -ne 0 ]; then
	exit 1
fi

#
# Make a list of all installed packages (package names only). We'll need
# it later to check which of the packages that we were going to install
# are already there
#
installed=$( ls /var/lib/pkgtools/packages/ | sed "s/\(.*\)-\(.*\)-\(.*\)-\(.*\)/\1/" )
installed=" $installed "
installed=$( echo $installed | tr '\n' ' ' )

#
# Make a list of all the packages that are going to be checked for
# installation, including their dependencies and suggestions
#
all_pkgs=" $CODEC_PKGS "
for i in $CODEC_PKGS; do
	output=$( LANG=C $SLAPTGET --show $i | \
		grep "Package Required:\|Package Suggests" | \
		sed "s/Package Required:[ ]*//" | \
		sed "s/Package Suggests:[ ]*//" | \
		sed "s/,/\n/g" | \
		sed "s/ /\n/g")
	all_pkgs="$all_pkgs $output"
done
all_pkgs=$( echo $all_pkgs | sed "s/ /\n/g" | sort | uniq )

#
# See which of the above packages are already installed and leave them
# out
#
not_installed_pkgs=""
for i in $all_pkgs; do
	if [[ $i == *"|"* ]]; then
		found_alternative=0
		for alternative in `echo $i | tr '|' ' '`; do
			if echo $installed | grep " $alternative " 2>&1 >/dev/null; then
				found_alternative=1
				break
			fi
		done
		# if none of the alternatives are found, install the first one
		if [[ $found_alternative == 0 ]]; then
			alt=`echo $i | cut -d'|' -f1`
			not_installed_pkgs="$not_installed_pkgs $alt "
		fi
	else
		if ! echo $installed | grep " $i " 2>&1 >/dev/null; then
			not_installed_pkgs="$not_installed_pkgs $i "
		fi
	fi
done
not_installed_pkgs=`echo $not_installed_pkgs | sed "s/ /\n/g" | sort | \
	uniq | tr '\n' ' '`

#
# If there is nothing to install, inform the user and exit
#
if [[ x$not_installed_pkgs == x"" ]]; then
	MSG=$( gettext "All codec packages are already installed." )
	TITLE=$( gettext "Nothing to install" )
	dialog --title "$TITLE" \
		--msgbox "$MSG" \
		0 0
	exit 0
fi

#
# Create a list of packages to be installed
#
final_list=""
for i in $not_installed_pkgs; do
	desc=$( LANG=C $SLAPTGET --show $i | \
		grep -A1 "Package Description:" | \
		sed "/Package Description:/d" | \
		sed "s/^[ ]*\(.*\)[ ]*$/\1/" )
	final_list="$final_list \"$i\" \"$desc\" on "
done

#
# Present the list of packages to be installed. By default everything is
# selected.
#
MSG=$( gettext "These are the packages that are about to be installed. Please check your country's legislation and select to install only the ones that you are allowed to." )
TITLE=$( gettext "List of packages to install" )
answer="$( eval dialog --stdout \
	--title \"$TITLE\" \
	--checklist \"$MSG\" \
	0 0 15 "$final_list" )"
retval=$?
clear
# if cancel is pressed, exit
if [ $retval -ne 0 ]; then
	exit 0
fi

#
# Install the packages, all in one go
#
for i in $answer; do
	pkgs_to_install="$pkgs_to_install $i "
done
$SLAPTGET --install $pkgs_to_install
retval=$?
# if there's a problem somewhere, stop right there and exit with an
# error
if [ $retval -ne 0 ]; then
	exit 1
fi

#
# If everything went well, ask if the package should be removed
#
MSG=$( gettext 'Codecs installation was succesfully completed. Would you like to remove the codecs installer from your system?' )
dialog --title "$( gettext 'Done!' )" \
	--yesno "$MSG" \
	0 0
retval=$?
if [ $retval -eq 0 ]; then
	$SPKG -d salix-codecs-installer
fi
