diff --git a/update-Nitrov3.sh b/update-Nitrov3.sh index daac557..bf6a765 100755 --- a/update-Nitrov3.sh +++ b/update-Nitrov3.sh @@ -18,6 +18,11 @@ if [ -z "${_UNBUFFERED:-}" ] && command -v unbuffer &> /dev/null; then fi exec 2>&1 +# Single-instance lock via flock +LOCKFILE="/tmp/$(basename "$0").lock" +exec 200>"$LOCKFILE" +flock -n 200 || { echo "=== ❌ Another instance already running, exiting ==="; exit 1; } + # Trap for clean error messages trap 'echo "=== ❌ ERROR: Update failed at line $LINENO (command: $BASH_COMMAND) ===" >&2; exit 1' ERR @@ -46,10 +51,10 @@ NITRO_GAMEDATA_URL="${NITRO_GAMEDATA_URL:-https://epicnabbo.nl/gamedata}" NITRO_ASSET_URL="${NITRO_ASSET_URL:-https://epicnabbo.nl/gamedata/bundled}" NITRO_FURNI_ASSET_ICON_URL="${NITRO_FURNI_ASSET_ICON_URL:-https://epicnabbo.nl/gamedata/icons/%libname%%param%_icon.png}" -# Build MySQL/MariaDB credentials argument +# Build MySQL/MariaDB credentials argument (password via MYSQL_PWD — not visible in ps) MYSQL_CRED="-h $DB_HOST -P $DB_PORT -u $DB_USER" if [ -n "$DB_PASS" ]; then - MYSQL_CRED="$MYSQL_CRED -p$DB_PASS" + export MYSQL_PWD="$DB_PASS" fi # --------------------- @@ -68,7 +73,10 @@ echo "=== Starting EPIC WEB CONTROL Update ===" # ---------------------------------------- echo "--> Updating Emulator..." cd "$EMULATOR_DIR/Emulator/" -git stash --include-untracked || true && git checkout main && git pull +git stash --include-untracked || true +OLD_HEAD=$(git rev-parse HEAD) +git checkout main +git pull # --- Automatic Safe Database Backup --- echo "--> Creating automatic database backup before update..." @@ -86,23 +94,22 @@ else echo "--> SQL directory not found, skipping SQL import." fi -echo "--> Building Emulator with Maven..." -mvn package +if [ "$(git rev-parse HEAD)" != "$OLD_HEAD" ]; then + echo "--> New commits detected, building emulator..." + mvn package -# Automatically detect the newly built .jar file -JAR_PATTERN="target/Habbo-*-jar-with-dependencies.jar" -JAR_FILE=$(basename "$(ls -t $JAR_PATTERN 2>/dev/null | head -n 1)") + JAR_FILE=$(find target -maxdepth 1 -name 'Habbo-*-jar-with-dependencies.jar' -printf '%T@ %p\n' 2>/dev/null | sort -rn | sed -n '1s/^[0-9.]* //p' | xargs basename) -if [ -z "$JAR_FILE" ]; then - echo "=== ❌ No jar file found with pattern: $JAR_PATTERN ===" - exit 1 -fi + if [ -z "$JAR_FILE" ]; then + echo "=== ❌ No jar file found with pattern: target/Habbo-*-jar-with-dependencies.jar ===" + exit 1 + fi -echo "--> Found jar file: $JAR_FILE" -echo "--> Updating emulator launch file..." -cd target/ + echo "--> Found jar file: $JAR_FILE" + echo "--> Updating emulator launch file..." + cd target/ -cat << EOF > emulator + cat << EOF > emulator #!/bin/sh file_name_emulator=emulator.log current_time=\$(date "+%H%M_%d-%m-%Y") @@ -110,7 +117,10 @@ file_name=\$file_name_emulator.\$current_time mv /var/log/emu/emulator.log /var/log/emu/\$file_name java -Dfile.encoding=UTF8 -Xmx2G -jar $JAR_FILE EOF -chmod +x emulator + chmod +x emulator +else + echo "--> Emulator already up to date, skipping build." +fi # ---------------------------------------- @@ -119,11 +129,17 @@ chmod +x emulator echo "--> Updating Nitro_Render_V3..." cd "$NITRO_RENDERER/" -git stash --include-untracked || true && git checkout main && git pull +git stash --include-untracked || true +OLD_HEAD=$(git rev-parse HEAD) +git checkout main +git pull -echo "--> Running yarn install for Nitro_Render_V3..." -clean_node_modules -yarn install +if [ "$(git rev-parse HEAD)" != "$OLD_HEAD" ]; then + echo "--> New commits detected, running yarn install for Nitro_Render_V3..." + yarn install --frozen-lockfile || { echo "--> Retrying with clean node_modules..."; clean_node_modules && yarn install; } +else + echo "--> Nitro_Render_V3 already up to date, skipping." +fi # ---------------------------------------- @@ -132,14 +148,18 @@ yarn install echo "--> Updating Nitro-V3..." cd "$NITRO_CLIENT" -git stash --include-untracked || true && git checkout main && git pull +git stash --include-untracked || true +OLD_HEAD=$(git rev-parse HEAD) +git checkout main +git pull -echo "--> Running yarn install for Nitro-V3..." -clean_node_modules -yarn install - -echo "--> Building Nitro-V3 with Vite 8 / Yarn..." -yarn build +if [ "$(git rev-parse HEAD)" != "$OLD_HEAD" ]; then + echo "--> New commits detected, building Nitro-V3..." + yarn install --frozen-lockfile || { echo "--> Retrying with clean node_modules..."; clean_node_modules && yarn install; } + yarn build +else + echo "--> Nitro-V3 already up to date, skipping build." +fi echo "--> Ensuring custom-themes/index.json exists..." mkdir -p "$NITRO_CLIENT/dist/custom-themes" @@ -158,25 +178,16 @@ echo "--> Synchronizing configurations..." mkdir -p "$GAMEDATA_CONF_DIR" MERGE_SCRIPT="$(dirname "$0")/scripts/merge-config.cjs" +NITRO_DIST_CONFIG_DIR="$NITRO_CLIENT/dist/configuration" -# Phase 1: Merge .example with live configs in the same public directory -# so the Nitro client always has 100% complete configs -echo "--> Merging .example files with live configs in $NITRO_SRC_DIR..." -for example_file in "$NITRO_SRC_DIR"/*.example; do - [ -f "$example_file" ] || continue - base=$(basename "$example_file") - target_name="${base%.example}" - case "$target_name" in - *.*) ;; # already has extension (e.g., .json, .json5) - *) target_name="$target_name.json" ;; # no extension -> add .json - esac - live_path="$NITRO_SRC_DIR/$target_name" - echo "--> Processing $base -> $target_name (live)..." - node "$MERGE_SCRIPT" "$example_file" "$live_path" +# Temporarily make config dirs writable if owned by www-data (restored in section 6) +for dir in "$NITRO_SRC_DIR" "$GAMEDATA_CONF_DIR"; do + if [ -d "$dir" ] && [ ! -w "$dir" ] && command -v sudo &> /dev/null; then + sudo chown -R "$(whoami)":"$(whoami)" "$dir" + echo "--> [OK] Made $dir writable (will restore to www-data later)" + fi done -# Phase 2: Then merge the same to Gamedata/config for the website -echo "--> Merging Nitro config examples into Gamedata/config..." for example_file in "$NITRO_SRC_DIR"/*.example; do [ -f "$example_file" ] || continue base=$(basename "$example_file") @@ -185,29 +196,21 @@ for example_file in "$NITRO_SRC_DIR"/*.example; do *.*) ;; *) target_name="$target_name.json" ;; esac - target_path="$GAMEDATA_CONF_DIR/$target_name" - echo "--> Processing $base -> $target_name..." - node "$MERGE_SCRIPT" "$example_file" "$target_path" -done -# Phase 3: Sync merged configs to dist/configuration for the live client -NITRO_DIST_CONFIG_DIR="$NITRO_CLIENT/dist/configuration" -if [ -d "$NITRO_DIST_CONFIG_DIR" ]; then - echo "--> Syncing merged configs to dist/configuration/..." - for example_file in "$NITRO_SRC_DIR"/*.example; do - [ -f "$example_file" ] || continue - base=$(basename "$example_file") - target_name="${base%.example}" - case "$target_name" in - *.*) ;; - *) target_name="$target_name.json" ;; - esac - if [ -f "$NITRO_SRC_DIR/$target_name" ]; then - cp "$NITRO_SRC_DIR/$target_name" "$NITRO_DIST_CONFIG_DIR/$target_name" - echo "--> [OK] Synced $target_name to dist/configuration/" - fi - done -fi + # Phase 1: Merge to live config in Nitro public dir + echo "--> Merging $base -> $target_name (live)..." + node "$MERGE_SCRIPT" "$example_file" "$NITRO_SRC_DIR/$target_name" + + # Phase 2: Merge to Gamedata/config for website + echo "--> Merging $base -> $target_name (gamedata)..." + node "$MERGE_SCRIPT" "$example_file" "$GAMEDATA_CONF_DIR/$target_name" + + # Phase 3: Copy merged config to dist/configuration for live client + if [ -d "$NITRO_DIST_CONFIG_DIR" ]; then + cp "$NITRO_SRC_DIR/$target_name" "$NITRO_DIST_CONFIG_DIR/$target_name" + echo "--> [OK] Synced $target_name to dist/configuration/" + fi +done # Phase 4: Restore critical icon URLs in renderer-config and ui-config # (prevents git pull from overwriting URLs with example values) @@ -265,7 +268,7 @@ echo "--> Starting automated cleanup..." # 1. Remove emulator logs older than 14 days echo "--> Removing emulator logs older than 14 days..." -find "$EMULATOR_DIR/" -name "*.log" -mtime +14 -exec rm -f {} \; 2>/dev/null || true +find "$EMULATOR_DIR" -name "*.log" -mtime +14 -exec rm -f {} \; 2>/dev/null || true # 2. Keep max 5 newest database backups, delete older ones echo "--> Managing update backups (keeping max 5)..."