#!/bin/bash

# This is Linux-specific startup script for a bundled version of Ardour

CA=0

while [ $# -gt 0 ] ; 
do
	case "$1" in

	--debug | -D )
		DEBUG="T";
		shift ;;
		
	* )
		if [[ "$1" =~ " " ]];then
		    IEND="$1"
		else
		  CA=$(($CA+1))
		  [ -z "$2" ] && AEND="$1" || A[$CA]="$1"
		fi
		shift; ;;
	esac
done


# LD_LIBRARY_PATH needs to be set here so that epa can swap between the original and the bundled version
# (the original one will be stored in PREBUNDLE_ENV)
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
export PREBUNDLE_ENV="$(env)"

BIN_DIR=/opt/ardour3/bin
INSTALL_DIR=$(dirname $BIN_DIR)
LIB_DIR=$INSTALL_DIR/lib
ETC_DIR=$INSTALL_DIR/etc
USER_ARDOUR_DIR=$HOME/.config/ardour3

if [ ! -d $USER_ARDOUR_DIR ] ; then
    mkdir -p $USER_ARDOUR_DIR || exit 1
fi

# this triggers code in main() that will reset runtime environment variables
# to point to directories inside the ardour package

export ARDOUR_BUNDLED=true

# edited

export PATH=/opt/ardour3/bin:$PATH
export LXVST_PATH="$VST_PATH"
cp /opt/ardour3/etc/gdk-pixbuf.loaders ~/.config/ardour3/
cp /opt/ardour3/etc/pango.modules ~/.config/ardour3/

# Disable extra modules from being loaded by gtk (example, libcanberra-gtk-module.so)
export GTK_MODULES=""
# Set this so that the executable will find all the right libraries inside the bundle
export LD_LIBRARY_PATH=$INSTALL_DIR/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}

if [ "T" = "$DEBUG" ]; then
	export ARDOUR_INSIDE_GDB=1
	exec gdb $INSTALL_DIR/bin/ardour-3.5.380
else
	exec $INSTALL_DIR/bin/ardour-3.5.380 ${A[1]} ${A[2]} ${A[3]} ${A[4]} "$AEND"
fi
 
