#! /bin/sh

# File:        /usr/local/lib/squeak/3.6-3/npsqueakrun
# Author:      Bert Freudenberg <bert@isg.cs.uni-magdeburg.de>
# Description: Script to start the Squeak VM binary 
#              from the Browser Plugin (npsqueak.so)
#              If necessary, sets up $HOME/.npsqueak/* first.

SQ_DIR=/usr/local/lib/squeak
VM_VERSION=3.6-3

IMAGE=${SQ_DIR}/npsqueak.image

VM=${SQ_DIR}/${VM_VERSION}/squeak
VMOPTIONS="-memory 64m -xshm"

ensuredir()
{
  if [ ! -d "${1}" ] ; then
    if mkdir "${1}" ; then
      echo npsqueak: Created ${1}
      if [ -n "${2}" ] ; then
        chmod "${2}" "${1}"
      fi
    else
      echo npsqueak: Could not create ${1}!
    fi
  fi
}

ensurefile()
{
  if [ ! -e "${1}" ] ; then
    if cp "${2}" "${1}" ; then
      echo npsqueak: Created ${1}
    else
      echo npsqueak: Could not create ${1}
      if [ ! -r "${2}" ] ; then
        echo npsqueak: because ${2} is missing
      fi
    fi
  fi
}

ensuredir "${HOME}/.npsqueak"
ensuredir "${HOME}/.npsqueak/secure" 700
ensuredir "${HOME}/.npsqueak/untrusted"
ensurefile "${HOME}/.npsqueak/SqueakPlugin.image" "${IMAGE}"

exec $VM $OPT "$@"
