#!/bin/sh

#
# Customizable variables used by the various functions.
#
acad_sensor="acpiacad0:connected"
battery_sensor="acpibat0:charge"
clock_format="+%H:%M"
date_format="+%d/%m/%Y"
mixer_mute="outputs.master.mute"
mixer_output="outputs.master"
network_interfaces="alc0 urtwn0"
temperature_sensor="acpitz0:cpu0/cpu1/cpu2/cpu3 temperature"

#
# Customizable status command dynamically evaluated every $sleep_time.
#
status_cmd='status="`network_status` $separator `volume` $separator `cpu_temperature` $separator `battery_status` $separator `unread_mail` $separator `unread_feeds` $separator `what_day_is_it` $separator `what_time_is_it`"'

# Sleep time (in seconds) used to refresh the status bar
sleep_time=60

#
# Symbols (mostly Unicode characters) used by the various functions.
#
acad_symbol="🔌"
battery_symbol="🔋"
clock_symbol="⏰"
date_symbol="📅"
mail_symbol="🖂"
network_symbol="🖧"
feeds_symbol="📰"
temperature_symbol="🌡"
separator="|"

mute_volume_symbol="🔇"
low_volume_symbol="🔈"
medium_volume_symbol=" 🔉"
high_volume_symbol="🔊"


#
# Print current date via date(1) honoring $date_format.
#
what_day_is_it()
{
	printf "%s " $date_symbol
	date "$date_format"
}

#
# Print current time via date(1) honoring $clock_format.
#
what_time_is_it()
{
	printf "%s " $clock_symbol
	date "$clock_format"
}

#
# Print the number of unread emails.
# FIXME: nmh logic is actually hardcoded inside the function.
#
unread_mail()
{
	printf "%s " $mail_symbol
	new | sed -nE '/total/ s/^ total +([0-9]+)\.$/\1/p'
}

#
# Print the number of unread feeds.
# FIXME: nmh logic is actually hardcoded inside the function.
#
unread_feeds()
{
	printf "%s " $feeds_symbol
	MH=${HOME}/.mh_rss_profile new | sed -nE '/total/ s/^ total +([0-9]+)\.$/\1/p'
}
	
#
# Print the percentage of battery left using envstat(8) and $battery_sensor.
# If an AC adapter is present set the AC adapter symbol accordingly.
#
battery_status()
{
	acad_connected=`envstat -s "$acad_sensor" | awk '/connected:/ { print $2 }'`

	if [ "$acad_connected" = "TRUE" ]; then
		printf "%s " $acad_symbol
	else
		printf "%s " $battery_symbol
	fi
	{ envstat -s "$battery_sensor" || echo "charge: (N/A)" ; } | sed -e 's/\.[0-9][0-9]%/%/' -nEe '/charge:/ s/^.*\(([^)]+)\)$/\1/p'
}

#
# Print all the network interfaces currently up defined in
# $network_interfaces list.
#
network_status()
{
	connected=""

	printf "%s " ${network_symbol}
	for i in $network_interfaces; do
		if [ -n "`ifconfig -u $i 2>/dev/null`" ]; then
			if [ ! "$connected" ]; then
				connected="yes"
			fi
			printf " ${i}"
		fi
	done

	if [ ! "$connected" ]; then
		printf " -"
	fi
}

#
# Print the CPU temperature using envstat(8) and $temperature_sensor.
#
cpu_temperature()
{
	printf "%s " $temperature_symbol
	{ envstat -s "$temperature_sensor" || echo "temperature: N/A" ; } | sed -nE '/temperature:/ s/^.*: *([0-9]+|N\/A) *.*$/\1/p'
}

#
# Print current volume level graphically via a symbol using mixerctl(1) and
# $mixer_output.
#
volume()
{
	low_volume=0
	medium_volume=80
	high_volume=160

	if [ "$mixer_mute" -a "`mixerctl -n $mixer_mute 2>/dev/null`" = "on" ]; then
		echo "$mute_volume_symbol"
		return
	fi

	volume=`mixerctl -n "$mixer_output" | awk -F ',' '{ print ($1 + $2) / 2 }'`

	if [ $volume -le $low_volume ]; then
		echo "$low_volume_symbol"
	elif [ $volume -le $medium_volume ]; then
		echo "$medium_volume_symbol"
	else
		echo "$high_volume_symbol"
	fi
}

onetime=false
output=false

args=`getopt 1os: $*`
if [ $? -ne 0 ]; then
	echo "usage: $0 [-1o] [-s seconds]"
	exit 2
fi
set -- $args
while [ $# -gt 0 ]; do
	case "$1" in
	-1)
		onetime=true
		;;
	-o)
		output=true
		;;
	-s)
		sleep_time=$2; shift
		;;
	--)
		shift; break
		;;
	esac
	shift
done

while true; do
	eval $status_cmd
	if $output; then
		printf "\r"
		printf "%s" "$status"
	else
		xsetroot -name "$status"
	fi
	if $onetime; then
		exit 0
	fi
	sleep $sleep_time
done
