#!/bin/bash
##
#   Read saved BunsenLabs Conky session file(s) and start the conkys
#
#   Written by damo <damo@bunsenlabs.org> for BunsenLabs Linux, April 2015
#
#   Repackaged for Manjaro by napcok <napcok@gmail.com>, March 2016
#
#   To start the default conky session at login, add the following line
#   to config/openbox/autostart:
#
#      (sleep 2s && mb-conky-session --autostart) &
#
########################################################################
CONKYPATH="$HOME/.config/conky"
SESSIONFILE="$CONKYPATH/conky-sessionfile"
USAGE="\vUSAGE:\tmb-conky-session [OPTION(S)]...FILES
\n\tWith no command argument, the script uses the default
\t\"\$CONKYPATH/conky-session\" sessionfile
\vOPTIONS:\n\t--default\t: specify default sessionfile
\t--autostart\t: no \"kill conky\" option asked for
\tpath/to/sessionfile1  /path/to/sessionfile2 etc
\vEXAMPLES:
\tRun specified sessionfile at login:
\t\"mb-conky-session --autostart /path/to/sessionfile\"
\v\tRun default sessionfile, without killing running conkys:
\t\"mb-conky-session --autostart\"
\v\tRun several conky sessionfiles (option to kill conkys first):
\t\"mb-conky-session --default sessionfile1 sessionfile2 etc\""

### DIALOG VARIABLES
DLG="yad --center --undecorated --borders=20 "
TITLE="Mabox Conky Session"
WINICON="--window-icon=distributor-logo-mabox"
OK="--button=OK:0"
case $LANG in
    pl*)
        CANCEL="--button=Anuluj:1"
        KILL_FIRST="Czy zamknąć najpierw działające Conky?"
    ;;
    *)
        CANCEL="--button=Cancel:1"
        KILL_FIRST="Kill running conkys first?"
    ;;
esac


########################################################################

findArgs(){     # get command args (switches & sessionfile paths)
    i=0
    for arg in "$@";do
        if [[ $arg = "--default" ]]; then
            arg="$SESSIONFILE"
        fi
        if [[ $arg = "--autostart" ]]; then
            NOKILL=1    # run from autostart, so don't ask to kill conkys
        fi
        if [[ -f  $arg ]]; then # sessionfile exists
            rawArr[$i]="$arg"       # add sessionfiles to array
            i=$(($i+1))
        fi
    done
    # check if sessionfiles were passed to mb-conky-session
    if (( ${#rawArr[@]} != 0 )); then
        # remove duplicate args
        sessArr=(`printf "%s\n" "${rawArr[@]}" | sort -u`)
        if (( $NOKILL == 0 )); then
            killConkys
        fi
        for SESSION in "${sessArr[@]}";do # run the conkys in the sessionfiles
            source "$SESSION"
	    done
        cat "$1" > "$SESSIONFILE"
    else    # --autostart used, but no sessionfiles passed to bl-conkyzen
        if [[ -f $SESSIONFILE ]] && (( $NOKILL == 1 )); then
            source "$SESSIONFILE"   # use conky-sessionfile
        else
            echo -e "ERROR: sessionfile \"$SESSIONFILE\" not found. Using default" >&2
            conky -c $HOME/.conkyrc     # run default conky
            exit 0
        fi
    fi
}

killConkys(){
    if pidof conky &>/dev/null; then
        $DLG $WINICON --title="$TITLE" --text="$KILL_FIRST"
        if (( $? == 0 )); then            # kill all conkys
            killall conky && sleep 0.2s
        fi
    fi
}

NOKILL=0
if (( $# == 0 )); then    # if no args, then run default sessionfile
    killConkys
    if [[ ! -f "$SESSIONFILE" ]]; then     # run default conky
        echo -e "ERROR: sessionfile \"$SESSIONFILE\" not found. Using default" >&2
        conky -c $HOME/.conkyrc
        exit 0
    else
        source "$SESSIONFILE"
    fi
elif [[ $1 = "-h" ]] || [[ $1 = "--help" ]]; then
    echo -e "$USAGE"
    exit 0
else 
    findArgs $@    # get the sessionfile paths from the command args
fi

exit 0