#!/bin/bash

if [ "$1" = "dummystart" ] || [ "$1" = "dummystop" ]; then
  case $1 in
    dummystart)
      pactl load-module module-null-sink media.class=Audio/Sink sink_name=Dummy channel_map=stereo
      pactl load-module module-virtual-source media.class=Audio/Source source_name=Dummy channel_map=stereo
    ;;
    dummystop)
      pactl unload-module module-null-sink
      pactl unload-module module-virtual-source
    ;;
  esac
  exit 0
fi

if [ "$1" = "startup" ]; then
  set -e
  . /etc/profile.d/ubuntustudio-pwjack.sh
  pw-metadata -n settings 0 clock.force-quantum $(echo $PIPEWIRE_QUANTUM | cut -d "/" -f 1)
  pw-metadata -n settings 0 clock.force-rate $(echo $PIPEWIRE_QUANTUM | cut -d "/" -f 2)
  exit 0
fi

if [ "$1" = "writeparams" ]; then
  echo "$2" > /etc/default/grub.d/ubuntustudio.cfg
  update-grub
  if [ -e /boot/refind_linux.conf ]; then
    if grep -qi 'lowlatency' /boot/refind_linux.conf; then
      _rootUuid=$(lsblk -no UUID "$(df -P / | awk 'END{print $1}')");
      _refindRead=$(grep 'lowlatency' /boot/refind_linux.conf);
      _refindWrite="\"Boot with lowlatency options\" \"root=UUID=${_rootUuid} ro quiet splash $3 $4 $5 vt.handoff=1\"";
      echo -e "${_refindRead}\n${_refindWrite}"
      sed -i "/\\$_refindRead/c\\$_refindWrite" /boot/refind_linux.conf;
    fi
  fi
  exit 0
fi

if ! [ "$(id -nu)" = "ubuntu-studio" ]; then
  if ! [ -e /etc/security/limits.d/30-ubuntustudio-audio.conf ] || [ -z "$(groups | grep 'audio')" ]; then
    infotext="Your system and/or user configuration is not optimally configured for audio \
production. In order to fix this, a system restart will be required."
    zenity \
    --title "Ubuntu Studio Audio Configuration" \
    --window-icon=/usr/share/icons/hicolor/scalable/apps/ubuntustudio-audio-config.svg \
    --info \
    --text="${infotext}"
   if pkexec /usr/sbin/ubuntustudio-installer-fix; then
       touch "${HOME}/.config/ubuntustudio-audio-firstrun"
       reboot
    else
      zenity --title "Ubuntu Studio Audio Configuration" \
      --window-icon=/usr/share/icons/hicolor/scalable/apps/ubuntustudio-audio-config.svg \
      --error \
      --text="An error has occurred."
    fi
    exit 0
  else
    if ! [ -e "${HOME}/.config/ubuntustudio-audio-firstrun" ]; then
      touch "${HOME}/.config/ubuntustudio-audio-firstrun"
      exit 0
    fi
  fi
fi

