#!/bin/bash

COLOR_FOCUSED='#FF202020'
COLOR_FREE='#FFCCCCCC'
COLOR_OCCUPIED='#FF666666'

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

while read -r line ; do
  case $line in
    VS*)
      # volume state
      if [ ${line#??} == "on" ]
      then
        volume_state=`echo -e "%{T2}\uf028"`
      else
        volume_state=`echo -e "%{T2}\uf026"`
      fi
      ;;
    VI*)
      # volume output
      volume="%{T1}${line#??}"
      ;;
    BS*)
      # battery state
      if [ ${line#??} == "Charging" ]
      then
        batt_state=`echo -e "%{T2}\uf0e7"`
      elif [ ${line#??} == "Full" ]
      then
        batt_state=`echo -e "%{T2}\uf0e7"`
      else
        batt_state=""
      fi
      ;;
    BI*)
      # battery output
      charge="${line#??}"
      if [ $charge -lt "12" ]
      then
        batt=`echo -e "%{T2}\uf244"`
      elif [ $charge -lt "37" ]
      then
        batt=`echo -e "%{T2}\uf243"`
      elif [ $charge -lt "62" ]
      then
        batt=`echo -e "%{T2}\uf242"`
      elif [ $charge -lt "87" ]
      then
        batt=`echo -e "%{T2}\uf241"`
      else
        batt=`echo -e "%{T2}\uf240"`
      fi
      charge=`echo -e "%{T1}$charge"`
      ;;
    CD*)
      # date output
      clock_date=`echo -e "%{T2}\uf073 %{T1}${line#??}"`
      ;;
    CT*)
      # clock output
      clock=`echo -e "%{T2}\uf017 %{T1}${line#??}"`
      ;;
    T*)
      # xtitle output
      title="%{T1}${line#?}"
      ;;
    W*)
      # bspwm internal state
      wm_infos=""
      IFS=':'
      set -- ${line#?}
      while [ $# -gt 0 ] ; do
        item=$1
        name=${item#?}
        case $item in
          M*)
            # active monitor
            if [ $num_mon -gt 1 ] ; then
              wm_infos="$wm_infos %{F$COLOR_FOCUSED} ${name} %{F-}:"
            fi
            ;;
          m*)
            # inactive monitor
            if [ $num_mon -gt 1 ] ; then
              wm_infos="$wm_infos %{F$COLOR_OCCUPIED} ${name} %{F-}:"
            fi
            ;;
          O*)
            # focused occupied desktop
            wm_infos="${wm_infos}%{F$COLOR_FOCUSED}%{+u} ${name} %{-u}%{F-}"
            ;;
          F*)
            # focused free desktop
            wm_infos="${wm_infos}%{F$COLOR_FOCUSED}%{+u} ${name} %{-u}%{F-}"
            ;;
          u*)
            # unfocused urgent desktop
            wm_infos="${wm_infos}%{F$COLOR_OCCUPIED}%{+u} ${name} %{-u}%{F-}"
            ;;
          U*)
            # focused urgent desktop
            wm_infos="${wm_infos}%{F$COLOR_FOCUSED}%{+u} ${name} %{-u}%{F-}"
            ;;
          o*)
            # occupied desktop
            wm_infos="${wm_infos}%{F$COLOR_OCCUPIED} ${name} %{F-}"
            ;;
          f*)
            # free desktop
            wm_infos="${wm_infos}%{F$COLOR_FREE} ${name} %{F-}"
            ;;
          h*)
            # urgent desktop
            wm_infos="${wm_infos} ${name} "
            ;;
        esac
        shift
      done
      ;;
  esac
  printf "%s\n" "%{l}${wm_infos}%{c}${title}%{r}${volume_state} ${volume}  ${batt_state} ${batt} ${charge}  ${clock_date} ${clock} "
done
