#!/bin/bash

# GPL v.3
# Copyright 2024 antiX community, anticapitalista.
# Written 1/2024 by Robin.antiX
# Version 0.4

TEXTDOMAINDIR=/usr/share/locale
TEXTDOMAIN=antiX-acoustic-colours

aac_lib="/etc/skel/.config/easyeffects"  # default config from which missing files in users individual config can get restored
aac_config="$XDG_CONFIG_HOME/antiX-acoustic-colours"   # inidvidual config in user's home
last_state="" # variable takes last state of acoustic colours setting
easyeffects_running=false  # variable to take previous state of easyeffects
easyeffects_visible=false  # variable to take previous state of easyeffects


mkdir -p "$aac_config"

# check for existing files and move them aside before copying the ones we need from aac_lib
function file_check () {
    private="$XDG_CONFIG_HOME"'/'"${1#/etc/skel/.config}"
    [ -f "$private" ] && mv "$private" "$private"'.bak'
    mkdir -p "${private%/*}"
    cp "$1" "${private%/*}"
}

# check for mising files and restore them from aac_lib
function file_renew () {
    private="$XDG_CONFIG_HOME"'/'"${1#/etc/skel/.config}"
    if [ ! -f "$private" ]; then
        mkdir -p "${private%/*}"
        cp "$1" "${private%/*}"
    fi
}

# check whether pipewire is installed and sound server up and running. Exit on error.
if ! wpctl status >/dev/null; then
   yad --window-icon=antiXac --info --fixed \
   --title=$"No Pipewire." \
   --borders=15 --image=antiXac --on-top --center \
   --text="<b>"$"Warning!""</b>\n\n\t"$"Pipewire not running.""\n\t"$"Please go to antiX control centre\n\tand start Pipewire before.""\n" \
   --button=$"OK":0
   exit 1
fi

# Depending on whether this is a first run check for missing files or copy all files over from aac_lib
if [ ! -f "$aac_config"/.0 ]; then
    while IFS= read -d '' i; do    # on first run copy needed files to user's home
       file_check "${i}" </dev/null
    done < <(find "$aac_lib" -name '*' -type f -print0)
    touch "$aac_config"/.0    # set flag file after setup on first run was completed.
else
    while IFS= read -d '' i; do    # on later runs check for files we need to run user has deleted
       file_renew "${i}" </dev/null
    done < <(find "$aac_lib" -name '*' -type f -print0)
fi

# make sure easyeffects is running in it's pseudo-demonised mode
if pidof easyeffects >/dev/null; then if xdotool search --onlyvisible --pid $(pidof easyeffects) >/dev/null; then easyeffects_visible=true; fi; fi
ps -aux | grep -F 'easyeffects --gapplication-service' | grep -v grep >/dev/null
if [ $? != 0 ] ; then
    if pidof easyeffects >/dev/null; then kill -15 $(pidof easyeffects); easyeffects_running=true; while pidof easyeffects >/dev/null; do sleep .1; done; fi
    easyeffects --gapplication-service &
    while ! pidof easyeffects >/dev/null; do sleep .1; done;
    if $easyeffects_running; then
        easyeffects &
        if ! $easyeffects_visible; then
            # dirty workaround for xdotool search for pid reports falsely two window ID's of same name for the PID, rendering them undistinguishable. And wmctrl can't do minimise...
            # another dirty workaround for easyeffects creating some more pid's and window ids on startup, just to drop them winthin a second again.
            # and then we have to wait for slow hardware in a loop, on fast hardware loop is left immediately by break statement.
            wid_easyeffects=0; while [ "$wid_easyeffects" -eq 0 ]; do wid_easyeffects="$(($(wmctrl -l -p | grep $(pidof easyeffects) 2>/dev/null | cut -d' ' -f1)))"; [ "$wid_easyeffects" != 0 ] && break; sleep .5; done
            xdotool windowminimize $wid_easyeffects
        fi  
    fi
fi

# read current preset from easyeffects for display in window border
i="$(gsettings get com.github.wwmm.easyeffects last-used-output-preset)"
i="${i:1:-1}"
case "$i" in
    'Bass Boosted')             last_state=" – "$"Bass";;
    'Treble (thin)')            last_state=" – "$"Treble";;
    'Tenor Boosted')            last_state=" – "$"Solo";;
    'Speech Intelligibility 2') last_state=" – "$"Speech";;
    'Advanced Auto Gain') last_state=" – "$"Orchestra";;
    'Advanced Auto Gain + Loudness Compensation') last_state=" – "$"Jazz";;
    'Cathedral')   last_state=" – "$"Cathedral";;
    'Muffled')     last_state=" – "$"Muffled";;
    'Limiter only')     last_state=" – "$"Off";;
     *)  last_state=" – "$"Unknown";;
esac;

# calculate window positon, so window doesn't come up in arbitrary positions on scereen after pressing a button.
y=$(($(xprop -root '_NET_WORKAREA' | sed -e "s/, / /g" | cut -d' ' -f6)-100)) # near lower border, own window hight must be taken into account.
x=20  # near left border
if $(wmctrl -m | grep herbstluft >/dev/null); then x=y=0; fi  # herbstluftwm must be treated separately

# start processing GUI dialog and preset switching loop
while :; do
   yad --title=$"antiX acoustic colours""$last_state" \
   --height=50 --posx=$x --posy=$y --fixed \
   --window-icon=antiXac \
   --button=$"Bass":0 \
   --button=$"Treble":2 \
   --button=$"Solo":4 \
   --button=$"Speech":6 \
   --button=$"Orchestra":8 \
   --button=$"Jazz":10 \
   --button=$"Cathedral":12 \
   --button=$"Muffled":14 \
   --button=$"Off":16
   case "$?" in
      0)  i='Bass Boosted'; last_state=" – "$"Bass";;
      2)  i='Treble (thin)'; last_state=" – "$"Treble";;
      4)  i='Tenor Boosted'; last_state=" – "$"Solo";;
      6)  i='Speech Intelligibility 2'; last_state=" – "$"Speech";;
      8)  i='Advanced Auto Gain'; last_state=" – "$"Orchestra";;
     10)  i='Advanced Auto Gain + Loudness Compensation'; last_state=" – "$"Jazz";;
     12)  i='Cathedral'; last_state=" – "$"Cathedral";;
     14)  i='Muffled'; last_state=" – "$"Muffled";;
     16)  i='Limiter only'; last_state=" – "$"Off";;
    252)  break;;
   esac;
   easyeffects -l "$i" &
done;

exit 0
