#!/bin/sh

# show mountpoint usage
fs_perc()
{
	df -h $1 | awk 'NR==2 {print $5}'
}

volume()
{
	sndioctl output.level | awk -F = '{printf "%d",$2*100}'
}

cputemp()
{
	sysctl hw.sensors.cpu0.temp0 | awk -F'=' '
	{
		split($2,a,".");
		if (a[1] > 55) {
			printf "[ %s°C ] ", a[1]
		}
	}'
}

cpuspeed()
{
	sysctl hw.cpuspeed | awk -F'=' '{print $2}'
}

cpuperc()
{
	vmstat | awk 'END {printf $16}'
}

ramperc()
{
	vmstat -s | awk '
	/pages managed/ { page_tot = $1; next }
	/pages active/ { page_active = $1; next }
	/pages inactive/ { page_inactive = $1; next }

	END {
		used = page_active + page_inactive
		perc = used / page_tot * 100

		perc = int(perc)

		print perc
	}
	'
}

loadavg()
{
	#uptime | awk -F'[a-z]:' '{print $2}' | tr -d ' '
	sysctl -n vm.loadavg
}

upsince()
{
	uptime | awk '
	$3 ~ /secs,/ {
		printf "%ss", $2
	}
	$4 ~ /min/ {
		sub(",", "", $4)
		printf "%s%s", $3, $4
	}
	$4 ~ /hrs/ {
		sub(",", "", $4)
		printf "%sh", $3
	}
	$3 ~ /[0-9]+:[0-9]/ {
		sub(",", "", $3)
		printf "%s", $3
	}
	'
}

bat()
{
	apm | awk '
	NR==1 { 
		if ($3 == "charging,") { batstr = "+" $4 }
		else { batstr = "-" $4 }
		sub("%","",$4)
		lvl = $4 + 0
	}
	NR==2 && /not connected/ {
		if (lvl < 90) {
			printf(" [ bat:%s ] ", batstr)
		}
	}
	'
	#test $(apm -a) -eq 0 && printf "🗲 %s%%" $(apm -l)
}

mpdstatus()
{
pgrep -q mpd 
if [ $? -eq 0 ]; then
	mpc -f "[ [%artist% - ]%title% ]|[ %file% ]" | awk '
(NR == 1) { 
	status = $0
	if (length(status) > 50) {
		status = substr($0, 1, 50) "..."
	}
}
(NR == 2) && /playing/ {
	isplaying = 1
	#progress = $3
}
END { if (isplaying) { printf " [%s] ", status } }
'
else
	printf ""
fi
}

temp()
{
	sysctl hw.sensors | awk -F"=" '{print $2; exit}'
}

# wifi iface
wifi()
{
	ifconfig $1 | awk '
	END { 
		ap = $3
		match($0, "[0-9]+%")
		if ( RLENGTH != -1 ) {
			perc = substr($0, RSTART, RLENGTH)
			printf("📶:%s %s", ap, perc)
		}
	}
'
}

pubip()
{
	#ftp -M -V -o- https://ipecho.net/plain
	ftp -M -V -o- https://ipaddress.sh/
}

# ratio of the year
yearratio()
{
	# number of days in the year
	awk 'BEGIN {
		year = strftime("%Y")
		today = strftime("%j")
		days = 365
		if (year % 4 == 0) {
			days=366
			if ((year % 100 == 0) && (year % 400 != 0)) {
				days == 365
			}
		}
		print today "/" days
	}'
}


# percentage of the year
yearperc()
{
	# number of days in the year
	awk 'BEGIN {
		elapsed = (strftime("%j") - 1 ) * 86400 \
			+ strftime("%H") * 3600 \
			+ strftime("%M") * 60 \
			+ strftime("%S")

		year = strftime("%Y")
		days = 365
		if (year % 4 == 0) {
			days=366
			if ((year % 100 == 0) && (year % 400 != 0)) {
				days = 365
			}
		}
		total = days * 86400
		perc = elapsed * 100 / total

		# check if there is a dot
		perc = int(perc)

		date = strftime("%d/%m %H:%M")
		printf "[%s:%s%%  %s]", 
			year, perc, date
	}'
}

# percentage of the day
dayperc()
{
	awk 'BEGIN {

	elapsed = strftime("%H") * 60 + strftime("%M")
	total = 1440

	printf "%s", int(elapsed * 100/total)
	}'
}

# bar showing elapsed time in the day
daybar()
{
	awk 'BEGIN {

	
	elapsed = 0 + strftime("%H")

	for (i=0; i<24; i++)  {
		if (i<elapsed) {
			printf "•"
		} else {
			printf "◦"
		}
	}

	}'
}

datetime()
{
	date "+%d/%m %H:%M"
}

#printf "%s  %s  🌡 %s°C  💾  %s  %s" \
printf "%s%s[ %s%% ] [ L=%s%% ] [ %s ] " \
	"$(mpdstatus)" \
	"$(bat)" \
	"$(dayperc)" \
	"$(volume)" \
	"$(datetime)"
