#!/usr/bin/env bash
set -euo pipefail

package_root="$(cd "$(dirname "$0")" && pwd)"
output="$package_root/build/MarvelPBX-12.6.0.zip"
temporary="$(mktemp -d)"
trap 'rm -rf -- "$temporary"' EXIT

install -m 0644 "$package_root/manifest.xml" "$temporary/manifest.xml"
cp -a "$package_root/src/." "$temporary/"
find "$temporary" -type d -exec chmod 0755 {} +
find "$temporary" -type f -exec chmod 0644 {} +

mkdir -p "$package_root/build"
rm -f -- "$output.tmp"
(
    cd "$temporary"
    zip -X -q -r "$output.tmp" manifest.xml modules layouts languages
)
mv -- "$output.tmp" "$output"
unzip -tq "$output"
printf 'Built %s\n' "$output"
