#!/bin/sh

# Papirus Icon Theme
{
    if papirus-folders -l | grep "{{@@ accent_color_name @@}}"
    then
        notify-send "Switching Folder Color"
        papirus-folders -uC "{{@@ accent_color_name @@}}"
    else
        papirus-folders -uD
    fi
} &> /dev/null &

# Materia GTK Theme
src_path="$HOME/.cache/materia-theme"

theme_name="{{@@ gtk_theme @@}}"
dst_path="${HOME}/.themes"

test -d "$src_path" ||
    git clone\
    "https://github.com/nana-4/materia-theme"\
    "$src_path"

cd "$src_path"

# SEL = accent
# BG = header-bar
# HDR = menubar
# View = Main part of file browser
# Surface = background of context menu
colors="\
SEL_BG={{@@             accent_color    .lstrip('#') @@}}\n\
FG={{@@                 color.txt       .lstrip('#') @@}}\n\
BG={{@@                 color.bg        .lstrip('#') @@}}\n\
HDR_FG={{@@             color.txt       .lstrip('#') @@}}\n\
HDR_BG={{@@             color.bg        .lstrip('#') @@}}\n\
MATERIA_VIEW={{@@       color.bg        .lstrip('#') @@}}\n\
MATERIA_SURFACE={{@@    color.bg        .lstrip('#') @@}}"

color_file="${dst_path}/${theme_name}/_colors"
# No changes in input, no change in output
echo -e "$colors" |
    diff "$color_file" - &&
    exit 0

mkdir -p "${dst_path}/${theme_name}"

{
    notify-send "Generating theme in Background"

    echo -e "$colors" > "$color_file"
    ./change_color.sh \
        -t "${dst_path}" \
        -o "${theme_name}" \
        "$color_file"
    echo -e "$colors" > "$color_file"

    notify-send "Finished generating theme"
} &> /dev/null &
