#!/bin/bash

# Define the character bars and the dictionary for replacements
bar="▁▂▃▄▅▆▇█"
replacement_dict="s/;//g;"

# Create the dictionary to replace characters with bars
for ((i=0; i<${#bar}; i++))
do
    replacement_dict="${replacement_dict}s/$i/${bar:$i:1}/g;"
done

# Ensure the FIFO pipe is clean
pipe="/tmp/cava.fifo"
if [ -p "$pipe" ]; then
    unlink "$pipe"
fi
mkfifo "$pipe"

# Define the Cava configuration file
config_file="/tmp/cava_config"
cat <<EOF > "$config_file"
[general]
bars = 26
framerate = 120
sensitivity = 200

[output]
method = raw
channels = mono
raw_target = $pipe
data_format = ascii
ascii_max_range = 7

[smoothing]
noise_reduction = 70

[eq]
1 = 0.7
2 = 1
3 = 1.2
4 = 1.5
5 = 1
6 = 0.8
EOF

# Run Cava in the background
cava -p "$config_file" &

# Read and process data from the FIFO
while read -r cmd; do
    echo "$cmd" | sed "$replacement_dict"
done < "$pipe"

# Clean up the FIFO when done (optional)
if [ -p "$pipe" ]; then
    unlink "$pipe"
fi
