#!/bin/bash
#
# conkyctl
CONKYDIR="$HOME/.config/conky"
BASECONKY="$CONKYDIR/sysinfo_mbcolor.conkyrc"
SESSIONFILE="$CONKYDIR/conky-sessionfile"

update_session () {
    :> "${SESSIONFILE}"
    while read -r pid b c confpath 
    do
    echo "$b $c ${confpath} & sleep .5" >> ${SESSIONFILE}
    done < <(pgrep -af "conky .*mbcolor")
}

transparent_all () {
    sd "own_window_transparent.*$" "own_window_transparent = ${1}," ${CONKYDIR}/*mbcolor.conkyrc
}
transparent () {
    sd "own_window_transparent.*$" "own_window_transparent = ${1}," ${2}
}
win_bgcolor_all () {
    sd "own_window_colour .*=.*$" "own_window_colour = '${1}'," ${CONKYDIR}/*mbcolor.conkyrc
    sd "own_window_transparent.*$" "own_window_transparent = false," ${CONKYDIR}/*mbcolor.conkyrc
}
win_bgcolor () {
    sd "own_window_colour .*=.*$" "own_window_colour = '${1}'," ${2}
    sd "own_window_transparent.*$" "own_window_transparent = false," ${2}
}
draw_borders_all () {
    sd "draw_borders.*$" "draw_borders = ${1}," ${CONKYDIR}/*mbcolor.conkyrc
}
draw_borders () {
    sd "draw_borders.*$" "draw_borders = ${1}," ${2}
}
stippled_borders_all () {
    sd "stippled_borders.*$" "stippled_borders = ${1}," ${CONKYDIR}/*mbcolor.conkyrc
    sd "draw_borders.*$" "draw_borders = true," ${CONKYDIR}/*mbcolor.conkyrc
}
stippled_borders () {
    sd "stippled_borders.*$" "stippled_borders = ${1}," ${2}
    sd "draw_borders.*$" "draw_borders = true," ${2}
}
default_color_all () {
    sd "default_color .*=.*$" "default_color = '${1}'," ${CONKYDIR}/*mbcolor.conkyrc
}
color0_all () {
    sd "color0 .*=.*$" "color0 = '${1}'," ${CONKYDIR}/*mbcolor.conkyrc
}
color1_all () {
    sd "color1 .*=.*$" "color1 = '${1}'," ${CONKYDIR}/*mbcolor.conkyrc
}
color2_all () {
    sd "color2 .*=.*$" "color2 = '${1}'," ${CONKYDIR}/*mbcolor.conkyrc
}
default_color () {
    sd "default_color .*=.*$" "default_color = '${1}'," ${2}
}
color0 () {
    sd "color0 .*=.*$" "color0 = '${1}'," ${2}
}
color1 () {
    sd "color1 .*=.*$" "color1 = '${1}'," ${2}
}
color2 () {
    sd "color2 .*=.*$" "color2 = '${1}'," ${2}
}
basefont_inc_all () {
    read FONTDEF <<< "$(grep "font .*=.*,$" ${BASECONKY} | cut -d'=' -f2,3 |cut -d"'" -f2)"
    FONT=${FONTDEF%:*}
    FONTSIZE=${FONTDEF#*=}
    ((FONTSIZE++))
    sd "font .*=.*,$" "font = '${FONT}:size=${FONTSIZE}'," ${CONKYDIR}/*mbcolor.conkyrc
}
basefont_dec_all () {
    read FONTDEF <<< "$(grep "font .*=.*,$" ${BASECONKY} | cut -d'=' -f2,3 |cut -d"'" -f2)"
    FONT=${FONTDEF%:*}
    FONTSIZE=${FONTDEF#*=}
    ((FONTSIZE--))
    sd "font .*=.*,$" "font = '${FONT}:size=${FONTSIZE}'," ${CONKYDIR}/*mbcolor.conkyrc
}
basefont_inc () {
    read FONTDEF <<< "$(grep "font .*=.*,$" ${1} | cut -d'=' -f2,3 |cut -d"'" -f2)"
    FONT=${FONTDEF%:*}
    FONTSIZE=${FONTDEF#*=}
    ((FONTSIZE++))
    sd "font .*=.*,$" "font = '${FONT}:size=${FONTSIZE}'," ${1}
}
basefont_dec () {
    read FONTDEF <<< "$(grep "font .*=.*,$" ${1} | cut -d'=' -f2,3 |cut -d"'" -f2)"
    FONT=${FONTDEF%:*}
    FONTSIZE=${FONTDEF#*=}
    ((FONTSIZE--))
    sd "font .*=.*,$" "font = '${FONT}:size=${FONTSIZE}'," ${1}
}
shades_all () {
    case "$1" in
        shadow)
        sd "draw_shades .*=.*,$" "draw_shades = true," ${CONKYDIR}/*mbcolor.conkyrc
        sd "draw_outline .*=.*,$" "draw_outline = false," ${CONKYDIR}/*mbcolor.conkyrc
        ;;
        outline)
        sd "draw_shades .*=.*,$" "draw_shades = false," ${CONKYDIR}/*mbcolor.conkyrc
        sd "draw_outline .*=.*,$" "draw_outline = true," ${CONKYDIR}/*mbcolor.conkyrc
        ;;
        *)
        sd "draw_shades .*=.*,$" "draw_shades = false," ${CONKYDIR}/*mbcolor.conkyrc
        sd "draw_outline .*=.*,$" "draw_outline = false," ${CONKYDIR}/*mbcolor.conkyrc
        ;;
    esac
}
shades () {
    case "$1" in
        shadow)
        sd "draw_shades .*=.*,$" "draw_shades = true," ${2}
        sd "draw_outline .*=.*,$" "draw_outline = false," ${2}
        ;;
        outline)
        sd "draw_shades .*=.*,$" "draw_shades = false," ${2}
        sd "draw_outline .*=.*,$" "draw_outline = true," ${2}
        ;;
        *)
        sd "draw_shades .*=.*,$" "draw_shades = false," ${2}
        sd "draw_outline .*=.*,$" "draw_outline = false," ${2}
        ;;
    esac
}
pos () {
    def_x=${conky_gap_x:-30}
    def_y=${conky_gap_y:-60}
    
    sd "alignment .*=.*,$" "alignment = '${1}'," ${2}
    ### jak po środku to wyzerować gaps
    case "$1" in
        top_left|top_right|bottom_left|bottom_right)
        sd "gap_x .*=.*,$" "gap_x = ${def_x}," ${2}
        sd "gap_y .*=.*,$" "gap_y = ${def_y}," ${2}
        ;;
        top_middle|bottom_middle)
        sd "gap_x .*=.*,$" "gap_x = 0," ${2}
        sd "gap_y .*=.*,$" "gap_y = ${def_y}," ${2}
        ;;
        middle_left|middle_right)
        sd "gap_x .*=.*,$" "gap_x = ${def_x}," ${2}
        sd "gap_y .*=.*,$" "gap_y = 0," ${2}
        ;;
        middle_middle)
        sd "gap_x .*=.*,$" "gap_x = 0," ${2}
        sd "gap_y .*=.*,$" "gap_y = 0," ${2}
        ;;
    esac
}
gap_x () {
    sd "gap_x .*=.*,$" "gap_x = ${1}," ${2}
}
gap_y () {
    sd "gap_y .*=.*,$" "gap_y = ${1}," ${2}
}
startone () {
    conky -c "${1}"
    update_session
}
startall () {
    for i in "$CONKYDIR"/*_mbcolor*; do
    if pgrep -af "conky .*${i}" >/dev/null; then
    :
    else
    conky -c "${i}"
    fi
    done
    update_session
}
kill () {
    pkill -f "conky .*${1}"
    update_session
}

single () {
    me="colorizer-conky -s"
    read POS <<< "$(grep alignment ${1} | cut -d'=' -f2 | cut -d"'" -f2)"
    read GAPX <<< "$(grep gap_x ${1} | cut -d'=' -f2 | cut -d"," -f1)"
    read GAPY <<< "$(grep gap_y ${1} | cut -d'=' -f2 | cut -d"," -f1)"
    
    read WINTRANS <<< "$(grep own_window_transparent ${1} | cut -d'=' -f2 | cut -d"," -f1)"
    read FONTDEF <<< "$(grep "font .*=.*,$" ${1} | cut -d'=' -f2,3 |cut -d"'" -f2)"
    FONT=${FONTDEF%:*}
    FONTSIZE=${FONTDEF#*=}
    read WINCLR <<< "$(grep own_window_colour ${1} | cut -d'=' -f2 | cut -d"'" -f2)"
    [[ $WINCLR =~ ^#.* ]] && : || WINCLR="#${1}"
    
    read CLR <<< "$(grep default_color ${1} | cut -d'=' -f2 | cut -d"'" -f2)"
    [[ $CLR =~ ^#.* ]] && : || CLR="#${CLR}"
    read CLR0 <<< "$(grep color0 ${1} | cut -d'=' -f2 | cut -d"'" -f2)"
    [[ $CLR0 =~ ^#.* ]] && : || CLR0="#${CLR0}"
    read CLR1 <<< "$(grep color1 ${1} | cut -d'=' -f2 | cut -d"'" -f2)"
    [[ $CLR1 =~ ^#.* ]] && : || CLR1="#${CLR1}"
    read CLR2 <<< "$(grep color2 ${1} | cut -d'=' -f2 | cut -d"'" -f2)"
    [[ $CLR2 =~ ^#.* ]] && : || CLR2="#${CLR2}"
    
    read BORD <<< "$(grep draw_borders ${1} | cut -d'=' -f2 | cut -d"," -f1)"
    read STIP <<< "$(grep stippled_borders ${1} | cut -d'=' -f2 | cut -d"," -f1)"
    
    [[ "$WINTRANS" == false ]] && BG="bgcolor='$WINCLR'" || BG=""
    
    out+=("^sep(POSITION)")
    out+=("Alignment [ $POS ],^checkout(pos)")
    out2+=("^tag(pos)")
    out2+=("^sep(POSITION)")
    out2+=("TopLeft,conkyctl pos top_left ${1};$me")
    out2+=("Top,conkyctl pos top_middle ${1};$me")
    out2+=("TopRight,conkyctl pos top_right ${1};$me")
    out2+=("Left,conkyctl pos middle_left ${1};$me")
    out2+=("Center,conkyctl pos middle_middle ${1};$me")
    out2+=("Right,conkyctl pos middle_right ${1};$me")
    out2+=("BottomLeft,conkyctl pos bottom_left ${1};$me")
    out2+=("Bottom,conkyctl pos bottom_middle ${1};$me")
    out2+=("BottomRight,conkyctl pos bottom_right ${1};$me")
    out+=("HORIZONTAL GAP [ $GAPX ],^checkout(gapx)")
    out2+=("^tag(gapx)")
    out2+=("^sep(HORIZONTAL_GAP)")
    for i in 0 10 20 30 40 60 80 100 120 140 160 180 200 220 240 260 280 300 320 340 360; do
    out2+=("${i},conkyctl gap_x ${i} ${1};$me")
    done
    out+=("VERTICAL GAP [ $GAPY ],^checkout(gapy)")
    out2+=("^tag(gapy)")
    out2+=("^sep(VERTICAL_GAP)")
    for i in 0 10 20 30 40 60 80 100 120 140 160 180 200 220 240 260 280 300 320 340 360; do
    out2+=("${i},conkyctl gap_y ${i} ${1};$me")
    done
    
    out+=("^sep(COLORS)")
    [[ "$CLR" != "#" ]] && out+=("<small><span bgcolor='$CLR'>   </span><span ${BG} fgcolor='$CLR'>  AbCd  </span></small>  Default color,^pipe(mbclr '$CLR' conkyctl default_color DEFAULT_COLOR '$me' ${1})")
    [[ "$CLR0" != "#" ]] && out+=("<small><span bgcolor='$CLR0'>   </span><span ${BG} fgcolor='$CLR0'>  AbCd  </span></small>  Color 0,^pipe(mbclr '$CLR0' conkyctl color0 COLOR_0 '$me' ${1})")
    [[ "$CLR1" != "#" ]] && out+=("<small><span bgcolor='$CLR1'>   </span><span ${BG} fgcolor='$CLR1'>  AbCd  </span></small>  Color 1,^pipe(mbclr '$CLR1' conkyctl color1 COLOR_1 '$me' ${1})")
    [[ "$CLR2" != "#" ]] && out+=("<small><span bgcolor='$CLR2'>   </span><span ${BG} fgcolor='$CLR2'>  AbCd  </span></small>  Color 2,^pipe(mbclr '$CLR2' conkyctl color2 COLOR_2 '$me' ${1})")
    out+=("^sep()")
    if [ "$WINTRANS" == false ];then
        out+=("<small><span bgcolor='$WINCLR'>   </span><span ${BG} fgcolor='$WINCLR'>  AbCd  </span></small>  Background,^checkout(transsingle)")
    else
        out+=("<small><span>transparent </span></small>  Background,^checkout(transsingle)")
    fi
    out2+=("^tag(transsingle)")
    out2+=("^sep(TRANSPARENT?)")
    out2+=("Yes,conkyctl transparent true ${1};$me")
    out2+=("No,conkyctl transparent false ${1};$me")
    out2+=("^sep()")
    out2+=("<small><span bgcolor='$WINCLR'>        </span></small>  Background color,^pipe(mbclr '$WINCLR' conkyctl win_bgcolor WIN_BGCOLOR '$me' ${1})")
    
    out+=("FONTS [ $FONT $FONTSIZE ],^checkout(fontsingle)")
    out2+=("^tag(fontsingle)")
    out2+=("^sep(BASE_FONT)")
    # if "$1" not quote
    out2+=("Set custom basefont...,mb-setfont conky_single ${1};$me")
    out2+=("^sep()")
    # fi
    out2+=("<tt>++</tt> Increase,conkyctl basefont_inc ${1};$me")
    out2+=("<tt>--</tt> Decrease,conkyctl basefont_dec ${1};$me")
    out2+=("^sep(Outline/Shadow)")
    out2+=("No,conkyctl shades no ${1};$me")
    out2+=("^sep()")
    out2+=("Draw shades,conkyctl shades shadow ${1};$me")
    out2+=("Draw outline,conkyctl shades outline ${1};$me")
    
    
    out+=("^sep()")
    out+=("BORDERS [ $BORD $STIP ],^checkout(bordersingle)")
    out2+=("^tag(bordersingle)")
    out2+=("^sep(DRAW BORDERS?)")
    out2+=("Yes,conkyctl draw_borders true ${1};$me")
    out2+=("No,conkyctl draw_borders false ${1};$me")
    out2+=("^sep(STIPPLED BORDERS?)")
    out2+=("0,conkyctl stippled_borders 0 ${1};$me")
    out2+=("1,conkyctl stippled_borders 1 ${1};$me")
    out2+=("2,conkyctl stippled_borders 2 ${1};$me")
    out2+=("3,conkyctl stippled_borders 3 ${1};$me")
    out2+=("4,conkyctl stippled_borders 4 ${1};$me")
    
    out+=("^sep()")
    out+=("EDIT...,xdg-open ${1}")
    out+=("^sep()")
    out+=("Kill ${1##*/},conkyctl kill ${1##*/};${me}")
    
    printf '%s\n' "${out[@]}"
    printf '%s\n' "${out2[@]}"
    exit 0
}

