#!/bin/ksh

trap 'trap - TERM; kill 0' INT TERM QUIT EXIT

FONT="-zevv-peep-medium-r-normal--14-130-75-75-c-70-iso8859-15"
DEFAULT_FG="#222222"
DEFAULT_BG="#CCCCCC"
MONITOR_FG="#8dbcdf"
MONITOR_BG="#333232"
FOCUSED_MONITOR_FG="#b1d0e8"
FOCUSED_MONITOR_BG="#144b6c"
FREE_FG="#737171"
FREE_BG="#333232"
FOCUSED_FREE_FG="#000000"
FOCUSED_FREE_BG="#504e4e"
OCCUPIED_FG="#a7a5a5"
OCCUPIED_BG="#333232"
FOCUSED_OCCUPIED_FG="#d6d3d2"
FOCUSED_OCCUPIED_BG="#504e4e"
URGENT_FG="#f15d66"
URGENT_BG="#333232"
FOCUSED_URGENT_FG="#501d1f"
FOCUSED_URGENT_BG="#d5443e"
STATE_FG="#89b09c"
STATE_BG="#333232"
TITLE_FG="#a8a2c0"
TITLE_BG="#333232"
SYS_FG="#b1a57d"
SYS_BG="#333232"

BG="%{F#CCCCCC}"
FG="%{F#222222}"
CLEAR="%{F-}%{B-}"

_battery() {
    ADAPTER=$(apm -a)
    if [ $ADAPTER = 0 ] ; then
        printf ">"
    elif [ $ADAPTER = 1 ] ; then
        printf "="
    else
        printf "?"
    fi
    BATTERY=$(apm -l)
    if [ $BATTERY -gt 66 ] ; then
        printf "$BATTERY%%"
    elif [ $BATTERY -gt 33 ] ; then
        printf "$BATTERY%%"
    else
        printf "$BATTERY%%"
    fi
    BATTERY=$(apm -m)
    [[ "$BATTERY" != "unknown" ]] && printf " ($BATTERY m)"
}

_cputemp() {
    printf "$(sysctl -n hw.sensors.cpu0.temp0 | cut -d"." -f1)°C"
}

_netload() {
    set -A if_load $(ifstat -n -i trunk0 -b 0.1 1 | sed '1,2d')
    printf "In: ${if_load[0]} kb/s Out: ${if_load[1]} kb/s "
}

_backlight() {
    LIGHT=$(xbacklight | cut -d"." -f1)
    printf "$LIGHT%%"
}

_volume() {
    MUTE=$(mixerctl outputs.master.mute | awk -F '=' '{ print $2 }')
    #LSPK=$(($(mixerctl outputs.master | awk -F '(=|,)' '{ print $2 }')*100/255))
    RSPK=$(($(mixerctl outputs.master | awk -F '(=|,)' '{ print $2 }')*100/255))
    if [ "$MUTE" = "on" ] ; then
        printf "muted"
    else
        printf "$RSPK%%"
    fi
}

_wlan() {
    WLANSTAT=$(ifconfig iwn0 | awk '/status:/ { print $2 }')
    WLANID=$(ifconfig iwn0  | awk '/(nwid|join)/ { print $3 }')
    if [ "$WLANSTAT" = "active" ] ; then
        printf '%s' "$WLANID"
    else
        printf '%s' "no-wifi"
    fi
}
_datetime() {
    printf '%s' "$(date +"%Y-%m-%d %H:%M")"
}

num_mon=$(bspc query -M | wc -l)

# INITIAL SETUP
cputemp=$(_cputemp)
battery=$(_battery)
date=$(_datetime)
backlight=$(_backlight)
volume=$(_volume)
wlan=$(_wlan)
netload=$(_netload)
datetime=$(_datetime)

mkfifo /tmp/panel-fifo
bspc subscribe report > /tmp/panel-fifo &

# TICK COUNTER FOR AUTOMATIC UPDATES
while true;
do
    TICK=$(($TICK + 1))
    case $TICK in
        1)
            printf '%s\n' "UBAT" > "$PANEL_FIFO"
            printf '%s\n' "UBL"  > "$PANEL_FIFO"
            printf '%s\n' "UNETLOAD"  > "$PANEL_FIFO"

            ;;
        2) 
            printf '%s\n' "UCPU" > "$PANEL_FIFO"
            ;;
        5) 
            printf '%s\n' "UWLAN" > "$PANEL_FIFO"
            ;;
        10) 
            printf '%s\n' "UDATE" > "$PANEL_FIFO"
            TICK=0
            ;;
    esac
    sleep 5
