#!/bin/bash set -euo pipefail cd "$(dirname "${BASH_SOURCE[0]}")/.." build_for_arch() { local arch="$1" local scratch=".build/server-$arch" swift build -c release --product EzServerMac --arch "$arch" --scratch-path "$scratch" >&2 || return swift build -c release --arch "$arch" --scratch-path "$scratch" --show-bin-path } runtime="${EZSERVER_RUNTIME:-$PWD/server/original}" if [[ ! -f "$runtime/www/ezwebserver/src/public/api/index.php" || ! -f "$runtime/www/ezwebserver/src/vendor/autoload.php" ]]; then printf 'Original PHP runtime missing or incomplete: %s\n' "$runtime" >&2 exit 1 fi arm64_bin="$(build_for_arch arm64)" x86_64_bin="$(build_for_arch x86_64)" mkdir -p dist stage="$(mktemp -d dist/.EzServer.XXXXXX)" trap 'rm -rf "$stage"' EXIT app="$stage/EzServer.app" mkdir -p "$app/Contents/MacOS" "$app/Contents/Resources" binary="$app/Contents/MacOS/EzServerMac" xcrun lipo -create "$arm64_bin/EzServerMac" "$x86_64_bin/EzServerMac" -output "$binary" case "$(xcrun lipo -archs "$binary")" in "arm64 x86_64"|"x86_64 arm64") ;; *) printf '%s\n' 'Expected arm64 and x86_64 in EzServerMac.' >&2; exit 1 ;; esac cp Packaging/Info.plist "$app/Contents/Info.plist" cp README.md "$app/Contents/Resources/README.md" ditto Packaging/webconsole "$app/Contents/Resources/webconsole" php_resources="$app/Contents/Resources/php" mkdir -p "$php_resources" cp server/{run.sh,router.php,nginx.conf.in,php-fpm.conf.in,init-sqlite.php,patients.sqlite.sql,sqlite-database.php,sqlite-rest.php} \ "$php_resources/" mkdir "$php_resources/runtime" ditto "$runtime/www" "$php_resources/runtime/www" cp "$runtime/index.html" "$php_resources/runtime/index.html" cp licenses.md "$app/Contents/Resources/licenses.md" application="$php_resources/runtime/www/ezwebserver/src/application" rm -rf "$application/logs" "$application/cache/sessions" mkdir -p "$application/logs" "$application/cache/sessions" codesign --force --sign - "$app" codesign --verify --deep --strict "$app" ditto -c -k --keepParent "$app" "$stage/EzServerMac-universal.zip" rm -rf dist/EzServer.app mv "$app" dist/EzServer.app mv "$stage/EzServerMac-universal.zip" dist/EzServerMac-universal.zip printf '%s\n' "$PWD/dist/EzServerMac-universal.zip"