#!/bin/bash

# To install this script, create a symlink to it from somewhere in your
# path.  Do *not* move the script out of the player directory, since
# it relies on the true location of hxplay to derive the location of the
# player directory

ARG0=$0

if [ "${OSTYPE:0:7}" = "solaris" ] ; then
    # Solaris requires this setup to use POSIX utilities.
    PATH=/usr/xpg4/bin:${PATH}
fi


if [ -d "$HELIX_LIBS" ] ; then
    true
elif [ "$HELIX_LIBS" = "" -a -L ${ARG0} ] ; then
    HELIX_LIBS=$(readlink ${ARG0})
    HELIX_LIBS=${HELIX_LIBS%/*}  # Delete trailing file name.
elif [ "$HELIX_LIBS" = "" -a -f ${ARG0} ] ; then
    HELIX_LIBS=$ARG0
    HELIX_LIBS=${HELIX_LIBS%/*}  # Delete trailing file name.
    HELIX_LIBS=$(cd $HELIX_LIBS; pwd)
elif [ -z "$HELIX_LIBS" ]; then
    echo "\$HELIX_LIBS is not set"
else
    echo "\$HELIX_LIBS points to a directory that does not exist ($HELIX_LIBS)"
fi

export HELIX_LIBS

if [ -n "$HELIX_LIBS" -a -d "$HELIX_LIBS" ]; then
    # See if LD_PRELOAD contains any of the sound server libs. If so, remove them.
    LD_PRELOAD=`echo $LD_PRELOAD | sed -e 's/\([^:]*libesd[^:]*\|[^:]*libarts[^:]*\):\?//g'`
    export LD_PRELOAD

    if [ -n "$LD_PRELOAD" ]; then
        echo "Warning: LD_PRELOAD=\"$LD_PRELOAD\""
    fi

    if [ -z "GTK_MODULES" ]; then
        # Enable accessibility
        export GTK_MODULES="gail:atk-bridge"
    fi

    while /bin/true; do
        # Restart the player if exit code is 10 (player reset)
        $HELIX_LIBS/realplay.bin "$@"
        if [ $? -ne 10 ]; then
           break
        fi
    done
fi

