#!/bin/sh
# Author: Bert Freudenberg
# Purpose: Run etoys using the old Sugar launch procedure which comes
#          pre-installed on the first mass-produced machines (Trial-3/Ship.1)
#          The new Sugar (Joyride/Update.1) uses bin/etoys-activity instead.
#
# This script is executed from sugar-native-factory
# in response to a create() DBus call. The arguments 
# to that call are passed verbatim on the command line.

echo "$0" "$@"

# arguments are unordered, have to loop
args=""
while [ -n "$2" ] ; do
    args="$args $1 $2"
    case "$1" in
      	bundle_id)   bundle_id="$2" ;;
      	activity_id) activity_id="$2" ;;
      	pservice_id) pservice_id="$2" ;;
      	object_id)   object_id="$2" ;;
	uri)         uri="$2" ;;
	*) echo unknown argument $1 $2 ;;
    esac
    shift;shift
done

# really need bundle id and activity id
if [ -z "$bundle_id" -o -z "$activity_id" ] ; then
  echo ERROR: bundle_id and activity_id arguments required
  echo Aborting
  exit 1
fi

# some debug output
echo launching $bundle_id instance $activity_id
[ -n "$pservice_id" ] && echo shared as $pservice_id
[ -n "$object_id"   ] && echo with journal obj $object_id
[ -n "$uri"         ] && echo loading uri $uri

# sanitize
[ -z "$SUGAR_PROFILE" ] && SUGAR_PROFILE=default
[ -z "$SUGAR_ACTIVITY_ROOT" ] && SUGAR_ACTIVITY_ROOT="$HOME/.sugar/$SUGAR_PROFILE/etoys"

export SQUEAK_SECUREDIR="$SUGAR_ACTIVITY_ROOT/data/private"
export SQUEAK_USERDIR="$SUGAR_ACTIVITY_ROOT/data/sandbox"

# now run Squeak VM with Etoys image
exec etoys \
    -sugarBundleId $bundle_id \
    -sugarActivityId $activity_id \
    $args
    
