diff --git a/user/local/share/towel/bin/towel b/user/local/share/towel/bin/towel index 9b61b72..557570c 100755 --- a/user/local/share/towel/bin/towel +++ b/user/local/share/towel/bin/towel @@ -14,6 +14,9 @@ if [ ! -d "$TOWEL_BIN_DIR" ]; then exit 1 fi +# shellcheck source=user/local/share/towel/bin/towel-common +source "$TOWEL_BIN_DIR/towel-common" + function print_help() { "$TOWEL_BIN_DIR/towel-help" } @@ -39,6 +42,8 @@ function unknown_option() { exit 1 } +towel_auto_update_check + if [ $# -eq 0 ]; then exec "$TOWEL_BIN_DIR/towel-enter" fi diff --git a/user/local/share/towel/bin/towel-common b/user/local/share/towel/bin/towel-common index beb0ee4..167ecf8 100755 --- a/user/local/share/towel/bin/towel-common +++ b/user/local/share/towel/bin/towel-common @@ -285,4 +285,52 @@ function towel_remove_exported_apps() { [ -d "$export_apps_dir" ] && rm -rf "$export_apps_dir" } +function towel_auto_update_check() { + [ -n "${CONTAINER_ID:-}" ] && return + [ "${TOWEL_NO_UPDATE_CHECK:-}" = "1" ] && return + [ -f "$TOWEL_DATA/no-update-check" ] && return + + local today + today="$(date +%Y-%m-%d)" + local check_state="$TOWEL_DATA/update_check" + local notified_file="$TOWEL_DATA/update_notified" + local stored_date="" stored_status="" stored_latest="" + + local stored_line + if stored_line="$(cat "$check_state" 2>/dev/null)"; then + read -r stored_date stored_status stored_latest <<<"$stored_line" + fi + + if [ "$stored_date" = "$today" ] && [ "$stored_status" = "lt" ] && [ -n "$stored_latest" ]; then + local notified_date + notified_date="$(cat "$notified_file" 2>/dev/null || true)" + if [ "$notified_date" != "$today" ]; then + echo 1>&2 "towel update available: $TOWEL_VERSION -> $stored_latest" + echo 1>&2 " Run 'towel update --apply' to update, or 'towel update --no-auto-check' to disable these notifications." + printf '%s\n' "$today" >"$notified_file" + fi + fi + + if [ "${stored_date:-}" != "$today" ]; then + ( + out="$("$TOWEL_BIN_DIR/towel-update" --check 2>/dev/null)" + rc=$? + ts="$(date +%Y-%m-%d)" + case $rc in + 0) printf '%s eq\n' "$ts" >"$check_state" ;; + 10) + ver="$(printf '%s\n' "$out" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+$' | tail -1)" + if [ -n "$ver" ]; then + printf '%s lt %s\n' "$ts" "$ver" >"$check_state" + else + printf '%s error\n' "$ts" >"$check_state" + fi + ;; + *) printf '%s error\n' "$ts" >"$check_state" ;; + esac + ) >/dev/null 2>&1 & + disown 2>/dev/null || true + fi +} + _towel_set_opts diff --git a/user/local/share/towel/bin/towel-update b/user/local/share/towel/bin/towel-update index 6cbc30b..2ba1b5c 100755 --- a/user/local/share/towel/bin/towel-update +++ b/user/local/share/towel/bin/towel-update @@ -14,20 +14,25 @@ TOWEL_GITHUB_REPO="${TOWEL_GITHUB_REPO:-codam-coding-college/towel}" function print_usage() { echo 1>&2 "Usage: - towel update --check [--quiet] - towel update --apply [-y|--yes] + towel update --check [--quiet] + towel update --apply [-y|--yes] + towel update --no-auto-check + towel update --auto-check towel update [-h|--help] Flags: - --check Query GitHub for the latest release and compare to the installed version. - Exits 0 if up to date, 10 if an update is available, 1 on error. - --apply Download, verify, and install the latest release. - -y, --yes Skip the confirmation prompt (--apply only). - --quiet Suppress stdout output (--check only; stderr and exit code preserved). - -h, --help Print this help text. + --check Query GitHub for the latest release and compare to the installed version. + Exits 0 if up to date, 10 if an update is available, 1 on error. + --apply Download, verify, and install the latest release. + -y, --yes Skip the confirmation prompt (--apply only). + --quiet Suppress stdout output (--check only; stderr and exit code preserved). + --no-auto-check Disable the automatic daily update check and notifications. + --auto-check Re-enable the automatic daily update check. + -h, --help Print this help text. Environment: - TOWEL_GITHUB_REPO Override the GitHub owner/repo (default: codam-coding-college/towel)." + TOWEL_GITHUB_REPO Override the GitHub owner/repo (default: codam-coding-college/towel) + TOWEL_NO_UPDATE_CHECK Set to 1 to disable automatic update checks." } function _towel_update_compare() { @@ -67,6 +72,8 @@ for arg in "$@"; do case "$arg" in --check) mode="check" ;; --apply) mode="apply" ;; + --no-auto-check) mode="no-auto-check" ;; + --auto-check) mode="auto-check" ;; -y | --yes) assume_yes="true" ;; --quiet) quiet="true" ;; -h | --help) @@ -82,11 +89,24 @@ for arg in "$@"; do done if [ -z "$mode" ]; then - echo 1>&2 "towel update: a mode flag is required (--check or --apply)" + echo 1>&2 "towel update: a mode flag is required (--check, --apply, --no-auto-check, or --auto-check)" print_usage exit 64 fi +if [ "$mode" = "no-auto-check" ]; then + touch "$TOWEL_DATA/no-update-check" + echo "Automatic update checks disabled." + echo " Re-enable with: towel update --auto-check" + exit 0 +fi + +if [ "$mode" = "auto-check" ]; then + rm -f "$TOWEL_DATA/no-update-check" "$TOWEL_DATA/update_check" "$TOWEL_DATA/update_notified" + echo "Automatic update checks enabled." + exit 0 +fi + latest_version="$(_towel_fetch_latest_version)" || { echo 1>&2 "towel update --${mode} failed: could not reach GitHub API (check network connectivity)" exit 1