#!/usr/bin/bash
if [ $UID != 0 ]; then
	su - -c "$0 S@"
	exit $?
fi

foxfound=""
libdir="$(rpm -E \%{_libdir})"

function Install
{
	Mensaje2="Protocolo afirma instalado en firefox"
	local AllowedPaths
	if [ $# = 0 ]; then
		AllowedPaths="$libdir/firefox $libdir/quantum $libdir/firefox-release /opt/firefox"
	else
		AllowedPaths="$1"
	fi
	for ActualPath in $AllowedPaths; do
		if  [ -z "$foxfound" ] ; then
			if [ "$ActualPath" = "$libdir/firefox" ] && [ -f "$ActualPath/browser/defaults/preferences/mga.js" ]; then
				foxfound="$Mensaje2 de Mageia en $ActualPath/browser/defaults/preferences"
				if ! ln -sf /usr/share/autofirma/autofirma.js $ActualPath/browser/defaults/preferences/autofirma.js ; then
					return 2
				fi
			else
				[ "$ActualPath" = "$libdir/quantum" ] && [ ! -x  /usr/bin/firefox-quantum ] && continue
				if  [ -x "$ActualPath/firefox" ]; then 
					foxfound="$Mensaje2 de Mozilla en $ActualPath/defaults/pref"
					if ! ln -sf /usr/share/autofirma/autofirma.js $ActualPath/defaults/pref/autofirma.js; then
						return 2
					fi
				fi
			fi
		else
			break
		fi
	done
	test -n "$foxfound"
	return $?
}

function Dialogo
{
	local tipo=$1
	local texto=$2
	local Topcion
	if [ -z "$DISPLAY" ]; then
		echo ; echo $texto ; echo
	else
		case $tipo in
			error*) Topcion="--error";;
			info) Topcion="--info";;
		esac
		zenity $Topcion --text="$texto"
	fi
	[ "$tipo" = "error-fatal" ] && exit 1
	return 0
}

Error1="No se encontro firefox en lo sitios soportados:
	$libdir/firefox
	$libdir/quantum
	$libdir/firefox-release
	/opt/firefox
	
	No se instalo el protocolo afirma, instale firefox en alguno de los sitios soportados"
Error2="No se pudo instalar protocolo afirma en"
Error3="Debe seleccionar una opción"
Mensaje1="Seleccione la ruta donde Firefox esta instalado"
Mensaje3="Protocolo afirma desinstalado"
if [ $# -gt 0 ]; then
	case "$1" in
	"-install")
	if ! Install; then
		Dialogo error-fatal $Error1
	fi ;;
	"-uninstall")
		[ -L "$libdir/firefox/browser/defaults/preferences/autofirma.js" ] && \
		 rm -f "$libdir/firefox/browser/defaults/preferences/autofirma.js"
		[ -L "$libdir/firefox/defaults/pref/autofirma.js" ] && rm -f "$libdir/firefox/defaults/pref/autofirma.js"
		[ -L "$libdir/quantum/defaults/pref/autofirma.js" ] && rm -f "$libdir/quantum/defaults/pref/autofirma.js"
		[ -L "$libdir/firefox-release/defaults/pref/autofirma.js" ] && rm -f "$libdir/firefox-release/defaults/pref/autofirma.js"
		[ -L "/opt/firefox/defaults/pref/autofirma.js" ] && rm -f "/opt/firefox/defaults/pref/autofirma.js" 
		Dialogo info "$Mensaje3"
		exit 0;;
	esac
else
	if [ -z "$DISPLAY" ]; then
		echo "$Mensaje1"
		select foxpath in $AllowedPaths; do
			if [ -n "$foxpath" ]; then
				if ! Install $foxpath ; then
					Dialogo error-fatal "$Error2 $foxpath" 
				else
					Dialogo info "$foxfound"
					break
				fi
			else
				Dialogo error "$Error3"
				echo "$Mensaje1"
				continue
			fi
		done
	else
		while ! foxpath=$(zenity --list --text="$Mensaje1" \
			--radiolist --column=" " --column="Ruta" FALSE $libdir/firefox FALSE $libdir/firefox-release FALSE $libdir/quantum FALSE /opt/firefox) ; do
			Dialogo error "$Error3"
		done
		if ! Install $foxpath ; then
			Dialogo error-fatal "$Error2 en $foxpath"
		fi
	fi
fi
if [ -n "$foxfound" ]; then
	Dialogo info "$foxfound"
fi