while true; do

  if dpkg -s "ubuntustudio-pipewire-config" &> /dev/null; then
    pwConfig="TRUE"; paConfig="FALSE"; swConfig="(NOT Recommended)"
  elif dpkg -s "ubuntustudio-pulseaudio-config" &> /dev/null; then
    paConfig="TRUE"; pwConfig="FALSE"; swConfig="(Recommended)"
  else
    zenity \
    --title "Ubuntu Studio Audio Configuration" \
    --window-icon=/usr/share/icons/hicolor/scalable/apps/ubuntustudio-audio-config.svg \
    --warning \
    --text="A default Ubuntu Studio audio configuration package could not be found.
  An audio configuration package will now be installed."
    qapt-batch --install ubuntustudio-pipewire-config
    zenity \
    --title "Ubuntu Studio Audio Configuration" \
    --window-icon=/usr/share/icons/hicolor/scalable/apps/ubuntustudio-audio-config.svg \
    --info \
    --text="You must restart your computer for these changes to take effect."
    exit 0
  fi

  if [ -e /etc/ld.so.conf.d/pipewire-jack.conf ]; then
    pwJackStr="DISABLE PipeWire-JACK (to use JACKd2 with QJackCtl, for Advanced Users)"
  else
    pwJackStr="ENABLE PipeWire-JACK (Default Configuration)"
  fi

  # Get dummy device status
  dummyDead=$(systemctl --user status ubuntustudio-dummy-audio.service | grep active | grep dead)
  if [ "${dummyDead}" = "" ]; then
    dummyDead="FALSE"
  else
    dummyDead="TRUE"
  fi
  if [ "${dummyDead}" = "FALSE" ]; then
    dummyStr="STOP Dummy Audio Device"
  else
    dummyStr="START Dummy Audio Device"
  fi

  changeConfig=$(zenity --width=600 --height=450 \
      --title "Ubuntu Studio Audio Configuration" \
      --window-icon=/usr/share/icons/hicolor/scalable/apps/ubuntustudio-audio-config.svg \
      --list \
      --radiolist \
      --separator="|" \
      --text="What would you like to do?" \
      --ok-label="Continue" \
      --cancel-label="Close" \
      --column="Select" \
      --column="Selection" \
      FALSE "Configure Current Audio Configuration" FALSE "Change Kernel Boot Parameters" FALSE "${dummyStr}" FALSE "${pwJackStr}" );
  strExit=$?
  if [ "${strExit}" = "1" ]; then
    exit 0;
  fi

  if [ "${changeConfig}" = "Configure Current Audio Configuration" ] && \
     [ "${paConfig}" = "TRUE" ]; then
    if ( zenity \
    --title "Ubuntu Studio Audio Configuration" \
    --window-icon=/usr/share/icons/hicolor/scalable/apps/ubuntustudio-audio-config.svg \
    --question \
    --ok-label="Yes" \
    --cancel-label="No" \
    --text="For your current audio configuration (JACK/PulseAudio), please use Studio Controls.\n\
  Would you like to launch Studio Controls now?" ); then
      studio-controls;
      exit 0;
    fi
  fi

  if [ "${changeConfig}" = "Configure Current Audio Configuration" ] && \
     [ "${pwConfig}" = "TRUE" ]; then
    pwSetStr=$(zenity \
    --title "Ubuntu Studio Audio Configuration" \
    --window-icon=/usr/share/icons/hicolor/scalable/apps/ubuntustudio-audio-config.svg \
    --entry \
    --ok-label="OK" \
    --cancel-label="Cancel" \
    --text="Please enter the values for the PipeWire Quantum / JACK Plugin
  configuration separated by a space.\n
  First value is the Buffer Size which can be one of the following:
  16, 32, 64, 128, 256, 512, 1024, 2048, or 4096\n
  Next value is the Sample Rate in Hertz (Hz) which can be one of the following:
  22050, 32000, 44100, 48000, 88200, 96000, or 192000\n
  Example (default): 1024 48000\n
  Leaving this blank will reset to default unless cancelled
  If you're not sure what to do here, it's best to reset to defaults." )
    strExit=$?
    if ! [ "${strExit}" = "1" ]; then
      if [ -n "${pwSetStr}" ]; then
        pwSetArry[0]=$(echo "${pwSetStr}" | cut -d " " -f 1)
        pwSetArry[1]=$(echo "${pwSetStr}" | cut -d " " -f 2)
        if pkexec ubuntustudio-pwjack-config ${pwSetArry[0]} ${pwSetArry[1]}; then
          . /etc/profile.d/ubuntustudio-pwjack.sh
          pw-metadata -n settings 0 clock.force-quantum $(echo $PIPEWIRE_QUANTUM | cut -d "/" -f 1)
          pw-metadata -n settings 0 clock.force-rate $(echo $PIPEWIRE_QUANTUM | cut -d "/" -f 2)
          zenity --title "Ubuntu Studio Audio Configuration" \
          --window-icon=/usr/share/icons/hicolor/scalable/apps/ubuntustudio-audio-config.svg \
          --info \
          --text "PipeWire Quantum set to ${pwSetArry[0]} Buffer and
  ${pwSetArry[1]} Hz Sample Rate."
        else
          zenity --title "Ubuntu Studio Audio Configuration" \
          --window-icon=/usr/share/icons/hicolor/scalable/apps/ubuntustudio-audio-config.svg \
          --error \
          --text="An error has occurred. Check your entry and try again."
        fi
      else
        if pkexec ubuntustudio-pwjack-config 1024 48000; then
          . /etc/profile.d/ubuntustudio-pwjack.sh
          pw-metadata -n settings 0 clock.force-quantum $(echo $PIPEWIRE_QUANTUM | cut -d "/" -f 1)
          pw-metadata -n settings 0 clock.force-rate $(echo $PIPEWIRE_QUANTUM | cut -d "/" -f 2)
          zenity --title "Ubuntu Studio Audio Configuration" \
          --window-icon=/usr/share/icons/hicolor/scalable/apps/ubuntustudio-audio-config.svg \
          --info \
          --text="PipeWire Quantum set to default 1024 Buffer and
  48000 Hz Sample Rate."
        else
          zenity --title "Ubuntu Studio Audio Configuration" \
          --window-icon=/usr/share/icons/hicolor/scalable/apps/ubuntustudio-audio-config.svg \
          --error \
          --text="An error has occurred."
        fi
      fi
    fi
  fi

  if [ "${changeConfig}" = "Change Kernel Boot Parameters" ]; then
    # Parse current Boot Parameters
    bootParamsLine=$(grep 'GRUB_CMDLINE_LINUX_DEFAULT' /etc/default/grub.d/ubuntustudio.cfg)
    bootParamsLine=${bootParamsLine::-1}
    bootParamsLine=$(echo "${bootParamsLine}" | cut -c 57- )
    bootParamOne="FALSE"; bootParamTwo="FALSE"; bootParamThree="FALSE"
    echo "${bootParamsLine}"
    declare -a j=(
    [1]=1
    [2]=2
    [3]=3)
    for i in ${j[@]}; do
      parameter=$(echo ${bootParamsLine} | cut -d ' ' -f ${i} )
      echo "${parameter}"
      if [ "${parameter}" = "preempt=full" ]; then
        bootParamOne="TRUE"
      elif [ "${parameter}" = "threadirqs" ]; then
        bootParamTwo="TRUE"
      elif [ "${parameter}" = "nohz_full=all" ]; then
        bootParamThree="TRUE"
      fi
    done
    # Build the Parameter table for the UI
    declare -a paramTableArry=(
    [0]=${bootParamOne}
    [1]="preempt=full"
    [2]="Makes the kernel fully-preemptible, best for lowlatency workloads"
    [3]=${bootParamTwo}
    [4]="threadirqs"
    [5]="Forces interrupt handlers to run in a threaded context, reducing buffer xruns"
    [6]=${bootParamThree}
    [7]="nohz_full=all"
    [8]="Reduces noise in latency critical applications such as virtualization"
    )
    newBootParams=$(zenity --width=800 --height=350 \
      --title "Ubuntu Studio Audio Configuration" \
      --window-icon=/usr/share/icons/hicolor/scalable/apps/ubuntustudio-audio-config.svg \
      --list \
      --checklist \
      --separator=" " \
      --text="Select which kernel boot parameters you would like. Default is first two." \
      --ok-label="Apply (Password Required)" \
      --cancel-label="Back" \
      --column=" " \
      --column="Parameter" \
      --column="Description" \
      "${paramTableArry[@]}"
    )
    exitCode=$?
    if [ "${exitCode}" = "1" ]; then
      true; #do nothing
    else
      grubCfg=$(cat << EOF
# Activate lowlatency effects of kernel
GRUB_CMDLINE_LINUX_DEFAULT="\$GRUB_CMDLINE_LINUX_DEFAULT ${newBootParams[@]}"
EOF
);
      if pkexec /usr/bin/ubuntustudio-audio-config writeparams "${grubCfg}" ${newBootParams[@]} | \
      zenity --progress --auto-close --no-cancel --pulsate; then
        zenity \
           --title "Ubuntu Studio Audio Configuration" \
           --window-icon=/usr/share/icons/hicolor/scalable/apps/ubuntustudio-audio-config.svg \
           --info \
           --text="You must restart your computer for these changes to take effect."
      else
        zenity --title "Ubuntu Studio Audio Configuration" \
          --window-icon=/usr/share/icons/hicolor/scalable/apps/ubuntustudio-audio-config.svg \
          --error \
          --text="An error has occurred or action was cancelled."
      fi
    fi
  fi

  if [ "${changeConfig}" = "${dummyStr}" ]; then
    if [ "${dummyDead}" = "TRUE" ]; then
      dummyInfoText="Sometimes, you may wish to enable a dummy audio device for your system \
audio or an audio application so that you don't inadvertently feed that to \
your main output or draw from your main input. This can be useful when you wish \
to send a music application to your your DAW or other mixer without sending to \
your main system output. There may be other workflow reasons as well.\n\n \
Do you wish to start a stereo dummy audio device?"
      if (zenity --width=400 --height=200 \
        --question \
        --title="Ubuntu Studio Audio Configuration" \
        --window-icon=/usr/share/icons/hicolor/scalable/apps/ubuntustudio-audio-config.svg \
        --cancel-label="Go Back" \
        --text="${dummyInfoText}"); then
        systemctl --user start ubuntustudio-dummy-audio.service
        if (zenity --width=200 --height=100 \
          --question \
          --title="Ubuntu Studio Audio Configuration" \
          --window-icon=/usr/share/icons/hicolor/scalable/apps/ubuntustudio-audio-config.svg \
          --ok-label="Yes" \
          --cancel-label="No" \
          --text="Would you like the dummy audio device to start at login?"); then
          systemctl --user enable ubuntustudio-dummy-audio.service || true
        else
          systemctl --user disable ubuntustudio-dummy-audio.service || true
        fi
      fi
    else
      if (zenity --width=200 --height=100 \
        --question \
        --title="Ubuntu Studio Audio Configuration" \
        --window-icon=/usr/share/icons/hicolor/scalable/apps/ubuntustudio-audio-config.svg \
        --ok-label="Yes" \
        --cancel-label="No" \
        --text="Would you like to stop the dummy audio device?"); then
        systemctl --user stop ubuntustudio-dummy-audio.service
        if (zenity --width=200 --height=100 \
          --question \
          --title="Ubuntu Studio Audio Configuration" \
          --window-icon=/usr/share/icons/hicolor/scalable/apps/ubuntustudio-audio-config.svg \
          --ok-label="Yes" \
          --cancel-label="No" \
          --text="Would you like the dummy audio device to start at login?"); then
          systemctl --user enable ubuntustudio-dummy-audio.service || true
        else
          systemctl --user disable ubuntustudio-dummy-audio.service || true
        fi
      fi
    fi
  fi

  if [ "${changeConfig}" = "${pwJackStr}" ]; then
    if [ -e /etc/ld.so.conf.d/pipewire-jack.conf ]; then
      if pkexec ubuntustudio-pwjack-config disable; then true; else
        zenity --title "Ubuntu Studio Audio Configuration" \
          --window-icon=/usr/share/icons/hicolor/scalable/apps/ubuntustudio-audio-config.svg \
          --error \
          --text="An error has occurred."
      fi
    else
      if pkexec ubuntustudio-pwjack-config enable; then true; else
        zenity --title "Ubuntu Studio Audio Configuration" \
          --window-icon=/usr/share/icons/hicolor/scalable/apps/ubuntustudio-audio-config.svg \
          --error \
          --text="An error has occurred."
      fi
    fi
  fi

done
