#!/bin/bash

# Get the directory where this script is located
HERE="$(dirname "$(readlink -f "${0}")")"
export VOICEVIBE_LAUNCH_ENV_VERSION=1
export VOICEVIBE_LAUNCH_ENV_PACKAGE=deb
export VOICEVIBE_LAUNCH_ENV_ROOT="${HERE}"
if [ "${LD_LIBRARY_PATH+x}" = x ]; then
    export VOICEVIBE_LAUNCH_ENV_LD_LIBRARY_PATH_STATE=set
    export VOICEVIBE_LAUNCH_ENV_LD_LIBRARY_PATH_VALUE="${LD_LIBRARY_PATH}"
else
    export VOICEVIBE_LAUNCH_ENV_LD_LIBRARY_PATH_STATE=unset
    unset VOICEVIBE_LAUNCH_ENV_LD_LIBRARY_PATH_VALUE
fi
export LD_LIBRARY_PATH="${HERE}${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"

# Basic environment variables
export DBUS_SESSION_BUS_ADDRESS="${DBUS_SESSION_BUS_ADDRESS:-unix:path=/run/user/$(id -u)/bus}"
export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}"

# Set working directory to the app directory for proper resource loading
cd "${HERE}"

# Debug output to help diagnose resource issues
if [ "$1" = "--debug" ]; then
    echo "VoiceVibe debug info:"
    echo "Working directory: $(pwd)"
    echo "Script location: ${HERE}"
    echo "Directory contents: $(ls -la)"
    echo "Icon directory contents: $(ls -la icons 2>/dev/null || echo 'Not found')"
    echo "QML directory contents: $(ls -la qml 2>/dev/null || echo 'Not found')"
    echo "Bundled tools: $(ls -la bundled_tools 2>/dev/null || echo 'Not found')"
    echo "PATH: $PATH"
    exit 0
fi

# Run the binary with all arguments passed to this script
exec "${HERE}/voicevibe.bin" "$@"
