#!/bin/sh

{%@@ if 'papirus' in icon_theme.lower() @@%}
# Papirus Icon Theme
{
    papirus-folders -l | grep "[^>] {{@@ accent_color_name @@}}"&&
        notify-send "Switching Folder Color"

    papirus-folders -uC "{{@@ accent_color_name @@}}" ||
        papirus-folders -uD

} > /dev/null &
{%@@ endif @@%}

{%@@ if 'materia' in gtk_theme.lower() @@%}

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

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

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

cd "$TMPDIR"

# 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"

    sudo cp -vr "${dst_path}/${theme_name}" /usr/share/themes/ ||
    doas cp -vr "${dst_path}/${theme_name}" /usr/share/themes/

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

{%@@ endif @@%}

{%@@ if 'orchis' in gtk_theme.lower() @@%}
    MARKER_FILE="$HOME/.themes/.orchis_installed"

    test -f "$MARKER_FILE" &&
    exit 0

    TMPDIR="$HOME/.cache/share/orchis"

    mkdir -p "$TMPDIR" && cd "$TMPDIR"

    test -d .git &&
        git pull ||
        git clone "https://github.com/vinceliuice/Orchis-theme" "$TMPDIR"

    ./install.sh \
        --theme "{{@@ accent_color_name.lower() @@}}" \
        --tweaks "solid" --tweaks "compact"

    touch "$MARKER_FILE"
{%@@ endif @@%}
