#!/usr/bin/env bash core_name_nethergamesmc() { echo "NetherGamesMC" } core_default_port_nethergamesmc() { echo "19132" } core_protocol_nethergamesmc() { echo "udp" } core_runtime_nethergamesmc() { echo "php" } core_validate_nethergamesmc() { return 0 } nethergamesmc_get_php_build_release_tag() { local requested_tag="$1" if [[ "$requested_tag" == "latest" ]]; then requested_tag="$(http_get "https://api.github.com/repos/NetherGamesMC/php-build-scripts/releases/latest" | jq -r '.tag_name // empty')" if [[ -z "$requested_tag" || "$requested_tag" == "null" ]]; then fail "$(t no_stable_build "NetherGamesMC/php-build-scripts" "latest")" fi fi echo "$requested_tag" } nethergamesmc_resolve_php_platform() { local os local arch os="$(uname -s | tr '[:upper:]' '[:lower:]')" arch="$(uname -m | tr '[:upper:]' '[:lower:]')" case "$os" in linux) os="linux" ;; darwin) os="macos|darwin" ;; *) os="$os" ;; esac case "$arch" in x86_64|amd64) arch="x86_64|amd64" ;; aarch64|arm64) arch="aarch64|arm64" ;; *) arch="$arch" ;; esac echo "$os:$arch" } nethergamesmc_get_php_build_asset_url() { local tag="$1" local allow_fallback="${2:-no}" local platform os_regex arch_regex local release_json platform="$(nethergamesmc_resolve_php_platform)" os_regex="${platform%%:*}" arch_regex="${platform##*:}" release_json="$(http_get "https://api.github.com/repos/NetherGamesMC/php-build-scripts/releases/tags/${tag}")" if [[ "$release_json" == *"\"message\""* && "$release_json" == *"Not Found"* && "$allow_fallback" == "no" ]]; then return 1 fi if [[ -z "$release_json" || "$release_json" == "null" ]]; then if [[ "$allow_fallback" == "yes" ]]; then release_json="$(http_get "https://api.github.com/repos/NetherGamesMC/php-build-scripts/releases/latest")" fi fi jq -r --arg os "$os_regex" --arg arch "$arch_regex" ' (.assets // [])[] | select((.name|test($os;"i")) and (.name|test($arch;"i")) and (.name|test("PM5";"i")) and (.name|test("\\.tar\\.gz$";"i"))) | .browser_download_url ' <<<"$release_json" | head -n 1 } nethergamesmc_sha256_file() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | awk '{print $1}' elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "$1" | awk '{print $1}' else echo "" fi } nethergamesmc_ensure_php_runtime() { local build_tag="$1" local php_dir="${SERVER_DIR}/tools/php" local marker="${SERVER_DIR}/.nethergamesmc-php-build" local current_build="" local build_url if [[ "${DRY_RUN:-no}" == "yes" ]]; then emit_step "core_runtime" "cache" echo "${php_dir}/bin/php" return 0 fi if [[ -f "$marker" ]]; then current_build="$(cat "$marker" 2>/dev/null || true)" fi if [[ "$current_build" == "$build_tag" ]]; then if [[ -x "${php_dir}/bin/php" && -f "${php_dir}/composer.phar" ]]; then emit_step "core_runtime" "cache" echo "${php_dir}/bin/php" return 0 fi fi build_url="$(nethergamesmc_get_php_build_asset_url "$build_tag" "no")" if [[ -z "$build_url" || "$build_url" == "null" ]]; then fail "$(t no_stable_build "NetherGamesMC/php-build-scripts" "$build_tag")" fi emit_step "core_phpbuild_resolve" "$build_tag" emit_step "core_phpbuild_fetch" "$build_url" local tmp_dir local tmp_archive tmp_dir="$(mktemp -d)" tmp_archive="${tmp_dir}/php-build-${build_tag}.tar.gz" run_cmd curl -fL "$build_url" -o "$tmp_archive" run_cmd mkdir -p "$tmp_dir/extracted" run_cmd tar -xzf "$tmp_archive" -C "$tmp_dir/extracted" run_cmd rm -rf "$php_dir" run_cmd mkdir -p "$php_dir" run_cmd cp -a "$tmp_dir/extracted"/. "$php_dir"/ run_cmd rm -rf "$tmp_dir" local php_bin php_bin="$(find "$php_dir" -type f -name 'php' | head -n 1 || true)" if [[ -z "$php_bin" ]]; then fail "PHP runtime binary not found after extraction" fi run_cmd chmod +x "$php_bin" run_cmd mkdir -p "$php_dir/bin" run_cmd ln -sf "$php_bin" "$php_dir/bin/php" run_cmd printf '%s\n' "$build_tag" > "$marker" echo "${php_dir}/bin/php" } nethergamesmc_fetch_composer() { local php_bin="$1" local composer_path="$2" if [[ "${DRY_RUN:-no}" == "yes" ]]; then return 0 fi emit_step "core_composer" "composer.phar" if [[ -f "$composer_path" ]]; then if "$php_bin" -n "$composer_path" --version >/dev/null 2>&1; then return 0 fi fi local phar_url="https://getcomposer.org/download/latest-stable/composer.phar" local sha_url="https://getcomposer.org/download/latest-stable/composer.phar.sha256sum" local hash_file="${composer_path}.sha256" run_cmd curl -fL "$phar_url" -o "$composer_path" run_cmd curl -fL "$sha_url" -o "$hash_file" local expected_sum local actual_sum expected_sum="$(awk '{print $1}' "$hash_file" | head -n 1 || true)" actual_sum="$(nethergamesmc_sha256_file "$composer_path" || true)" if [[ -n "$expected_sum" && -n "$actual_sum" && "$expected_sum" == "$actual_sum" ]]; then run_cmd rm -f "$hash_file" return 0 fi # Fallback to latest release asset when hash check is unavailable or changed run_cmd rm -f "$composer_path" "$hash_file" run_cmd curl -fL "https://github.com/composer/composer/releases/latest/download/composer.phar" -o "$composer_path" } nethergamesmc_render_start_script() { local php_bin="$1" emit_step "core_render" "nethergamesmc start script" run_cmd bash -c "cat > '$SERVER_DIR/start.sh' < "$SERVER_DIR/.nethergamesmc-build" emit_step "core_configure" "nethergamesmc done" } core_post_install_nethergamesmc() { emit_step "core_post" "NetherGamesMC" run_cmd bash -lc "chown -R ${SERVER_USER}:${SERVER_USER} '$SERVER_DIR'" emit_step "core_chown" "NetherGamesMC" open_firewall_port "$SERVER_PORT" "udp" emit_step "core_service" "NetherGamesMC" create_service "$CORE" "$SERVER_DIR" "$SERVICE_MODE" "$SERVER_DIR/start.sh" start_service } core_update_nethergamesmc() { info "$(t core_update "$(core_name_nethergamesmc)")" emit_step "core_update" "$(core_name_nethergamesmc)" backup_file "$SERVER_DIR/server.phar" backup_file "$SERVER_DIR/start.sh" backup_file "$SERVER_DIR/.nethergamesmc-build" core_install_nethergamesmc emit_step "core_update_done" "NetherGamesMC" }