#!/bin/bash

ME=${0##*/}

FILE=$HOME/.conkyrc

white_THEME="
COLOR_1=white
COLOR_2=ffffff
COLOR_3=ffffff
COLOR_4=yellow
COLOR_8=e0e0e0
COLOR_9=fefefe
GRAPH_C=fefefe"

light_THEME="
COLOR_1=ffffff
COLOR_2=ffffff
COLOR_3=ffffff
COLOR_4=yellow
COLOR_8=77ccff
COLOR_9=5599cc
GRAPH_C=5599cc"

light_grey_THEME="
COLOR_1=808080
COLOR_2=808080
COLOR_3=white
COLOR_4=white
COLOR_8=white
COLOR_9=white
GRAPH_C=fffffe"

light_red_THEME="
COLOR_1=ffffff
COLOR_2=ffffff
COLOR_3=ffffff
COLOR_4=yellow
COLOR_8=993333
COLOR_9=998888
GRAPH_C=993333"

light_blue_THEME="
COLOR_1=ffffff
COLOR_2=ffffff
COLOR_3=ffffff
COLOR_4=yellow
COLOR_8=6666ff
COLOR_9=yellow
GRAPH_C=5555ff"

light_green_THEME="
COLOR_1=ffffff
COLOR_2=ffffff
COLOR_3=ffffff
COLOR_4=yellow
COLOR_8=88ff88
COLOR_9=yellow
GRAPH_C=55ff55"

dark_THEME="
COLOR_1=505050
COLOR_2=505050
COLOR_3=505050
COLOR_4=505050
COLOR_8=808080
COLOR_9=103040
GRAPH_C=103040"

darker_blue_THEME="
COLOR_1=505050
COLOR_2=505050
COLOR_3=0000ff
COLOR_4=505050
COLOR_8=0000ff
COLOR_9=0000ff
GRAPH_C=0000ff"

dark_blue_THEME="
COLOR_1=0000dd
COLOR_2=505050
COLOR_3=0000ff
COLOR_4=505050
COLOR_8=000000
COLOR_9=000000
GRAPH_C=0000ff"

dark_red_THEME="
COLOR_1=dd0000
COLOR_2=505050
COLOR_3=a00000
COLOR_4=505050
COLOR_8=dd0000
COLOR_9=000000
GRAPH_C=ff0000"

dark_purple_THEME="
COLOR_1=800080
COLOR_2=505050
COLOR_3=a000a0
COLOR_4=505050
COLOR_8=a000a0
COLOR_9=000000
GRAPH_C=900090"

black_THEME="
COLOR_1=000000
COLOR_2=000000
COLOR_3=000000
COLOR_4=000000
COLOR_8=000000
COLOR_9=000000
GRAPH_C=000090"


SED_ARG=-i
SUBST_SUFFIX=
QUIET=

usage() {
    local ret=${1:-0}
    local width=$(stty size 2>/dev/null | cut -d" " -f2)
    : ${width:=70}
    cat <<Usage
Usage: $ME [<theme>|cycle|random|toggle]

Change the colors in the ~/.conkyrc.  Meta-themes:

    cycle      Cycle through all light or all dark themes
    random     Pick a theme at random
    toggle     Toggle between light themes and dark themes

The current themes are:

$(list_themes | column -c 68 | sed 's/^/  /')

Options:
    -f --file=<file>   Makes change to <file> instead of .conkyrc
    -h --help          Show this help
    -p --pretend       Show changes but don't do anything
    -r --reload        Reload conky (usually this is not needed)
    -q --quiet         Only print error messages
Usage

    exit $ret
}

main() {

    local arg val="unknown"
    while [ $# -gt 0 -a -n "$1" -a -z "${1##-*}" ]; do
        arg=${1#-}
        shift

        case $arg in
            -file|[f])
                [ $# -lt 1 ] && fatal "Expected a parameter after: -$arg"
                val=$1
                [ -n "$val" -a -z "${val##-*}" ] \
                    && fatal "Suspicious argument after -$arg: $val"
                shift           ;;

            *=*) val=${arg#*=} ;;
            *)   val="unknown" ;;
        esac

        case $arg in
               -file|f) FILE=$val                       ;;
               -help|h) usage                           ;;
            -pretend|p) SUBST_SUFFIX=p ; SED_ARG=-n     ;;
              -quiet|q) QUIET=true                      ;;
             -reload|r) RELOAD=true                     ;;
                     *) fatal "Unknown argument: -$arg" ;;
        esac
    done

    [ $# -lt 1 ] && usage
    [ $# -gt 1 ] && fatal "Expected at most one command line argument"

    test -e "$FILE" || fatal "Could not find file: $FILE"
    test -r "$FILE" || fatal "Can not read file: $FILE"
    test -w "$FILE" || fatal "Can not write to file: $FILE"

    local cmd_theme=$1

    case $cmd_theme in
        toggle) local old_theme=$(guess_old_theme)
                qsay "old theme: $old_theme"
                cmd_theme=$(toggle_theme $old_theme)             ;;
         cycle) local old_theme=$(guess_old_theme)
                qsay "old theme: $old_theme"
                cmd_theme=$(cycle_theme $old_theme)              ;;
        random) cmd_theme=$(list_themes $FILE | shuf | head -n1) ;;
    esac

    #case $cmd_theme in
    #    light-*) eval "$light_THEME" ;;
    #esac

    theme=${cmd_theme//-/_}

    eval local theme_var=${theme}_THEME
    #echo $theme_var
    #eval echo \$$theme_var
    eval local vars=\$$theme_var
    #echo $vars

    [ "$vars" ] || fatal "Unrecognized theme: $cmd_theme"
    eval "$vars"

    [ "$SUBST_SUFFIX" ] || grep  -q "^\s*#\s*Color-theme: " "$FILE" || \
        sed -i "1i# Color-theme: $cmd_theme" "$FILE"

    sed  $SED_ARG -r \
         -e "s/^(#\s+Color-theme: ).*/\1$cmd_theme/$SUBST_SUFFIX" \
        -e "s/^(default_color ).*/\1$COLOR_1/$SUBST_SUFFIX" \
        -e "s/^(color2 ).*/\1$COLOR_2/$SUBST_SUFFIX" \
        -e "s/^(color3 ).*/\1$COLOR_3/$SUBST_SUFFIX" \
        -e "s/^(color4 ).*/\1$COLOR_4/$SUBST_SUFFIX" \
        -e "s/^(color8 ).*/\1$COLOR_8/$SUBST_SUFFIX" \
        -e "s/^(color9 ).*/\1$COLOR_9/$SUBST_SUFFIX" \
        -e "/^[$]\{.*\{\w+graph / s/ [0-9a-fA-F]{6}\>/ $GRAPH_C/g$SUBST_SUFFIX" \
        "$FILE"

    if [ -z "$PRETEND" -a -n "$RELOAD" ]; then
        killall conky
        conky
        sleep 0.5
        wmctrl -r "Conky ($(hostname))" -b add,below
    fi

    qsay "Set theme to: $cmd_theme"
}

