#!/bin/sh
# wmfs status
# inspired by addikt1ve, rhaamo, linkdd and some others

# music with mpc
_music() {
	play=`mpc | grep playing`
	pause=`mpc | grep pause`

	artist=`mpc -f %artist% | head -n1 | cut -c1-15`
	song=`mpc -f %title% | head -n1 | cut -c1-15`

	[ -n "$play" ] && playing="$artist - $song" || playing="stop"
	
	color="#5ca881"

	music="^s[right;$color;$playing]"
}

# volume
_volume() {

	status=`amixer -c 0 sget 'Master' | tail -n1 | awk '{ print $6 }' | sed -re 's/^\[*//' | cut -d\] -f1`
	if [ $status == on ]; then
		vol=`amixer -c 0 sget 'Master' | tail -n1 | awk '{ print $4 }' | sed -re 's/^\[*//' | cut -d\] -f1`
	else
		vol="mute"
	fi

	color="#5ca8a7"

	volume="^s[right;$color;vol $vol]"
}

# memory used
_memory() {
	total=`free -m -t | head -n2 | tail -n1 | awk '{print $2}'`
	used=`free -m -t | head -n2 | tail -n1 | awk '{print $3}'`
	mem=$((100*$used/$total))
	
	color="#f96d6d"

	memory="^s[right;$color;mem $mem%]"
}

#swap used
_swap() {
	total=`free -m -t | tail -n1 | awk '{print $2}'`
	used=`free -m -t | tail -n1 | awk '{print $3}'`
	mem=$((100*$used/$total))

	color="#faa125"

	swap="^s[right;$color;swap $mem%]"
}

# hdd used
_hdd() {
	total=`df -h --total | tail -n1 | awk '{print $2}'`
	used=`df -h --total | tail -n1 | awk '{print $3}'`

	color="#fad325"

	hdd="^s[right;$color;hdd $used/$total]"
}

# wifi
_wifi() {
	wlan=`awk '/wlan/' /proc/net/wireless`
	quality=`awk '/wlan0/ { print $3 }' /proc/net/wireless|sed -e 's/\.$//'`
	essid=`iwconfig wlan0 | awk '/ESSID/ { print $4 }'|sed -e 's/ESSID:"//'|sed -e 's/"$//' | cut -c1-7`

	if [ -z "$wlan" ]; then
		wifi="off"
	else
		wifi="$essid $quality%"
	fi

	color="#b4c862"

	wifi="^s[right;$color;$wifi]"
}

# network
# _network() {
# }

# uptime
_uptime() {
	up=`uptime | awk '{print $3}' | cut -d, -f1`

	color="#c090e4"

	uptime="^s[right;$color;up $up]"
}

# date
_date() {
	hour=(`date +%H:%M`)
	day=(`date +%d/%m`)

	color="#8696e4"

	date="^s[right;$color;$day $hour]"
}

_button() {
	button="pause"
	color="#8696e4"
	button="^s[right;$color;$button]"
}

# statustext
statustext() {
	first=1
	args=""
	for arg in $@; do
		if [ $first == 1 ]; then
			separator=""
			first=0
		else	
			separator="^s[right;#c0c0c0; | ]"
		fi
		_${arg}
		args="${args} `eval echo '$separator$'$arg`"
	done
	wmfs -c status "default $args"
}

# possible args:
# 	memory swap battery
#	date uptime hdd
#	volume music
#	wifi network
while true;
do
	statustext volume music wifi hdd swap memory uptime date
	sleep 1
done