#!/bin/bash

# make sure it doesn't run more than once
pgrep -x "$(basename "$0")" | grep -v $$ >/dev/null && exit 0

# placeholder for first loop pass
xsetroot -name "Loading ..."

while true; do
    final_output=""

    #set date: 04.03 15:22
    final_output=$(date +"%m.%d %H:%M")

    # get charge percentage
    if [[ -d "/sys/class/power_supply/BAT0" ]]; then
        battery_output="$(< /sys/class/power_supply/BAT0/capacity)%"

        # add the + sign if its charging 
        battery_status=$(< /sys/class/power_supply/BAT0/status)
        if [[ "$battery_status" == "Charging" ]]; then
            battery_output="${battery_output}+"
        fi
        final_output="${battery_output} ${final_output}"
    fi

    ### weather stuff
    wttr_file="/tmp/weather"
    if [[ -s "$wttr_file" ]]; then
        wttr=$(<"$wttr_file")
        final_output="${wttr} ${final_output}"
    fi

    # final output to status bar
    xsetroot -name "$final_output"
    sleep 1;
done
