#!/bin/bash
#################################################################
#                                                               #
#  Starts the cava data stream and saves it to cava-output.     #
#  It also starts a loop to see if spectrum-equalizer conky     #
#  is still running. If not running, then it terminates cava.   #
#                                                               #
#  It depends on pgrep and wmctrl (lua)                         #
#                                                               #
#  Scripted by Koentje  (remon@cobrasoft.nl)                    #
#                                                               #
#################################################################
currver="2.1"


if [ "$1" != "gocava" ]; then
   echo -e "\n\e[31mERROR:\e[m cava-loop can not start outside of conky!\n"
   exit 1
fi

source "./settings.ini"
scriptname="$(basename $0)    :"

# Check for dependencies
if [ "$(which pgrep)" = "" ]; then
   sleep 1
   pkill -f spectrum-eq.conky
   echo -e "\n$(basename $0)  :\e[31m PGREP not found!!\e[m  'sudo apt install procps'\n" > /dev/stdout
   exit 1
fi
if [ "$(which wmctrl)" = "" ]; then
   sleep 1
   pkill -f spectrum-eq.conky
   echo -e "\n$(basename $0)  :\e[31m WMCTRL not found!!\e[m  'sudo apt install wmctrl'\n" > /dev/stdout
   exit 1
fi


# Terminate all
terminate() {
  echo -e "$scriptname Cava engine terminated" > /dev/stdout
  echo "0" > "$cavaout/cava-spectrum.fps"
  killall -q cava
  pkill -f spectrum-eq.conky
  killall -q $(basename $0)
  exit 0
}


# Check if cava and conky are already running, if so then exit script
  icava=$(pgrep -lx cava)
  iconky=$(pgrep -f "spectrum-eq.conky" > /dev/null)
  if [ "$icava" != "" ] && [ "$iconky" != "" ]; then
    # Exit this script because both are already runnung!
    exit 1
  elif [ "$icava" != "" ] && [ "$iconky" = "" ]; then
    # Terminate cava engine because spectrum conky is not running!
    echo 1
    terminate
    exit 1
  fi


# Set loop to check if spectrum conky is still running
# If not then kill cava engine and exit script
function conky_active () {
  sleep 4
  iconky=$(pgrep -f "spectrum-eq.conky") # > /dev/null)
  if [ "$iconky" = "" ]; then
    echo 2
    terminate
  fi
  conky_active
}
conky_active &


# Check cava-config number of bars and change that number in the spectrum configs
  cavabars=$(grep 'bars = ' "./cava-config" | awk -F '= ' '{print $2}')
  while read -r file
  do
        luabars=$(grep -I '^bars=' "./spectrum-configs/$file" | awk -F '=' '{print $2}')
        if ! [ "$cavabars" = "$luabars" ]; then
          if [ "$(cat ./spectrum-configs/$file | head -n1)" = "[spectrum-config]" ]; then
            if [ -f "/tmp/speq-editor.debug" ]; then 
              echo -e "$scriptname Changing bar numbers in Spectrum config \e[33;1m'$file'\e[m to \e[32;1m$cavabars\e[m" > /dev/stdout
            fi
            sed -i "/^bars=$luabars/c\bars=$cavabars" "./spectrum-configs/$file"
          fi
        fi
  done< <(ls -1 ./spectrum-configs)


# Saves last line from cava data stream to tmp file
  echo "$scriptname Cava engine started" > /dev/stdout
  echo "$scriptname Output to $cavaout" > /dev/stdout
  while read -r  line
  do
     echo "$line" > "$cavaout/cava-output"
  done< <(cava -p ./cava-config)
  echo 3
  terminate

exit 0
