#! /bin/bash
#
# This AppRun is included in the latest versions of Videomass*.AppImage.
# During the creation of the appimage, it will be copied to AppDir, replacing
# the existing one.
# Always make sure that there are no major changes in the original AppRun first.
# Updated to python-appimage AppRun on November 25 2022.
#
# if you plan to install extra packages, extract the AppImage, e.g. as:
#
#   ./Videomass*.AppImage --appimage-extract
#   export PATH="$(pwd)/squashfs-root/usr/bin:$PATH"
#---------------------------------------------------------------------------

# If running from an extracted image, then export ARGV0 and APPDIR
if [ -z "${APPIMAGE}" ]; then
    export ARGV0="$0"

    self=$(readlink -f -- "$0") # Protect spaces (issue 55)
    here="${self%/*}"
    tmp="${here%/*}"
    export APPDIR="${tmp%/*}"
fi

# Resolve the calling command (preserving symbolic links).
export APPIMAGE_COMMAND=$(command -v -- "$ARGV0")

# Export TCl/Tk
export TCL_LIBRARY="${APPDIR}/usr/share/tcltk/tcl8.4"
export TK_LIBRARY="${APPDIR}/usr/share/tcltk/tk8.4"
export TKPATH="${TK_LIBRARY}"

# Export SSL certificate
export SSL_CERT_FILE="${APPDIR}/opt/_internal/certs.pem"

# required shared libraries to run Videomass
LIBSDL=$(/sbin/ldconfig -p | grep libSDL2.2.0 | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
if [ -z "$LIBSDL" ]; then  # if empty
    echo "export ${APPDIR}/x86_64-linux-gnu/"
    export LD_LIBRARY_PATH="${APPDIR}/x86_64-linux-gnu/":$LD_LIBRARY_PATH
fi

export LD_LIBRARY_PATH="${APPDIR}/usr/lib/x86_64-linux-gnu/":$LD_LIBRARY_PATH
if [ -d "${APPDIR}/lib/x86_64-linux-gnu" ]; then
    export LD_LIBRARY_PATH="${APPDIR}/lib/x86_64-linux-gnu/":$LD_LIBRARY_PATH
fi

# Call Python
"$APPDIR/opt/python3.9/bin/python3.9" "$APPDIR/usr/bin/videomass" "$@"
