#!/bin/bash

GRADIENT_FILE="$HOME"/.config/colorizer/gradients_all.inc
if [ ! -f $GRADIENT_FILE ]; then
cp -a /usr/share/mabox-colorizer/gradients_all.inc $GRADIENT_FILE
fi

case "$1" in
    ob)
    CACHEFILE="$HOME"/.config/colorizer/grad_ob.csv
    COMMAND="obtctl activegradient"
    me="colorizer-ob -s"
    ;;
    menu)
    CACHEFILE="$HOME"/.config/colorizer/grad_menu.csv
    COMMAND="jgctl gradient"
    me="colorizer-menus -s"  
    ;;
    cava)
    CACHEFILE="$HOME"/.config/colorizer/grad_cava.csv
    COMMAND="cavactl gradient"
    me="colorizer-cava -s"
    ;;
    *)
    CACHEFILE="$HOME"/.config/colorizer/grad_root.csv
    COMMAND="w2theme gradient"
    me="colorizer -s"
    ;;

esac


if [ -f "$CACHEFILE" ]; then
cat "$CACHEFILE"
else

case $LANG in
    pl*)
    GRADIENTS="Gradienty"
    MORE_GRADIENTS="Więcej..."
    ;;
    *)
    GRADIENTS="Gradients"
    MORE_GRADIENTS="More gradients"
    ;;
esac

out2+=("^sep($GRADIENTS)")
n=0
while read -r FROM TO NAME;
do 
    ((n++))
    for i in $(pastel gradient -n 24 ${FROM} ${TO}|pastel format hex)
    do
        tablica+="<span bgcolor='$i'> </span>"
    done
    case "$n" in
    31)   out2+=("^sep($MORE_GRADIENTS)")
    out2+=("$GRADIENTS 30...60,^checkout(grad31)")
    out3+=("^tag(grad31)")
    out3+=("^sep($GRADIENTS 30...60)");;
    61) out2+=("$GRADIENTS 61...90,^checkout(grad61)")
    out3+=("^tag(grad61)")
    out3+=("^sep($GRADIENTS 61...90)");;
    91) out2+=("$GRADIENTS 91...120,^checkout(grad91)")
    out3+=("^tag(grad91)")
    out3+=("^sep($GRADIENTS 91...120)");;
    121) out2+=("$GRADIENTS 121...150,^checkout(grad121)")
    out3+=("^tag(grad121)")
    out3+=("^sep($GRADIENTS 121...150)");;
    151) out2+=("$GRADIENTS 151...180,^checkout(grad151)")
    out3+=("^tag(grad151)")
    out3+=("^sep($GRADIENTS 151...180)");;
    181) out2+=("$GRADIENTS 181...210,^checkout(grad181)")
    out3+=("^tag(grad181)")
    out3+=("^sep($GRADIENTS 181...210)");;
    211) out2+=("$GRADIENTS 211...240,^checkout(grad211)")
    out3+=("^tag(grad211)")
    out3+=("^sep($GRADIENTS 211...240)");;
    241) out2+=("$GRADIENTS 241...270,^checkout(grad241)")
    out3+=("^tag(grad241)")
    out3+=("^sep($GRADIENTS 241...270)");;
    271) out2+=("$GRADIENTS 271...300,^checkout(grad271)")
    out3+=("^tag(grad271)")
    out3+=("^sep($GRADIENTS 271...300)");;
    301) out2+=("$GRADIENTS 301...330,^checkout(grad301)")
    out3+=("^tag(grad301)")
    out3+=("^sep($GRADIENTS 301...330)");;
    331) out2+=("$GRADIENTS 331...,^checkout(grad331)")
    out3+=("^tag(grad331)")
    out3+=("^sep($GRADIENTS 331...)");;
    *) : ;;
    esac
    case 1 in
    $(($n<=30))) out2+=("<tt>$(echo ${tablica})</tt>  ${NAME},${COMMAND} '${FROM}' '${TO}';${me}");;
    $((30<$n && $n<=60))) out3+=("<tt>$(echo ${tablica})</tt>  ${NAME},${COMMAND} '${FROM}' '${TO}';$me");;
    $((60<$n && $n<=90))) out3+=("<tt>$(echo ${tablica})</tt>  ${NAME},${COMMAND} '${FROM}' '${TO}';$me");;
    $((90<$n && $n<=120))) out3+=("<tt>$(echo ${tablica})</tt>  ${NAME},${COMMAND} '${FROM}' '${TO}';$me");;
    $((120<$n && $n<=150))) out3+=("<tt>$(echo ${tablica})</tt>  ${NAME},${COMMAND} '${FROM}' '${TO}';$me");;
    $((150<$n && $n<=180))) out3+=("<tt>$(echo ${tablica})</tt>  ${NAME},${COMMAND} '${FROM}' '${TO}';$me");;
    $((180<$n && $n<=210))) out3+=("<tt>$(echo ${tablica})</tt>  ${NAME},${COMMAND} '${FROM}' '${TO}';$me");;
    $((210<$n && $n<=240))) out3+=("<tt>$(echo ${tablica})</tt>  ${NAME},${COMMAND} '${FROM}' '${TO}';$me");;
    $((240<$n && $n<=270))) out3+=("<tt>$(echo ${tablica})</tt>  ${NAME},${COMMAND} '${FROM}' '${TO}';$me");;
    $((270<$n && $n<=300))) out3+=("<tt>$(echo ${tablica})</tt>  ${NAME},${COMMAND} '${FROM}' '${TO}';$me");;
    $((300<$n && $n<=330))) out3+=("<tt>$(echo ${tablica})</tt>  ${NAME},${COMMAND} '${FROM}' '${TO}';$me");;
    $((330<$n))) out3+=("<tt>$(echo ${tablica})</tt>  ${NAME},${COMMAND} '${FROM}' '${TO}';$me");;
    *)
    :
    ;;
    esac
    tablica=()
done < "${GRADIENT_FILE}"

printf '%s\n' "${out[@]}"
printf '%s\n' "${out2[@]}"
printf '%s\n' "${out3[@]}"

printf '%s\n' "${out[@]}" > $CACHEFILE
printf '%s\n' "${out2[@]}" >> $CACHEFILE
printf '%s\n' "${out3[@]}" >> $CACHEFILE

fi