#!/bin/bash

VERSION=10.3.1
GST_INSTALL_PLUGINS_SUCCESS=0
GST_INSTALL_PLUGINS_NOT_FOUND=1
GST_INSTALL_PLUGINS_ERROR=2
GST_INSTALL_PLUGINS_PARTIAL_SUCCESS=3
GST_INSTALL_PLUGINS_USER_ABORT=4

OFFLINE_URI="file:///usr/share/opensuse-codecs-installer/offline.html#"
MISSING_PLUGINS_URI="http://software.opensuse.org/codecs?"

function check_connection () {
	result=$(dbus-send --print-reply --system --type=method_call \
		--dest=org.freedesktop.NetworkManager \
		/org/freedesktop/NetworkManager \
		org.freedesktop.NetworkManager.state 2> /dev/null)

	if [ $? = 0 ]; then
		status_code=${result:$((${#result}-1)):1}
		if [ "x$status_code" = "x3" ]; then
			connected=1
			return
		fi
	else
		curl --connect-timeout 5 http://opensuse.org &> /dev/null
		if [ $? = 0 ]; then
			connected=1
		fi
	fi
}

function open_webpage () {
	check_connection
	if [ "x$connected" = "x1" ]; then
		xdg-open "${MISSING_PLUGINS_URI}${1}"
	else
		xdg-open "${OFFLINE_URI}${1}"
	fi
}

function detect_desktop () {
	if [ x"$DE" != x"" ]; then return; fi

    if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde;
    elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
    elif xprop -root _DT_SAVE_MODE | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce;
    fi
}

function load_prompt_message () {
	msg_lang=$(echo $LANG | cut -d . -f1)
	for dir in /usr/share/opensuse-codecs-installer .; do
		file="${dir}/prompt.${msg_lang}"
		test -e $file || file="${dir}/prompt.en_US"
		test -e $file || continue
		prompt_title=$(head -n 1 < $file)
		prompt_body=$(head -n 2 < $file | tail -n 1)
		break
	done
}

function prompt () {
	load_prompt_message
	detect_desktop 

	zen=$(which zenity 2>/dev/null)
	if [[ -x $zen && ! "x$zen" = "x" ]]; then
		zen="$zen --question --text "
	else 
		zen=""
	fi

	kd=$(which kdialog 2>/dev/null)
	if [[ -x $kd && ! "x$kd" = "x" ]]; then
		kd="$kd --yesno "
	else
		kd=""
	fi

	case "x$DE" in
		xkde) prompter="$kd" ;;
		*) prompter="$zen" ;;
	esac

	if [ "x$prompter" = "x" ]; then
		if [ ! "x$zen" = "x" ]; then
			prompter="$zen"
		elif [ ! "x$kd" = "x" ]; then
			prompter="$kd"
		fi
	fi

	if [ "x$prompter" = "x" ]; then
		echo "$prompt_body" | \
		sed 's,<br>,\n,g;s,<[\/]*big>,,g;s,<[\/]*b>,,g;s,\. ,\.\n,g' | \
		xmessage -file - -buttons Yes,No -center
		return $(($? - 101))
	fi

	iskd=$(echo "$prompter" | grep kdialog)
	if [ x"$iskd" = x"" ]; then
		message=$(echo "$prompt_body" | sed 's,<br>,\n,g')
	else
		message="$prompt_body"
	fi

	$prompter "$message" --title "$prompt_title"
	return $?
}

plugins=()
plugins_count=0
transient_for=0
run_prompt=yes

for arg in "$@"; do
	if [ ! "${arg:0:2}" = "--" ]; then
		plugins[$plugins_count]=$arg
		let plugins_count++
		continue
	fi

	option=${arg#--}
	value=$(expr "$option" : '.*=\(.*\)')

	case $option in
		transient-for=*) transient_for=$value ;;
		no-prompt) run_prompt=no ;;
	esac
done

os_release="$(head -n 1 < /etc/SuSE-release)"
kernel_version="$(uname -r)"
gstreamer_package=$(rpm -qf `which /usr/bin/gst-inspect 2>/dev/null` 2>/dev/null)
xine_package=$(rpm -qf /usr/lib/xine 2>/dev/null)
query_string="client_version=${VERSION}&lang=${LANG}&os_release=${os_release}&kernel=${kernel_version}&"

if [ ! -z $gstreamer_package ]; then
	query_string="${query_string}gstreamer=${gstreamer_package}&"
fi

if [ ! -z $xine_package ]; then
	query_string="${query_string}xine=${xine_package}&"
fi

for ((i = 0; i < $plugins_count; i++)); do
	# completely URL encode the missing element description
	# and construct the HTTP query string
	query_part=$(echo ${plugins[$i]} | tr -d '\n' | od -t x1 -A n | sed 's/ /%/g;' | tr -d '\n')
	query_part="plugin${i}=${query_part}"
	
	if [ $i -lt $(($plugins_count - 1)) ]; then
		query_part="$query_part&"
	fi

	query_string="${query_string}${query_part}"
done

if [ "x$CODECS_INSTALLER_NO_PROMPT" != x ]; then
	run_prompt=no
fi

if [ "x$run_prompt" = "xyes" ]; then
	prompt
	result=$?
else
	result=255
fi

if [ "x$result" = "x255" ]; then
	open_webpage "${query_string}"
	exit $GST_INSTALL_PLUGINS_PARTIAL_SUCCESS
elif [ "x$result" = "x0" ]; then
	open_webpage "${query_string}"
	exit $GST_INSTALL_PLUGINS_PARTIAL_SUCCESS
else
	exit $GST_INSTALL_PLUGINS_USER_ABORT
fi

