#!/bin/bash # Exit immediately if a command exits with a non-zero status set -e # --- 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 find "$SQL_DIR" -name "*.sql" -mmin -10 -print0 | while IFS= read -r -d '' sql_file; do echo "--> Importing new SQL file: $(basename "$sql_file")" mariadb $MYSQL_CRED --force "$DB_NAME" < "$sql_file" || echo "--> Note: Some SQL rules skipped (probably already exist)." done 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 | 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/" # Check if package.json will change after git pull if git diff --name-only HEAD..@{u} | grep -q 'package.json'; then NEED_YARN_RENDER=true else NEED_YARN_RENDER=false fi git pull if [ "$NEED_YARN_RENDER" = true ]; then echo "--> package.json changed! Cleaning node_modules from Nitro_Render_V3..." rm -rf node_modules echo "--> Running yarn install for Nitro_Render_V3..." yarn install else echo "--> No changes in package.json for Nitro_Render_V3. Skipping yarn install." fi # ---------------------------------------- # 3. Update and Build Nitro-V3 # ---------------------------------------- echo "--> Updating Nitro-V3..." cd "$NITRO_CLIENT" # Check if package.json will change after git pull if git diff --name-only HEAD..@{u} | grep -q 'package.json'; then NEED_YARN_V3=true else NEED_YARN_V3=false fi git pull if [ "$NEED_YARN_V3" = true ]; then echo "--> package.json changed! Cleaning node_modules from Nitro-V3..." rm -rf node_modules echo "--> Running yarn install for Nitro-V3..." yarn install else echo "--> No changes in package.json for Nitro-V3. Skipping yarn install." fi 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" ]; 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" ]; 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" ]; 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. Keep max 5 newest database backups, delete older ones echo "--> Managing update backups (keeping max 5)..." if [ -d "$BACKUP_DIR" ]; then # Find all .sql files in backup dir, sort by date (newest first), # skip the first 5 (tail -n +6) and delete the rest. 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) — only if sudo is available # ---------------------------------------- if command -v sudo &> /dev/null; then echo "--> Setting permissions to www-data:www-data..." sudo chown -R www-data:www-data "$NITRO_CLIENT" 2>/dev/null || echo "--> chown voor NITRO_CLIENT overgeslagen" sudo chown -R www-data:www-data "$NITRO_RENDERER" 2>/dev/null || echo "--> chown voor NITRO_RENDERER overgeslagen" sudo chown -R www-data:www-data "$EMULATOR_DIR" 2>/dev/null || echo "--> chown voor EMULATOR_DIR overgeslagen" sudo chown -R www-data:www-data "$GAMEDATA_CONF_DIR" 2>/dev/null || echo "--> chown voor GAMEDATA_CONF_DIR overgeslagen" else echo "--> Sudo not available, skipping chown." fi # ---------------------------------------- # 7. Restart the Service (Veilige Check) # ---------------------------------------- if systemctl list-units --type=service --all | grep -q "$EMULATOR_SERVICE.service"; then echo "--> Restarting $EMULATOR_SERVICE service..." if command -v sudo &> /dev/null; then sudo systemctl restart $EMULATOR_SERVICE 2>/dev/null || echo "--> Service restart via sudo failed (possible insufficient sudo rights)" sudo systemctl status $EMULATOR_SERVICE --no-pager -n 5 2>/dev/null || true 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 echo "=== Update successfully completed! ==="