#!/bin/bash # Strict mode: exit on any error, undefined var, or pipe failure set -euo pipefail # Real-time output via pseudo-terminal (unbuffer from expect) # Dit forceert line-buffered output voor ALLE commands, ook in web interfaces if [ -z "$_UNBUFFERED" ] && command -v unbuffer &> /dev/null; then export _UNBUFFERED=1 exec unbuffer bash "$0" "$@" fi exec 2>&1 # Trap for clean error messages trap 'echo "=== ❌ FOUT: Update mislukt op regel $LINENO (commando: $BASH_COMMAND) ===" >&2; exit 1' ERR # --- CONFIGURATION (overridable via environment variables) --- DB_NAME="${NITRO_DB_NAME:-habbo}" DB_HOST="${NITRO_DB_HOST:-127.0.0.1}" DB_PORT="${NITRO_DB_PORT:-3306}" DB_USER="${NITRO_DB_USER:-root}" DB_PASS="${NITRO_DB_PASS:-}" EMULATOR_SERVICE="${NITRO_EMULATOR_SERVICE:-emulator}" EMULATOR_DIR="${NITRO_EMULATOR_PATH:-/var/www/emulator}" SQL_DIR="${NITRO_SQL_DIR:-$EMULATOR_DIR/Database Updates}" GAMEDATA_CONF_DIR="${NITRO_GAMEDATA_DIR:-/var/www/Gamedata/config}" NITRO_SRC_DIR="${NITRO_CLIENT_DIR:-/var/www/Nitro-V3/public/configuration}" BACKUP_DIR="${NITRO_BACKUP_DIR:-$EMULATOR_DIR/Database Updates/backups}" NITRO_CLIENT="${NITRO_CLIENT_SRC:-/var/www/Nitro-V3}" NITRO_RENDERER="${NITRO_RENDERER_SRC:-/var/www/Nitro_Render_V3}" # Build MySQL/MariaDB credentials argument MYSQL_CRED="-h $DB_HOST -P $DB_PORT -u $DB_USER" if [ -n "$DB_PASS" ]; then MYSQL_CRED="$MYSQL_CRED -p$DB_PASS" fi # --------------------- echo "=== Starting EPIC WEB CONTROL Update ===" # ---------------------------------------- # 1. Update and Build Emulator # ---------------------------------------- echo "--> Updating Emulator..." cd "$EMULATOR_DIR/Emulator/" git pull # --- Automatic Safe Database Backup --- echo "--> Creating automatic database backup before update..." mkdir -p "$BACKUP_DIR" mariadb-dump $MYSQL_CRED "$DB_NAME" > "$BACKUP_DIR/backup_$(date +%Y%m%d_%H%M%S).sql" # --- Automatic SQL Import --- echo "--> Checking for new SQL files..." if [ -d "$SQL_DIR" ]; then while IFS= read -r -d '' sql_file; do echo "--> Importing new SQL file: $(basename "$sql_file")" mariadb $MYSQL_CRED --force "$DB_NAME" < "$sql_file" done < <(find "$SQL_DIR" -name "*.sql" -mmin -10 -print0 2>/dev/null) else echo "--> SQL directory not found, skipping SQL import." fi echo "--> Building Emulator with Maven..." mvn package # Automatically detect the newly built .jar file JAR_FILE=$(basename "$(ls -t target/Habbo-*-jar-with-dependencies.jar 2>/dev/null | head -n 1)") echo "--> Found jar file: $JAR_FILE" echo "--> Updating emulator launch file..." cd target/ cat << EOF > emulator #!/bin/bash java -Xmx2G -jar $JAR_FILE EOF chmod +x emulator # ---------------------------------------- # 2. Update Nitro_Render_V3 # ---------------------------------------- echo "--> Updating Nitro_Render_V3..." cd "$NITRO_RENDERER/" git pull echo "--> Running yarn install for Nitro_Render_V3..." rm -rf node_modules yarn install # ---------------------------------------- # 3. Update and Build Nitro-V3 # ---------------------------------------- echo "--> Updating Nitro-V3..." cd "$NITRO_CLIENT" git pull echo "--> Running yarn install for Nitro-V3..." rm -rf node_modules yarn install echo "--> Building Nitro-V3 with Vite 8 / Yarn..." yarn build # ---------------------------------------- # 4. Sync Gamedata Configs & UITexts (.example logica) # ---------------------------------------- echo "--> Synchronizing Gamedata configurations..." mkdir -p "$GAMEDATA_CONF_DIR" # --- Renderer Config Sync --- if [ ! -f "$GAMEDATA_CONF_DIR/renderer-config.json" ] || [ "${NITRO_SRC_DIR}/renderer-config.example" -nt "$GAMEDATA_CONF_DIR/renderer-config.json" 2>/dev/null ]; then if [ -f "$NITRO_SRC_DIR/renderer-config.example" ]; then echo "--> Updating renderer-config.json from latest example..." cp "$NITRO_SRC_DIR/renderer-config.example" "$GAMEDATA_CONF_DIR/renderer-config.json" fi fi # --- UI Config Sync --- if [ ! -f "$GAMEDATA_CONF_DIR/ui-config.json" ] || [ "${NITRO_SRC_DIR}/ui-config.json.example" -nt "$GAMEDATA_CONF_DIR/ui-config.json" 2>/dev/null ]; then if [ -f "$NITRO_SRC_DIR/ui-config.json.example" ]; then echo "--> Updating ui-config.json from latest example..." cp "$NITRO_SRC_DIR/ui-config.json.example" "$GAMEDATA_CONF_DIR/ui-config.json" fi fi # --- UITexts Sync --- if [ ! -f "$GAMEDATA_CONF_DIR/UITexts.json5" ] || [ "${NITRO_SRC_DIR}/UITexts.json5.example" -nt "$GAMEDATA_CONF_DIR/UITexts.json5" 2>/dev/null ]; then if [ -f "$NITRO_SRC_DIR/UITexts.json5.example" ]; then echo "--> Updating UITexts.json5 from latest example..." cp "$NITRO_SRC_DIR/UITexts.json5.example" "$GAMEDATA_CONF_DIR/UITexts.json5" fi fi # ---------------------------------------- # 5. Automated Cleanup (Logs & Backups) # ---------------------------------------- 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 # 2. Keep max 5 newest database backups, delete older ones echo "--> Managing update backups (keeping max 5)..." if [ -d "$BACKUP_DIR" ]; then ls -t "$BACKUP_DIR"/*.sql 2>/dev/null | tail -n +6 | xargs -r rm -f || true fi # 3. Clean Yarn cache to save SSD space echo "--> Cleaning Yarn cache..." yarn cache clean # ---------------------------------------- # 6. Fix Permissions (www-data) # ---------------------------------------- if command -v sudo &> /dev/null; then echo "--> Setting permissions to www-data:www-data..." for dir in "$NITRO_CLIENT" "$NITRO_RENDERER" "$EMULATOR_DIR" "$GAMEDATA_CONF_DIR"; do if [ -d "$dir" ]; then sudo chown -R www-data:www-data "$dir" fi done echo "--> Permissions successfully set to www-data:www-data" else echo "--> Sudo not available, skipping chown." fi # ---------------------------------------- # 7. Restart the Service # ---------------------------------------- if systemctl list-units --type=service --all 2>/dev/null | grep -q "$EMULATOR_SERVICE.service"; then echo "--> Restarting $EMULATOR_SERVICE service..." if command -v sudo &> /dev/null; then sudo systemctl restart "$EMULATOR_SERVICE" sudo systemctl status "$EMULATOR_SERVICE" --no-pager -n 5 else echo "--> Sudo not available. Restart the service manually: sudo systemctl restart $EMULATOR_SERVICE" fi else echo "--> Warning: Service '$EMULATOR_SERVICE' not found in systemd." echo "--> If you use PM2, restart your processes manually: pm2 restart all" fi # ---------------------------------------- # 8. Extra Validation — 100% check # ---------------------------------------- echo "--> Running extra validation..." ERRORS=0 # Check build output exists if [ -d "$NITRO_CLIENT/public/assets" ]; then echo "--> [OK] Nitro-V3 build assets found" else echo "--> [FAIL] Nitro-V3 build assets missing!" ERRORS=$((ERRORS + 1)) fi # Check permissions on key files for dir in "$NITRO_CLIENT" "$NITRO_RENDERER" "$EMULATOR_DIR" "$GAMEDATA_CONF_DIR"; do if [ -d "$dir" ]; then OWNER=$(stat -c '%U:%G' "$dir" 2>/dev/null || echo "unknown") if [ "$OWNER" = "www-data:www-data" ] || [ "$(id -u)" -ne 0 ]; then echo "--> [OK] $dir ($OWNER)" else echo "--> [WARN] $dir heeft eigenaar $OWNER ipv www-data:www-data" sudo chown -R www-data:www-data "$dir" 2>/dev/null || true echo " Hersteld naar www-data:www-data" fi fi done # Check if emulator service is running if systemctl list-units --type=service --all 2>/dev/null | grep -q "$EMULATOR_SERVICE.service"; then SERVICE_STATUS=$(systemctl is-active "$EMULATOR_SERVICE" 2>/dev/null || echo "unknown") if [ "$SERVICE_STATUS" = "active" ]; then echo "--> [OK] $EMULATOR_SERVICE service is $SERVICE_STATUS" else echo "--> [WARN] $EMULATOR_SERVICE service is $SERVICE_STATUS (not active, check logs)" ERRORS=$((ERRORS + 1)) fi fi if [ "$ERRORS" -eq 0 ]; then echo "=== ✅ Update 100% successfully completed! ===" else echo "=== ⚠️ Update completed with $ERRORS warning(s) — check bovenstaande output ===" fi