Files
strawberry/build_tools/macos/export_compliance/make_pdf.sh

97 lines
2.5 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<'EOF'
Usage:
./build_tools/macos/export_compliance/make_pdf.sh \
--bundle-id com.dryark.strawberry \
--version 1.2.3 \
--developer "Dry Ark LLC" \
--contact "support@example.com"
Outputs (in the same folder as this script):
- EXPORT_COMPLIANCE.filled.txt
- EXPORT_COMPLIANCE.pdf
Notes:
- Uses macOS built-in /usr/sbin/cupsfilter to generate the PDF from plain text.
EOF
}
script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
template="${script_dir}/EXPORT_COMPLIANCE.txt"
filled="${script_dir}/EXPORT_COMPLIANCE.filled.txt"
pdf="${script_dir}/EXPORT_COMPLIANCE.pdf"
tmp_html="${script_dir}/EXPORT_COMPLIANCE.tmp.html"
bundle_id=""
version=""
developer=""
contact=""
date_str="$(date +%Y-%m-%d)"
while [[ $# -gt 0 ]]; do
case "$1" in
--bundle-id) bundle_id="${2:-}"; shift 2 ;;
--version) version="${2:-}"; shift 2 ;;
--developer) developer="${2:-}"; shift 2 ;;
--contact) contact="${2:-}"; shift 2 ;;
-h|--help) usage; exit 0 ;;
*) echo "Unknown arg: $1" >&2; usage; exit 2 ;;
esac
done
if [[ -z "$bundle_id" || -z "$version" || -z "$developer" || -z "$contact" ]]; then
echo "Error: missing required args." >&2
usage
exit 2
fi
if [[ "$(uname -s)" != "Darwin" ]]; then
echo "Error: This script is for macOS only (uses textutil)." >&2
exit 1
fi
if [[ ! -f "$template" ]]; then
echo "Error: missing template file: $template" >&2
exit 1
fi
if [[ ! -x /usr/sbin/cupsfilter ]]; then
echo "Error: /usr/sbin/cupsfilter not found. This should exist on macOS." >&2
exit 1
fi
escape_sed_repl() {
# Escape characters that are special in sed replacement strings: \ & and delimiter |
# bash 3.2 compatible
echo "$1" | sed -e 's/[\\&|]/\\&/g'
}
bundle_id_esc="$(escape_sed_repl "$bundle_id")"
version_esc="$(escape_sed_repl "$version")"
developer_esc="$(escape_sed_repl "$developer")"
contact_esc="$(escape_sed_repl "$contact")"
date_esc="$(escape_sed_repl "$date_str")"
sed \
-e "s|@BUNDLE_ID@|${bundle_id_esc}|g" \
-e "s|@VERSION@|${version_esc}|g" \
-e "s|@DEVELOPER@|${developer_esc}|g" \
-e "s|@CONTACT@|${contact_esc}|g" \
-e "s|@DATE@|${date_esc}|g" \
"$template" > "$filled"
rm -f "$pdf" >/dev/null 2>&1 || true
rm -f "$tmp_html" >/dev/null 2>&1 || true
# Convert plain text to PDF. cupsfilter writes PDF to stdout.
# Suppress noisy DEBUG output from cupsfilter on stderr.
/usr/sbin/cupsfilter -i text/plain -m application/pdf "$filled" > "$pdf" 2>/dev/null
echo "Wrote:"
echo " $filled"
echo " $pdf"