case "$1" in
    transparent_all) transparent_all "$2";;
    transparent) transparent "$2" "$3";;
    win_bgcolor_all) win_bgcolor_all "$2";;
    win_bgcolor) win_bgcolor "$2" "$3";;
    draw_borders_all) draw_borders_all "$2" ;;
    draw_borders) draw_borders "$2" "$3";;
    stippled_borders_all) stippled_borders_all "$2" ;;
    stippled_borders) stippled_borders "$2" "$3";;
    default_color_all) default_color_all "$2";;
    color0_all) color0_all "$2";;
    color1_all) color1_all "$2";;
    color2_all) color2_all "$2";;
    default_color) default_color "$2" "$3";;
    color0) color0 "$2" "$3";;
    color1) color1 "$2" "$3";;
    color2) color2 "$2" "$3";;
    basefont_inc_all) basefont_inc_all;;
    basefont_dec_all) basefont_dec_all;;
    basefont_inc) basefont_inc "$2";;
    basefont_dec) basefont_dec "$2";;
    setfont_all) setfont_all;;
    serfont) setfont;;
    shades_all) shades_all "$2";;
    shades) shades "$2" "$3";;
    single) single "$2";;
    pos) pos "$2" "$3";;
    gap_x) gap_x "$2" "$3";;
    gap_y) gap_y "$2" "$3";;
    startone) startone "$2";;
    startall) startall;;
    kill) kill "$2";;
    reset) reset ;;
    *) : ;;
esac