#!/bin/sh
set -e

if [ "$1" = "" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
  echo "Usage: ubuntustudio-pwjack-config [disable|enable]|BUFFERSIZE SAMPLERATE"
  echo "       where BUFFERSIZE is 16, 32, 64, 128, 256, 512, 1024, 2048, or 4096"
  echo "       and SAMPLERATE is 22050, 32000, 44100, 48000, 88200, 96000, or 192000"
  if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then exit 0; else exit 1; fi
elif [ "$1" = "enable" ]; then
  ln -fs /usr/share/doc/pipewire/examples/ld.so.conf.d/pipewire-jack-*-linux-gnu.conf \
    /etc/ld.so.conf.d/pipewire-jack.conf
  ldconfig
elif [ "$1" = "disable" ]; then
  rm -f /etc/ld.so.conf.d/pipewire-jack.conf || true
  ldconfig
elif [ "$1" = "16" ] || [ "$1" = "32" ] || [ "$1" = "64" ] ||\
     [ "$1" = "128" ] || [ "$1" = "256" ] || [ "$1" = "512" ] || [ "$1" = "1024" ] ||\
     [ "$1" = "2048" ] || [ "$1" = "4096" ]; then
  if [ "$2" = "22050" ] || [ "$2" = "32000" ] || [ "$2" = "44100" ] ||\
     [ "$2" = "48000" ] || [ "$2" = "88200" ] || [ "$2" = "96000" ] ||\
     [ "$2" = "192000" ]; then
       writefile=$(cat << EOF 
export PIPEWIRE_QUANTUM="$1/$2"
EOF
    )
    echo "${writefile}" > /etc/profile.d/ubuntustudio-pwjack.sh
    echo "Log out and in for changes to take effect."
  fi
else
  echo "Invalid Value"
  exit 2
fi
