#!/bin/sh
# colors
#source ~/.local/bin/statusbar/themes/sweetmars
#source ~/.local/bin/statusbar/themes/dracula
#source ~/.local/bin/statusbar/themes/sweetdracula
#source ~/.local/bin/statusbar/themes/tomorrow-night
source ~/.local/bin/statusbar/themes/tomorrow-dark
#source ~/.local/bin/statusbar/themes/tomorrow
#source ~/.local/bin/statusbar/themes/vacuous
#source ~/.local/bin/statusbar/themes/doom


interval=0

# Battery :
bat(){
	for battery in /sys/class/power_supply/BAT?*; do
		# If non-first battery, print a space separator.
		[ -n "${capacity+x}" ] && printf " "
		# Sets up the status and capacity
		case "$(cat "$battery/status" 2>&1)" in
			"Full") status="" ;;
			"Discharging") status="" ;;
			"Charging") status="" ;;
			"Not charging") status="" ;;
			"Unknown") status="" ;;
			*) exit 1 ;;
		esac
		capacity="$(cat "$battery/capacity" 2>&1)"
		# Will make a warn variable if discharging and low
		[ "$status" = " " ] && [ "$capacity" -le 25 ] && warn=" "
		# Prints the info
		printf "%s%s%d%%" "^c$cyan^^c$black^^b$cyan^ $status" "^c$black^^b$cyan^ $warn" "$capacity"; unset warn
	done && printf " \\n"
}

# Date && clock :
dat(){
	date="$(date +"%a %d %b %H:%M"| sed 's/  / /g')"
	#echo -e " $date"
	echo -e "^c$blue^^c$black^^b$blue^  $date "
	
}

# Cpu Temp :
tmp(){
	ctmp=$(sensors | awk '/Core 0/ {print$3}' | sed 's/+//')
	echo -e "^c$red^^c$black^^b$red^  $ctmp "
}

# Cpu Usage :
cpu(){
	read cpu a b c previdle rest < /proc/stat
	prevtotal=$((a+b+c+previdle))
	sleep 0.5
	read cpu a b c idle rest < /proc/stat
	total=$((a+b+c+idle))
	cpu=$((100*( (total-prevtotal) - (idle-previdle) ) / (total-prevtotal) ))
	#echo -e " $cpu%"
	echo -e "^c$cyan^^c$black^^b$cyan^  $cpu% " 
}

# Keybord Layout :
key(){
	kb="$(xkb-switch)" || exit 1
	#echo " $kb"
	echo -e "^c$green^^b$green^^c$black^^b$green^  $kb "
	
}

# Screen Light :
lit(){
	#lit="$(xbacklight | sed 's/\..*//g')"
	#echo " $lit%"
	lit="$(brightnessctl | grep -oP '[^()]+%')"
	#echo " $lit"
	echo -e "^c$yellow^^b$yellow^^c$black^^b$yellow^  $lit "		
}

# Memory :
mem(){
	mem="$(free -h | awk '/^Mem/ { print $3 }' | sed s/i//g)"
	echo -e "^c$green^^b$green^^c$black^^b$green^  $mem "
}

# Volume :
vol(){
	[ $(pamixer --get-mute) = true ] && echo  && exit

	vol="$(pamixer --get-volume)"

	if [ "$vol" -gt "70" ]; then
		icon=""
	elif [ "$vol" -gt "30" ]; then
		icon=""
	elif [ "$vol" -gt "0" ]; then
		icon="" 
	else
		echo "" && exit
	fi
	echo "^c$magenta^^b$magenta^^c$black^^b$magenta^ $icon $vol% "
}


net() {
	connected=$(cat /sys/class/net/e*/operstate 2>/dev/null)
	if [[ "$connected" =~ "up" ]]; then
		printf "^c$green^^b$green^^c$black^  %s"
	elif [[ "$connected" =~ "down" ]]; then
		check_wifi=$(cat /sys/class/net/wl*/operstate 2>/dev/null)
		if [[ "$check_wifi" =~ "up" ]]; then
			printf "^b$green^^c$black^  %s"
		elif [[ "$check_wifi" =~ "down" ]]; then
			printf "^c$green^^b$green^^c$black^ 睊 %s"
		fi
	fi
}

# Network traffic
nettrf(){
	
	update() {
		sum=0
		for arg; do
			read -r i < "$arg"
			sum=$(( sum + i ))
		done
		cache=${XDG_CACHE_HOME:-$HOME/.cache}/${1##*/}
		[ -f "$cache" ] && read -r old < "$cache" || old=0
		printf %d\\n "$sum" > "$cache"
		printf %d\\n $(( sum - old ))
	}

	rx=$(update /sys/class/net/[ew]*/statistics/rx_bytes)
	tx=$(update /sys/class/net/[ew]*/statistics/tx_bytes)

	printf "^c$magenta^^b$magenta^^c$black^ ^c$black^^b$magenta^ %4sB ^b$magenta^^c$black^ ^c$black^^b$magenta^ %4sB \\n" $(numfmt --to=iec $rx) $(numfmt --to=iec $tx)	
}

# Updates
pkg_updates() {
	#updates=$(xbps-install -un | wc -l) # void
	updates=$(pacman -Qu | grep -Fcv "[ignored]" | sed "s/^//;s/^0$//g")   # arch , needs pacman contrib
	# updates=$(aptitude search '~U' | wc -l)  # apt (ubuntu,debian etc)

	if [ "$updates" == "0" ]; then
		printf "^c$green^^b$green^^c$black^  ^c$green^^b$black^Fully Updated^d^"
	else
		printf "^c$green^^b$green^^c$black^  $updates " "^c$green^^b$black^updates ^d^"
	fi
}
while true; do
 [ $interval = 0 ] || [ $(($interval % 3600)) = 0 ] && updates=$(pkg_updates)
  interval=$((interval + 1))
  space=" "
  range="|"
  
  #xsetroot -name "$updates$(nettrf)$(tmp)$(cpu)$(mem)$(vol)$(lit)$(bat)$(key)$(dat)$(net)"
  xsetroot -name "$(cpu)$(mem)$(vol)$(lit)$(bat)$(key)$(dat)$(net)"
  sleep 0.2
done &
