#!/bin/bash

# Translations only work with utf8 locales
if [ ! `echo $LANG | grep -i utf` ]; 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="gstreamer gst-plugins-base gst-plugins-good gst-plugins-bad gst-plugins-ugly gst-python gst-ffmpeg gstreamer1 gst-plugins-base1 gst-plugins-good1 gst-plugins-bad1 gst-plugins-ugly1 gst-libav libdvdcss flash-plugin"

#
# First ask if you really want to do this.
#
# The message string is a bit ugly, but it has to be this way so that it
# matches the same string in the GTK counterpart application. That way
# we can use the same translated strings
MSG="$( eval_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.

You will be able to play files encoded with free codecs, but you will not be able to play commercial DVDs or listen to mp3 files without these codecs.

Please check the legislation in your country and select to install the codecs only if it is legal for you to do so.

Click Next to update the package cache and receive a list of packages about to be installed.')"
dialog --title "$( eval_gettext 'Install multimedia codecs?')" \
	--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/log/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" | \
		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 echo $installed | grep " $i " 2>&1 >/dev/null; then
		sleep 0
	else
		not_installed_pkgs="$not_installed_pkgs $i "
	fi
done

#
# If there is nothing to install, inform the user and exit
#
if [[ x$not_installed_pkgs == x"" ]]; then
	MSG=$( eval_gettext "All codec packages are already installed." )
	TITLE=$( eval_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=$( eval_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=$( eval_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=$( eval_gettext 'Codecs installation was succesfully completed. Would you like to remove the codecs installer from your system?' )
dialog --title "$( eval_gettext 'Done!' )" \
	--yesno "$MSG" \
	0 0
retval=$?
if [ $retval -eq 0 ]; then
	$SPKG -d salix-codecs-installer
fi
