#!/bin/sh # ========================================================================== # WeBrake updater — pulls the latest app.py and static/index.html straight # from the repository and restarts the service. # # /opt/webrake/update.sh # # Tries branches master and main, and both static/index.html and a root # index.html, validating every payload before anything is swapped in. # token.json is never touched: secrets stay local, exactly as installed. # ========================================================================== set -eu REPO_BASE="${WEBRAKE_REPO_BASE:-https://gogs.av2x.dev/av2x/WeBrake/raw}" BRANCHES="${WEBRAKE_BRANCH:-master main}" APP_DIR="/opt/webrake" BACKUP_DIR="$APP_DIR/.backup/$(date +%Y%m%d-%H%M%S)" say() { printf '\033[1;33m[WeBrake]\033[0m %s\n' "$*"; } die() { printf '\033[1;31m[WeBrake]\033[0m %s\n' "$*" >&2; exit 1; } [ "$(id -u)" = "0" ] || die "Run as root inside the Alpine container." [ -d "$APP_DIR" ] || die "WeBrake is not installed at $APP_DIR — run install.sh first." TMP="$(mktemp -d)" trap 'rm -rf "$TMP"' EXIT # fetch [alt-path…] fetch() { dest="$1"; check="$2"; shift 2 for br in $BRANCHES; do for p in "$@"; do if wget -q -O "$dest" "$REPO_BASE/$br/$p" && [ -s "$dest" ] \ && head -c 4096 "$dest" | grep -q "$check"; then say " fetched $p (branch: $br)" return 0 fi done done return 1 } say "Fetching latest files from ${REPO_BASE} (branches tried: ${BRANCHES})…" fetch "$TMP/app.py" "^#!/usr/bin/env python3" "app.py" \ || die "No valid app.py found in the repo — nothing changed." fetch "$TMP/index.html" "^" "static/index.html" "index.html" \ || die "No valid index.html found (looked at static/index.html and index.html) — nothing changed." fetch "$TMP/update.sh" "^#!/bin/sh" "update.sh" \ || say "Could not refresh update.sh (keeping current copy)." say "Backing up current files to $BACKUP_DIR…" mkdir -p "$BACKUP_DIR" "$APP_DIR/static" cp -a "$APP_DIR/app.py" "$APP_DIR/static/index.html" "$BACKUP_DIR/" 2>/dev/null || true install -m 0755 "$TMP/app.py" "$APP_DIR/app.py" install -m 0644 "$TMP/index.html" "$APP_DIR/static/index.html" [ -s "$TMP/update.sh" ] && install -m 0755 "$TMP/update.sh" "$APP_DIR/update.sh" || true say "token.json untouched — local secrets preserved." say "Restarting service…" rc-service webrake restart >/dev/null 2>&1 || rc-service webrake start say "WeBrake updated."