done &

# READ FIFO EVENTS
while read -r line;
do
    case $line in
        UVOL)
            volume=$(_volume) ;;
        UDATE)
            datetime=$(_datetime) ;;
        UBAT)
            battery=$(_battery) ;;
        UBL)
            backlight=$(_backlight) ;;
        UCPU)
            cputemp=$(_cputemp) ;;
        UWLAN)
            wlan=$(_wlan) ;;
        UNETLOAD)
            netload=$(_netload) ;;
        S*)
            sys=${line#?}
            ;;
        W*)
            # BSPWM STATE
            wm=
            IFS=':'
            set -- ${line#?}
            while [ $# -gt 0 ] ; do
                item=$1
                name=${item#?}
                case $item in
                    [mM]*)
                        case $item in
                            m*)
                                # monitor
                                FG=$MONITOR_FG
                                BG=$MONITOR_BG
                                on_focused_monitor=
                                ;;
                            M*)
                                # focused monitor
                                FG=$FOCUSED_MONITOR_FG
                                BG=$FOCUSED_MONITOR_BG
                                on_focused_monitor=1
                                ;;
                        esac
                        [ $num_mon -lt 2 ] && shift && continue
                        wm="${wm}%{F${FG}}%{B${BG}}%{A:bspc monitor -f ${name}:} ${name} %{A}%{B-}%{F-}"
                        ;;
                    [fFoOuU]*)
                        case $item in
                            f*)
                                # free desktop
                                FG=$FREE_FG
                                BG=$FREE_BG
                                UL=$BG
                                ;;
                            F*)
                                if [ "$on_focused_monitor" ] ; then
                                    # focused free desktop
                                    FG=$FOCUSED_FREE_FG
                                    BG=$FOCUSED_FREE_BG
                                    UL=$BG
                                else
                                    # active free desktop
                                    FG=$FREE_FG
                                    BG=$FREE_BG
                                    UL=$FOCUSED_FREE_BG
                                fi
                                ;;
                            o*)
                                # occupied desktop
                                FG=$OCCUPIED_FG
                                BG=$OCCUPIED_BG
                                UL=$BG
                                ;;
                            O*)
                                if [ "$on_focused_monitor" ] ; then
                                    # focused occupied desktop
                                    FG=$FOCUSED_OCCUPIED_FG
                                    BG=$FOCUSED_OCCUPIED_BG
                                    UL=$BG
                                else
                                    # active occupied desktop
                                    FG=$OCCUPIED_FG
                                    BG=$OCCUPIED_BG
                                    UL=$FOCUSED_OCCUPIED_BG
                                fi
                                ;;
                            u*)
                                # urgent desktop
                                FG=$URGENT_FG
                                BG=$URGENT_BG
                                UL=$BG
                                ;;
                            U*)
                                if [ "$on_focused_monitor" ] ; then
                                    # focused urgent desktop
                                    FG=$FOCUSED_URGENT_FG
                                    BG=$FOCUSED_URGENT_BG
                                    UL=$BG
                                else
                                    # active urgent desktop
                                    FG=$URGENT_FG
                                    BG=$URGENT_BG
                                    UL=$FOCUSED_URGENT_BG
                                fi
                                ;;
                        esac
                        wm="${wm}%{F${FG}}%{B${BG}}%{U${UL}}%{+u}%{A:bspc desktop -f ${name}:} ${name} %{A}%{B-}%{F-}%{-u}"
                        ;;
                    [LTG]*)
                       # layout, state and flags
                       wm="${wm}%{F$STATE_FG}%{B$STATE_BG} ${name} %{B-}%{F-}"
                esac
                shift
            done
            ;;
    esac
    printf "%s\n" "%{l}${wm}%{c}%{r}$cputemp $backlight $volume $wlan $netload $battery $datetime "
done < "$PANEL_FIFO" | lemonbar -f "$FONT" -B "$DEFAULT_FG" -F "$DEFAULT_BG" -u 8 | sh
