#!/bin/sh

WD="$(dirname "$0")"
mkdir --parent "$HOME/.sjphone" || exit 1

export SJPHONE_CONFIG_PATH="$HOME/.sjphone"
export SJPHONE_DATA_PATH="$WD/lib"
export SJPHONE_LIB_PATH="$WD/lib"
export SJPHONE_SUGGESTED_PROFILE=C0DFB9E1-79A0-4749-844F-18EC3DE3629D
export SJPHONERC_DEFAULT="$WD/lib/defaults.ini"
export SJPHONE_ID=sjlabs

if [ -z "$SJPHONE_BROWSER_CMD" ]; then
  if which firefox &>/dev/null; then
    export SJPHONE_BROWSER_CMD="firefox -remote 'openURL(%URL,new-window)' &>/dev/null || firefox %URL"
  elif which mozilla &>/dev/null; then
    export SJPHONE_BROWSER_CMD="mozilla -remote 'openURL(%URL,new-window)' &>/dev/null || mozilla %URL"
  elif which opera &>/dev/null; then
    export SJPHONE_BROWSER_CMD="opera -newwindow %URL"
  elif which netscape &>/dev/null; then
    export SJPHONE_BROWSER_CMD="netscape -remote 'openURL(%URL,new-window)' &>/dev/null || netscape %URL"
  fi
fi

#echo Browser command: "$SJPHONE_BROWSER_CMD"

if [ ! -x "$SJPHONE_LIB_PATH/sjphone" ]; then
  echo "Run me from the distribution directory"
  exit 1
fi

ldd -v "$SJPHONE_LIB_PATH"/sjphone > $SJPHONE_CONFIG_PATH/.ldd.out
exec "$SJPHONE_LIB_PATH"/sjphone $@
