set -e

defaultDevice=PCH

start_jack(){

  internal_device_number=-1
  komplete_device_number=$(aplay -l | grep Vestax | cut -d":" -f1 | cut -d" " -f2)
  babyface_device_number=$(aplay -l | grep Babyface | cut -d":" -f1 | cut -d" " -f2)
  cmedia_device_number=$(aplay -l | grep C-Media | cut -d":" -f1 | cut -d" " -f2)
  h2n_device_number=$(aplay -l | grep H2n | cut -d":" -f1 | cut -d" " -f2)

  # this should be more readable some day
  if [[ $babyface_device_number == "" ]]; then
    if [[ $komplete_device_number == "" ]]; then
      if [[ $cmedia_device_number == "" ]]; then
        if [[ $h2n_device_number == "" ]]; then
          echo "use : default device"
          device_number=$internal_device_number
        else
          echo "use : h2n"
          device_number=$h2n_device_number
        fi
      else
        echo "use : c-media"
        device_number=$cmedia_device_number
      fi
    else
      echo "use : komplete"
      device_number=$komplete_device_number
    fi
  else
    echo "use : babyface"
    device_number=$babyface_device_number
  fi

  # device parameter configuration
  # ==============================
  #
  # to find configuration options do
  # jack_control dp
  if [[ $device_number -eq -1 ]]
  then
    # we use alsa in reality, but pulse opens up all the pulse
    # sink and source stuff
    # jack_control ds pulse # not working for some reason
    jack_control ds alsa
    jack_control dps device hw:$defaultDevice
  else
    jack_control ds  alsa
    jack_control dps device hw:$device_number  # use usb card
  fi

  jack_control dps duplex True                 # record and playback ports
  jack_control dps hwmon  False                # no hardware monitoring
  jack_control dps rate   48000                # use cd sample rate


  # nperiods are the splitup of the
  # sound-ring-buffer. 2 are ok for internal cards
  # but for usb you should use 3 because
  # you can have to write in junks to the card
  # so there is one backup slice in the middle
  if [[ $internal_device_number -ne -1 ]]
  then
    jack_control dps nperiods 3
  fi

  # engine parameter configuration
  # ==============================
  #
  # to find configuration options do
  # jack_control ep
  jack_control eps sync True

  # realtime kernel
  # set True for using a realtime kernel
  jack_control eps realtime False
  # set priority if realtime kernel is set True
  # jack_control eps realtime-priority 10

  jack_control start
}

stop_jack(){
  jack_control exit
}

status_jack() {
  jack_control dp
  jack_control ep
  jack_control status
}


case $1 in
  start) start_jack
    ;;
  stop) stop_jack
    ;;
  restart) stop_jack ; start_jack
    ;;
  *) status_jack
    ;;
esac