#!/bin/sh
set -e
set -u

main() { (
    sed '1,/^#~\/.local\/bin\/brew-updater/d' "${0}" > ~/.local/bin/brew-update-hourly
    chmod a+x ~/.local/bin/brew-update-hourly

    echo "Checking for serviceman..."
    ~/.local/bin/webi serviceman
    if ! command -v serviceman > /dev/null; then
        export PATH="$HOME/.local/bin:$PATH"
    fi
    serviceman --version

    serviceman add --agent \
        --workdir ~/.local/opt/brew/ \
        --name sh.brew.updater -- \
        ~/.local/bin/brew-update-hourly
); }

if ! main; then
    exit 1
fi
exit 0

#~/.local/bin/brew-updater
#!/bin/sh
#set -e
set -u

if test -e ~/.config/envman/PATH.env; then
    # shellcheck disable=SC1090
    . ~/.config/envman/PATH.env
fi

while true; do
    my_start="$(date '+%s')"

    my_date="$(date '+%F %T')"
    echo "[$my_date] Updating brew..."
    brew update
    echo ''

    my_end="$(date '+%s')"
    my_elapsed="$((my_end - my_start))"
    my_date="$(date '+%F %T')"
    echo "[$my_date] Updated in ${my_elapsed}s."

    echo "[$my_date] Cleaning up..."
    brew cleanup

    echo "[$my_date] Waiting 24 hours..."
    my_wait="$((24 * 60 * 60))"
    sleep "$my_wait"
done
