#!/bin/sh # =========================================================================== # Dis2Hook updater — pulls the latest app.py and index.html straight from the # repo and restarts the service. Never touches token.json or config.json. # # /opt/dis2hook/update.sh update app.py + index.html # /opt/dis2hook/update.sh --all also refresh update.sh and the initd # # D2H_BRANCH=master ./update.sh pull from a different branch # =========================================================================== set -eu REPO="${D2H_REPO:-https://gogs.av2x.dev/av2x/Dis2Hook}" APP_DIR="$(cd "$(dirname "$0")" && pwd)" SERVICE="dis2hook" BRANCH="${D2H_BRANCH:-$(cat "$APP_DIR/.branch" 2>/dev/null || echo master)}" say() { printf '\033[1;33m[dis2hook]\033[0m %s\n' "$*"; } fail() { printf '\033[1;31m[dis2hook] ERROR:\033[0m %s\n' "$*" >&2; exit 1; } fetch() { wget -q -O "$2" "$REPO/raw/$BRANCH/$1" && [ -s "$2" ]; } TMP="$(mktemp -d)" trap 'rm -rf "$TMP"' EXIT say "Pulling latest files from $REPO ($BRANCH)…" fetch app.py "$TMP/app.py" || fail "Could not download app.py." fetch index.html "$TMP/index.html" || fail "Could not download index.html." # Sanity checks so a broken download never replaces a working install. grep -q DIS2HOOK_VERSION "$TMP/app.py" || fail "Downloaded app.py failed validation. Nothing changed." grep -qi '/dev/null || fail "Downloaded app.py does not compile. Nothing changed." OLD_VER="$(grep -m1 'DIS2HOOK_VERSION =' "$APP_DIR/app.py" 2>/dev/null | cut -d'"' -f2 || echo '?')" NEW_VER="$(grep -m1 'DIS2HOOK_VERSION =' "$TMP/app.py" | cut -d'"' -f2)" cp -f "$APP_DIR/app.py" "$APP_DIR/app.py.bak" 2>/dev/null || true cp -f "$APP_DIR/index.html" "$APP_DIR/index.html.bak" 2>/dev/null || true install -m 644 "$TMP/app.py" "$APP_DIR/app.py" install -m 644 "$TMP/index.html" "$APP_DIR/index.html" say "app.py + index.html updated ($OLD_VER → $NEW_VER). Previous versions kept as *.bak." if [ "${1:-}" = "--all" ]; then fetch update.sh "$TMP/update.sh" && install -m 755 "$TMP/update.sh" "$APP_DIR/update.sh" \ && say "update.sh refreshed." || say "Skipped update.sh (not downloadable)." fetch dis2hook.initd "$TMP/dis2hook.initd" && install -m 755 "$TMP/dis2hook.initd" "/etc/init.d/$SERVICE" \ && say "Service script refreshed." || say "Skipped service script (not downloadable)." fi say "token.json and config.json were left untouched." if rc-service "$SERVICE" restart >/dev/null 2>&1; then say "Service restarted — Dis2Hook v$NEW_VER is live." else say "Could not restart via OpenRC. Start manually: rc-service $SERVICE start" fi