#!/bin/sh
# 
# Launch squeakvm from the command line or a menu script, with a good
# plugin path, text encodings and pulseaudio kludge
# 
# Last edited: 2009-08-27 22:25:34 by piumarta on emilia-2.local

PATH=/usr/bin:/bin

me=`basename $0`
bindir=`dirname $0`
bindir=`cd ${bindir}; pwd`
prefix=`dirname ${bindir}`
libdir="${prefix}/lib/squeak"
plgdir="${libdir}/3.11.3-2135"
wrapper=""
vm="squeakvm"
plugins=""
sound=""
pathenc="-pathenc UTF-8"
encoding="-encoding UTF-8"

# find the vm and set the plugin path

if test -x "${plgdir}/${vm}"; then	# bin/squeak -> lib/squeak/x.y-z/squeakvm
    vm="${plgdir}/${vm}"
    plugins="${plgdir}"
elif test -x "${bindir}/${vm}"; then	# bld/squeak -> bld/squeakvm
    vm="${bindir}/${vm}"
    plugins="${bindir}/%n"
elif test -x "`which ${vm}`"; then
    vm="`which ${vm}`"
    plugins=""
else    
    error "cannot find ${vm}"
    exit 1
fi

# deal with pulseaudio if it is running

if pulseaudio --check 2>/dev/null; then
    sound="-vm-sound-oss"
    if padsp true 2>/dev/null; then
        wrapper=padsp
    fi
fi

# we should not interfere with the user's command-line options

for opt in $*; do
    case ${opt} in
	-plugins)	plugins="";;
	-pathenc)	pathenc="";;
	-encoding)	encoding="";;
	-vm-sound*)	sound=""; wrapper="";;
	-vm)		case "$2" in sound*) sound=""; wrapper=""; esac;;
    esac
done

# we should not interfere with the user's environment variables

if test -n "${SQUEAK_PLUGINS}"; then
    plugins=""
fi

if test -n "${SQUEAK_PATHENC}"; then
    pathenc=""
fi

if test -n "${SQUEAK_ENCODING}"; then
    encoding=""
fi

options="-mmap 1024 -vm-display-x11 -swapbtn"

if test -z "${plugins}"; then
    set -x
    exec ${wrapper} "${vm}" ${options} ${pathenc} ${encoding} ${sound} "$@"
else
    set -x
    exec ${wrapper} "${vm}" ${options} ${pathenc} ${encoding} -plugins "${plugins}" ${sound} "$@"
fi
