#!/bin/sh
# DJPlay launch script
# Written by Adrien Cunin <adri2000@ubuntu.com> for Debian

# First, get the jack driver the user wants to use (default: alsa).
# If jackd and qjackctl are not already running, launch them.
# Launch djplay.
# Kill jackd and qjackctl if they were not running before.

jackdriver=alsa

for argv in "$@"; do
    param=${argv%=*}
    value=${argv#*=}
    case $param in
        "--jackdriver")     jackdriver="$value" ;;
        "--help")           echo "Options:\n  --jackdriver=DRIVER: specify the driver used by jackd (default: alsa)"; exit 0 ;;
        *)                  echo "Bad parameter, please use --help" >&2; exit 1 ;;
    esac
done

if [ -z "$(pidof jackd)" ]; then
    jackd -d $jackdriver&
    jpid="$(pidof jackd)"
fi

if [ -z "$(pidof qjackctl.bin)" ]; then
    qjackctl&
    sleep 1s
    qpid="$(pidof qjackctl.bin)"
fi

djplay

if [ -n "$jpid" ]; then
    kill $jpid
fi

if [ -n "$qpid" ]; then
    kill $qpid
fi