warn() {
    echo "$ME warning: $*" >&2
}

fatal() {
    echo "$ME Error: $*" >&2
    exit 3
}

qsay() {
    [ "$QUIET" ] && return
    echo "$*"
}

guess_old_theme() {
    local file=$1
    sed -r -n 's/^#\s+Color-theme: ([A-Za-z0-9-]+).*/\1/p' "$FILE" | head -n1
}

toggle_theme() {
    local old=$1

    case $old in
          white) echo dark         ;;
     light-grey) echo dark         ;;
          light) echo dark         ;;
     light-blue) echo dark-blue    ;;
    light-green) echo dark-purple  ;;
      light-red) echo dark-red     ;;
           dark) echo light        ;;
    darker-blue) echo light        ;;
      dark-blue) echo light-blue   ;;
    dark-purple) echo light-green  ;;
       dark-red) echo light-red    ;;
              *) echo white        ;;
    esac
}


cycle_theme() {
    local old=$1

    case $old in
          white) echo light-grey    ;;
     light-grey) echo light         ;;
          light) echo light-blue    ;;
     light-blue) echo light-green   ;;
    light-green) echo light-red     ;;
      light-red) echo white         ;;
           dark) echo darker-blue   ;;
    darker-blue) echo dark-blue     ;;
      dark-blue) echo dark-purple   ;;
    dark-purple) echo dark-red      ;;
       dark-red) echo dark          ;;
              *) echo white         ;;
    esac
}

list_themes() {
    sed -n -r "s/^([a-z_0-9]+)_THEME=.*/\1/p" $0 | tr '_' '-' | sort
}

main "$@"

exit 0
