#! /bin/sh
#
# Copyright (c) 2006, 2008 SuSE
# Author: Stanislav Brabec <sbrabec@suse.cz>
#
# Creates defaults.list from gnome_defaults.conf using only available
# applications and preferring GNOME and GTK applications.
#
# See /etc/gnome_defaults.conf for algorithm description.

shopt -s nullglob

unset DUMMY ${!MIME_*} ${!APP_*} ${!DEFAULT_*} ${!PREFDEFAULT_*} ${!DEFDESKTOP_*}

# Ensure, that $XDG_DATA_DIRS is set properly. (#71978, #240603)
if test "$PROFILEREAD" != true ; then
    . $r/etc/profile
fi

# Read defaults.list and set applications as best candidates.
exec <$r/etc/gnome_defaults.conf
while read ; do
    case "$REPLY" in
	"#"* | "" )
	    ;;
	# Is is default application for particular MIME type?
	*=* )
	    # Strip optional priority record (and ignore it - no GNOME app uses it).
	    MIME=${REPLY%%=*}
	    # Replace special characters.
	    MIMES=${MIME//_/__}
	    MIMES=${MIMES//\//_S}
	    MIMES=${MIMES//-/_M}
	    MIMES=${MIMES//+/_P}
	    MIMES=${MIMES//./_D}
	    eval DEFAULT_${MIMES}\=${REPLY#*=}
	    ;;
	# Is it preferred default application?
	!* )
	    DESKTOP=${REPLY#!}
	    DESKTOPS=${DESKTOP//_/__}
	    DESKTOPS=${DESKTOPS//-/_M}
	    DESKTOPS=${DESKTOPS//+/_P}
	    DESKTOPS=${DESKTOPS//./_D}
	    DESKTOPS=${DESKTOPS// /_S}
	    DESKTOPS=${DESKTOPS//:/_C}
	    eval DEFDESKTOP_$DESKTOPS\=PREFDEFAULT
	    ;;
	# Is it default application?
	* )
	    DESKTOP=${REPLY}
	    DESKTOPS=${DESKTOP//_/__}
	    DESKTOPS=${DESKTOPS//-/_M}
	    DESKTOPS=${DESKTOPS//+/_P}
	    DESKTOPS=${DESKTOPS//./_D}
	    DESKTOPS=${DESKTOPS// /_S}
	    DESKTOPS=${DESKTOPS//:/_C}
	    eval DEFDESKTOP_$DESKTOPS\=DEFAULT
	    ;;
    esac
done
exec <&-

IFS="$IFS:;"
# Browse all desktop files
for DIR in ${XDG_DATA_DIRS:-/usr/local/share:/usr/share} ; do
    for DESKTOP in $r$DIR/applications/*.desktop ; do
	DESKTOPN=${DESKTOP##*/}
	DESKTOPS=${DESKTOPN//_/__}
	DESKTOPS=${DESKTOPS//-/_M}
	DESKTOPS=${DESKTOPS//+/_P}
	DESKTOPS=${DESKTOPS//./_D}
	DESKTOPS=${DESKTOPS// /_S}
	DESKTOPS=${DESKTOPS//:/_C}
	IS_DEFAULT_DESKTOP=false
	eval "if test \"\$DEFDESKTOP_$DESKTOPS\" ; then
	    IS_DEFAULT_DESKTOP=true
	    DEFTYPE=\$DEFDESKTOP_$DESKTOPS
	fi"
	exec <"$DESKTOP"
	CATEGORIES=
	MIMETYPE=
	while read ; do
	    case "$REPLY" in
		Categories=* )
		    CATEGORIES="${REPLY#Categories=}"
		    ;;
		MimeType=* )
		    MIMETYPE="${REPLY#MimeType=}"
		    ;;
	    esac
	done
	exec <&-
	# Is it a GNOME application?
	APPTYPE=GENERIC
	if test "$CATEGORIES" != "${CATEGORIES/GTK}" ; then
	    APPTYPE=GTK
	fi
	if test "$CATEGORIES" != "${CATEGORIES/GNOME}" ; then
	    APPTYPE=GNOME
	fi
	if $IS_DEFAULT_DESKTOP ; then
	    APPTYPE=$DEFTYPE
	fi
	for MIME in $MIMETYPE ; do
	    # Strip optional priority record (and ignore it - no GNOME app uses it).
	    MIME=${MIME%:*}
	    # Replace special characters.
	    MIMES=${MIME//_/__}
	    MIMES=${MIMES//\//_S}
	    MIMES=${MIMES//-/_M}
	    MIMES=${MIMES//+/_P}
	    MIMES=${MIMES//./_D}
	    eval MIME_$MIMES\=$MIME
	    IS_DEFAULT=false
	    eval "if test \"\$DEFAULT_$MIMES\" = \"\$DESKTOPN\" ; then
		IS_DEFAULT=true
	    fi"
	    if $IS_DEFAULT ; then
		eval APP_MIMEDEFAULT_$MIMES\=\"\$DESKTOPN\"
	    else
		eval APP_${APPTYPE}_$MIMES\=\"\$DESKTOPN\"
	    fi
	done
    done
done

exec >$r/var/cache/gio-2.0/defaults.list
echo "# generated by SuSEconfig --module glib2 from /etc/gnome_defaults.conf"
echo "[Default Applications]"

# Browse all offered MIME types and write defaults.list.
for MIMES in ${!MIME_*} ; do
    eval MIME\=\$$MIMES
    MIMES=${MIMES#MIME_}
    # Try GNOME applications first, then GTK, then generic.
    for APPTYPE in MIMEDEFAULT PREFDEFAULT DEFAULT GNOME GTK GENERIC ; do
	eval "if test \"\$APP_${APPTYPE}_$MIMES\" ; then
	    echo \"$MIME=\$APP_${APPTYPE}_$MIMES\" ;
	    break ;
	fi"
    done
done
exec >&-