#!/usr/bin/env bash
#
# Produce a .deb from a built Boulder plus helper files.
#
# This script expects to run on Ubuntu, as configured on GitHub Actions runners
# (with curl, make, and git installed).
#
# -e Stops execution in the instance of a command or pipeline error.
# -u Treat unset variables as an error and exit immediately.
set -eu
cd "$(realpath -- "$(dirname -- "$0")")/.."

if [ -z "${ARCH:-}" ]; then echo "ARCH not set"; exit 1; fi
if [ -z "${VERSION:-}" ]; then echo "VERSION not set"; exit 1; fi
if [ -z "${COMMIT_ID:-}" ]; then echo "COMMIT_ID not set"; exit 1; fi

BUILD="$(mktemp -d)"

mkdir -p "${BUILD}/opt"
cp -a /opt/boulder "${BUILD}/opt/boulder"

mkdir -p "${BUILD}/DEBIAN"
cat > "${BUILD}/DEBIAN/control" <<-EOF
Package: boulder
Version: 1:${VERSION}
License: Mozilla Public License v2.0
Vendor: ISRG
Architecture: ${ARCH}
Maintainer: Community
Section: default
Priority: extra
Homepage: https://github.com/letsencrypt/boulder
Description: Boulder is an ACME-compatible X.509 Certificate Authority
EOF

# Normalize timestamps for reproducible builds (after all files created)
find "${BUILD}" ! -type l -exec touch -d "@${SOURCE_DATE_EPOCH}" {} \;
find "${BUILD}" -type l -exec touch -h -d "@${SOURCE_DATE_EPOCH}" {} \;

dpkg-deb -Zgzip -b "${BUILD}" "boulder-${VERSION}-${COMMIT_ID}.${ARCH}.deb"
