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

### configuration ###
TEMPLATE_DEFAULTS=/etc/defaultprograms.template
SYSTEM_DEFAULTS=/etc/defaultprograms
USER_DEFAULTS=$FATDOG_STATE_DIR/defaultprograms
[ -e $TEMPLATE_DEFAULTS ] && . $TEMPLATE_DEFAULTS # load system template first
[ -e $SYSTEM_DEFAULTS ] && . $SYSTEM_DEFAULTS     # override with system settings
[ -e $USER_DEFAULTS ] && . $USER_DEFAULTS       # override with pe-user 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 "$@"
