#!/bin/sh

HERE="$(dirname "$(readlink -f "${0}")")"

# Allow the AppImage to be symlinked to e.g., /usr/bin/freaccmd
# or called with ./freac*.AppImage freaccmd ...

if [ ! -z "$APPIMAGE" ] ; then
  BINARY_NAME=$(basename "$ARGV0")
else
  BINARY_NAME=$(basename "$0")
fi
if [ ! -z "$1" ] && [ -x "$HERE/$1" ] && [ ! -d "$HERE/$1" ] ; then
  MAIN="$HERE/$1" ; shift
elif [ -x "$HERE/$BINARY_NAME" ] && [ ! -d "$HERE/$BINARY_NAME" ] ; then
  MAIN="$HERE/$BINARY_NAME"
else
  MAIN="$HERE/freac"
fi

# Use system SSL libraries over our own if available

if ! ld.so --list --library-path "${HERE}" "${HERE}/libsmooth-0.9.so.0" > /dev/null 2> /dev/null; then
  LIBRARY_PATH="${HERE}/openssl"
fi

LD_LIBRARY_PATH="${HERE}:${LIBRARY_PATH}" \
LD_PRELOAD="${HERE}/codecs/ogg.so:${HERE}/codecs/vorbis.so" \
exec "${MAIN}" "$@"
