#!/bin/dash
# Launch a default program
# Copyright (C) James Budiono, 2014
#
# Usage: this program should be symlinked to defaultpaint, defaultbrowser, etc
#
# 2016-03-05 step - fix handling symlink from Rox-Filter SendTo
# 

### configuration ###
SYSTEM_DEFAULTS=/etc/defaultprograms
LOCAL_DEFAULTS=$FATDOG_STATE_DIR/defaultprograms
[ -e $SYSTEM_DEFAULTS ] && . $SYSTEM_DEFAULTS # load system's first
[ -e $LOCAL_DEFAULTS ] && . $LOCAL_DEFAULTS   # override with local settings

### main ###
# find the program name and the registry name
# step - special handling from those invoked from SendTo
case "$0" in
  */SendTo/*) # find out which default program invoked us from ROX-Filer
    progname="$(readlink "$0")"
    ;;
  *) # assume single symlinking scheme
    progname="$0"
    ;;
esac
progname=${progname##*/} 
appname=${progname#default} # make sure not clash with real environment names
registryname=DEF_$(echo "$appname" | tr 'a-z' 'A-Z')

# and launch the program if it is not empty
eval cmd=\$$registryname
[ -e $FATDOG_STATE_DIR/language ] && read LANG < $FATDOG_STATE_DIR/language # L18L
[ "$cmd" ] && exec $cmd "$@"
