#!/usr/bin/env sh

# RUN THIS SCRIPT RIGHT AFTER ONE OF THE FOLLOWING:
#   - hellwal
#   - pywal
#   - wallust
#
# TO PROPERLY APPLY COLORS!

confPath="/home/$USER/.config"
supportedClients=("Vencord" "vesktop")
availableClients=()
generatedColors="hellwal" # default option is hellwal
colosPath=""
themeFiller="$dark_theme"

show_help() {
    echo "Usage: themecord [OPTION]"
    echo "Discord theme will always match your wallpaper! "
    echo
    echo "Options:"
    echo "  -h            Display this message and exit"
    echo "  -l            use light mode instead of dark mode"
    echo
    echo "Select wallpaper palette backend:"
    echo "  -g            Use hellwal"
    echo "  -p            Use pywal"
    echo "  -w            Use wallust"
    echo
    echo "  -f            Provide path to generated discord colors"
    echo
}

while getopts ":hlwpgf:" opt; do
    case ${opt} in
        h )
            show_help
            exit 0
            ;;
        l )
            themeFiller="$light_theme"
            ;;
        w )
            generatedColors="wallust"
            ;;
        p )
            generatedColors="wal"
            ;;
        g )
            generatedColors="hellwal"
            colorsPath="$OPTARG"
            ;;
        f )
            colorsPath="$OPTARG"
            ;;
        \? )
            echo "Invalid option: -$OPTARG" 1>&2
            echo
            show_help
            exit 1
            ;;
    esac
done

function print_err() {
    echo "[ERROR] $1"
    echo "Exitting..."
}

for client in "${supportedClients[@]}"; do
    client_path=~/.config/$client
    if test -d $(realpath "$client_path"); then
        echo "[SUCCESS] $client DETECTED (path: $client_path)"
        availableClients+=("$client")
        continue
    else
        echo "[FAILURE] $client not DETECTED (path: $client_path)"
        continue
    fi
done

if [ -z "$availableClients" ]; then
    print_err "No compatible clients detected! In order to use this script you have to use compatible discord clients! Check out github page for more info: https://github.com/danihek/Themecord"
    exit
fi

for client in "${availableClients[@]}"; do
    themecordPath="/home/$USER/.config/$client/themes/Themecord.css"

    if test -f "$themecordPath"; then
        echo "[INFO] Themecord found in $client"
    else
        echo :root { > $themecordPath
        echo "}" >> $themecordPath
        printf '%s\n' "$themecordBase" >> $themecordPath
        echo "[INFO] Added themecord base to: $themecordPath"
    fi
done

if [ "$colorsPath" == "" ]; then
    colorsPath="/home/$USER/.cache/$generatedColors/discord-colors.css"
fi

if [ ! -f "$colorsPath" ]; then
    print_err "Cannot access $colorsPath"
    exit
fi
echo "[INFO] discord-colors.css path: $colorsPath"

for client in "${availableClients[@]}"; do
    themecordPath="/home/$USER/.config/$client/themes/Themecord.css"

    echo "/* This discord theme is generated by Themecord! */" > $themecordPath
    echo :root { >> $themecordPath
    cat $colorsPath | while IFS= read -r line; do echo -e "\n\t"$line >> $themecordPath; done

    if [[ -v themecordFiller ]]; then
        printf '%s\n' "$themecordFiller" >> $themecordPath
    else
        print_err "Cannot access \$themecordFiller content."
    fi

    if [[ -v themeFiller ]]; then
        printf '%s\n' "$themeFiller " >> $themecordPath
    else
        print_err "Cannot access \$themeFiller content."
    fi

    if [[ -v themecordFiller2 ]]; then
        printf '%s\n' "$themecordFiller2" >> $themecordPath
    else
        print_err "Cannot access \$themecordFiller2 content."
    fi

    printf "%s\n" "$themecordBase" >> $themecordPath
done
