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

### 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 - resolve symlink $0 when invoked from a ROX SendTo or URI handler
# this helps figuring out the $appname for some SendTo items and with rox -U
case "$0" in
  */rox.sourceforge.net/SendTo/*|*/rox.sourceforge.net/URI/*)
    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 "$@"

