#!/bin/bash
# Script to start Carla bridges

INSTALL_PREFIX="X-PREFIX-X"
CARLA_PREFIX="$INSTALL_PREFIX"/lib/carla

# ----------------------------------------------------------------------
# Check for enough arguments

if [ "$3"x == ""x ]; then
  echo "usage: $0 [arch] [mode] [filename] [label/uri]

Possible archs:
  - \"native\"
  - \"posix32\"
  - \"posix64\"
  - \"win32\"
  - \"win64\"

Possible modes:
  - \"ladspa\"
  - \"dssi\"
  - \"lv2\"
  - \"vst\"

Examples:
  $0 native dssi \"/usr/lib/dssi/hexter.so\" \"hexter\"
  $0 native lv2 \"/usr/lib/lv2/calf/\" \"http://calf.sourceforge.net/plugins/Compressor\"
  $0 native vst \"/usr/lib/vst/TAL-NoiseMaker.so\"
"
  exit
fi

# ----------------------------------------------------------------------
# Set client name (from environment)

if [ "$CARLA_CLIENT_NAME"x == ""x ]; then
  CARLA_CLIENT_NAME="(none)"
fi

# ----------------------------------------------------------------------
# Set variables

RUN_ARCH="$1"
RUN_MODE="$2"
RUN_FILE="$3"
RUN_LABEL="$4"

# ----------------------------------------------------------------------
# Fix arch for windows bridges

if [ $RUN_ARCH == "win32" ]; then
  RUN_ARCH="win32.exe"
fi

if [ $RUN_ARCH == "win64" ]; then
  RUN_ARCH="win64.exe"
fi

# ----------------------------------------------------------------------
# Check for existing carla folder

if [ ! -d $CARLA_PREFIX ]; then
  echo "$0: Carla folder does not exist, is it installed?"
  exit
fi

# ----------------------------------------------------------------------
# Check for existing arch binary

CARLA_EXEC="$CARLA_PREFIX/carla-bridge-$RUN_ARCH"

if [ ! -f $CARLA_EXEC ]; then
  echo "$0: Invalid arch (may not be installed)"
  exit
fi

# ----------------------------------------------------------------------
# Check mode

if [ "$RUN_MODE"x == "ladspa"x ]; then
  if [ "$RUN_LABEL"x == ""x ]; then
    echo "$0: LADSPA needs label"
    exit
  fi
  RUN_MODE="DSSI"
elif [ "$RUN_MODE"x == "dssi"x ]; then
  if [ "$RUN_LABEL"x == ""x ]; then
    echo "$0: DSSI needs label"
    exit
  fi
  RUN_MODE="DSSI"
elif [ "$RUN_MODE"x == "lv2"x ]; then
  if [ "$RUN_LABEL"x == ""x ]; then
    echo "$0: LV2 needs uri"
    exit
  fi
  RUN_MODE="LV2"
elif [ "$RUN_MODE"x == "vst"x ]; then
  RUN_MODE="VST"
else
  echo "$0: Invalid mode"
  exit
fi

# ----------------------------------------------------------------------
# Exec

exec $CARLA_EXEC "null" "$RUN_MODE" "$RUN_FILE" "$CARLA_CLIENT_NAME" "$RUN_LABEL"
