#!/bin/sh

# Autostart script
#
# I've tried to make it compatible with the specification, but YMMV
#
# Start autostart programs for the following environments:
# WB_AUTOSTART_ENVIRONMENT= # No environment
# WB_AUTOSTART_ENVIRONMENT=all # All environments
# WB_AUTOSTART_ENVIRONMENT=GNOME:KDE # GNOME and KDE only

# Using the old set hack rather than ksh arrays, which some shells don't
# implement correctly or at all
set --

oldifs=$IFS
IFS=:
for d in ${XDG_CONFIG_HOME:-~/.config}:${XDG_CONFIG_DIRS:-${sysconfdir:-/etc}/xdg};
do
    for f in $d/autostart/*.desktop;
    do
        # Skip this entry if this file doesn't exist (e.g. if there are no
        # desktop files in that directory)
        if test ! -f "$f";
        then
            continue
        # Hidden key; must not be shown
        elif cat "$f" | grep -q "^Hidden\s*=\s*true$";
        then
            show_in=0
        # Value of all: run all programs
        elif test "$WB_AUTOSTART_ENVIRONMENT" = "all";
        then
            show_in=1
        # No OnlyShowIn or NotShowIn key, so show regardless of the value of
        # WB_AUTOSTART_ENVIRONMENT
        elif ! (cat "$f" | grep -q '^\(Only\|Not\)ShowIn\s*=\s*');
        then
            show_in=1
        # No WB_AUTOSTART_ENVIRONMENT specified, so don't run any programs that
        # specify a special environment
        elif test -z "$WB_AUTOSTART_ENVIRONMENT";
        then
            cat "$f" | grep -q '^OnlyShowIn\s*=\s*' && show_in=0
        else
            # Run programs only in the specified autostart environments
            IFS=:
            show_in=0
            for autostart_environment in $WB_AUTOSTART_ENVIRONMENT;
            do
                IFS=\;
                ONLY_SHOW_IN=$(cat "$f" | grep '^OnlyShowIn\s*=\s*' | sed -e 's/^OnlyShowIn\s*=\s*//g')
                for only_show_in in $ONLY_SHOW_IN;
                do
                    if test "$only_show_in" = "$autostart_environment";
                    then
                        show_in=1
                        break 2
                    fi
                done
                # But don't show in these environments
                NOT_SHOW_IN=$(cat "$f" | grep '^NotShowIn\s*=\s*' | sed -e 's/^NotShowIn\s*=\s*//g')
                for not_show_in in $NOT_SHOW_IN;
                do
                    if test "$not_show_in" = "$autostart_environment";
                    then
                        show_in=0
                        break 2
                    fi
                done
            done
        fi

        fn=$(basename "$f")
        # Don't use if a desktop entry with the same name has already been used 
        for df in "$@";
        do
            if test "$df" = "$fn";
            then
                show_in=0
            fi
        done
        # Must be set back before executing the program
        IFS=$oldifs

        set -- $@ "$fn"
        if test $show_in -ne 0;
        then
            # Don't run the Exec key if a non-empty TryExec command can't be found
            TRY_EXEC=$(cat "$f" | grep '^TryExec\s*=\s*\S' | sed -e 's/^TryExec\s*=\s*//g');
            if test -n "$TRY_EXEC" && ! which $TRY_EXEC;
            then
                continue
            fi
            $(cat "$f" | grep '^Exec\s*=\s*' | sed -e 's/^Exec\s*=\s*//g') &
        fi
    done
done
