#!/bin/zsh

# main interval in seconds
INTERVAL=0.5

# path to the icons
DZEN_ICONPATH='/home/luc/.share/icons/dzen'

# colors definition
RED='#FF0000'
ORANGE='#FF9900'
GREEN='#00FF00'
WHITE='#FFFFFF'

#SEPARATOR='^p(3)^r(3x3)^p(3)'
SEPARATOR=' - '
###################
## Date and Hour ##
###################

HOUR_FORMAT='%H:%M:%S'
DATE_FORMAT='%A %d %B %Y'

fdate() {
  date +$DATE_FORMAT
}

fhour() {
  date +$HOUR_FORMAT
}

##############
## Cpu temp ##
##############

fcputemp() {
  # print -n `sensors | grep -A 1 'Core0 Temp' | cut -c15-16 | sed '/^$/d'`
  TEMP=`sensors | grep -A 1 'temp1' | cut -c15-16 | sed '/^$/d'`
  if [[ $TEMP > 69 ]]
  then
    ICOTEMP="${DZEN_ICONPATH}/temp_high.xbm"
    TEMPCOLOR=$RED
  else
    if [[ $TEMP > 54 ]]
    then
      ICOTEMP="${DZEN_ICONPATH}/temp_med.xbm"
      TEMPCOLOR=$ORANGE
    else
      ICOTEMP="${DZEN_ICONPATH}/temp_low.xbm"
      TEMPCOLOR=$GREEN
    fi
  fi
  print -n `echo "^fg(${TEMPCOLOR})^i($ICOTEMP)^fg() $TEMP C"`
}

##################
## Volume gauge ##
##################

fvolume() {
  if [[ `amixer -c0 get Master | awk '/^ Mono/ { print $6 }'| sed -e "s/\[//" | sed -e "s/\]//"` == "on" ]]
  then
    VOLUME=`amixer -c0 get Master | awk '/^ Mono/ { print $4 }' | sed -e "s/\[//" | sed -e "s/\]//" | sed -e "s/%//"`
  else
    VOLUME=0
  fi
  if [[ $VOLUME > 66 || $VOLUME == 100 ]]
  then
    ICOVOLUME="${DZEN_ICONPATH}/vol-hi.xbm"
  else
    if [[ $VOLUME > 33 ]]
    then
      ICOVOLUME="${DZEN_ICONPATH}/vol-med.xbm"
    else
      if [[ $VOLUME > 0 ]]
      then
       	ICOVOLUME="${DZEN_ICONPATH}/vol-low.xbm"
      else
       	ICOVOLUME="${DZEN_ICONPATH}/vol-mute.xbm"
      fi
    fi
  fi
  print -n `echo "^i($ICOVOLUME) ${VOLUME}%"`
}

###############
## Bandwidth ##
###############

# network interface
finterface() {
  if [[ `sudo ethtool eth0 | awk '/Link detected/ { print $3 }'` == "no" ]]
  then
    if [[ `sudo iwconfig wlan0 | awk '/ESSID:off/ { print $4 }'` == "ESSID:off/any" ]]
    then
      LINK=0
      INTERFACE=""
    else
      INTERFACE="wlan0"
      LINK=1
    fi
  else
    INTERFACE="eth0"
    LINK=1
  fi
}

fbwup() {
  if [[ $LINK == 1 ]]
  then
    BW_UP=`ifstat -i $INTERFACE 0.1 1 | awk '/[0-9]*\.[0-9]*/ { print $2 }'`
  else
    BW_UP=""
  fi
}

fbwdown() {
  if [[ $LINK == 1 ]]
  then
    BW_DOWN=`ifstat -i $INTERFACE 0.1 1 | awk '/[0-9]*\.[0-9]*/ { print $1 }'`
  else
    BW_DOWN=""
  fi
}

#############
## Battery ##
#############
fac() {
  if [[ `acpi -a | awk '/Adapter/ { print $3 }'` == "on-line" ]]
  then
    ICOAC="${DZEN_ICONPATH}/ac.xbm"
  else
    ICOAC=""
  fi
  print -n `echo "^i($ICOAC)"`
}

fbat() {
  BAT=`acpi -b | awk '/Battery/ { print $4 }' | sed -e "s/[%,]//g"`
  BAT=$(print -n $BAT)
  if [[ $BAT > -1 ]]
  then
    if [[ $BAT > 70 || $BAT == 100 ]]
    then
      ICOBAT="${DZEN_ICONPATH}/battery_full.xbm"
      BATCOLOR=$GREEN
      BAT="${BAT}%"
    else
      if [[ $BAT > 45 ]]
      then
       	ICOBAT="${DZEN_ICONPATH}/battery_good.xbm"
       	BATCOLOR=$ORANGE
       	BAT="${BAT}%"
      else
       	if [[ $BAT > 10 ]]
       	then
	  ICOBAT="${DZEN_ICONPATH}/battery_low.xbm"
	  BATCOLOR=$RED
	  BAT="${BAT}%"
       	else
	  ICOBAT="${DZEN_ICONPATH}/battery_empty.xbm"
	  BATCOLOR=$RED
	  BAT="${BAT}%"
       	fi
      fi
    fi
    print -n `echo " ^fg(${BATCOLOR})^i($ICOBAT)^fg() ${BAT}"`
  else
    print -n ""
  fi
}

##########
## Main ##
##########

while true; do
  HOUR=$(fhour)
  DATE=$(fdate)
  CPUTEMP=$(fcputemp)
  VOLUME=$(fvolume)
  finterface
  fbwup $INTERFACE
  fbwdown $INTERFACE
  AC=$(fac)
  PBAT=$(fbat)

  print "${AC} ${PBAT} ${SEPARATOR} ${VOLUME} ${SEPARATOR} ${INTERFACE}: ^fg(${GREEN})^p(3)^i(${DZEN_ICONPATH}/arr_down.xbm)^fg(white)${BW_DOWN} kB/s^fg(${RED})^i(${DZEN_ICONPATH}/arr_up.xbm)^fg(white)${BW_UP} kB/s^fg() ${SEPARATOR} ${CPUTEMP} ${SEPARATOR} ${HOUR} ${SEPARATOR} ${DATE}"

  sleep $INTERVAL
done
