#!/usr/bin/env bash set -euo pipefail if [[ $# != 6 ]]; then echo "usage: $0 STAGE ARTIFACT_BASE SOURCE_DATE_EPOCH ARCHIVE INSTALLER WORK_DIR" >&2 exit 2 fi stage=$(realpath -e -- "$1") artifact_base=$2 source_date_epoch=$3 archive=$(realpath -m -- "$4") installer=$(realpath -m -- "$5") work_dir=$(realpath -m -- "$6") [[ $artifact_base =~ ^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$ ]] || { echo "invalid artifact base: $artifact_base" >&2 exit 2 } [[ $source_date_epoch =~ ^[0-9]+$ ]] || { echo "SOURCE_DATE_EPOCH must be a non-negative integer" >&2 exit 2 } [[ -x $stage/install.sh && -x $stage/uninstall.sh ]] || { echo "stage is missing executable installer helpers" >&2 exit 2 } [[ -f $stage/manifest.json && -x $stage/bin/echoiond && -x $stage/bin/echoion-updater && -f $stage/lib/Echoion.ts ]] || { echo "stage is missing required Echoion payload files" >&2 exit 2 } mkdir -p -- "$(dirname -- "$archive")" "$(dirname -- "$installer")" "$work_dir" archive_tmp="$(dirname -- "$archive")/.$(basename -- "$archive").tmp.$$" installer_tmp="$(dirname -- "$installer")/.$(basename -- "$installer").tmp.$$" gzip_payload="$work_dir/$artifact_base.tar.gz.$$" header="$work_dir/$artifact_base.run-header.$$" cleanup() { rm -f -- "$archive_tmp" "$installer_tmp" "$gzip_payload" "$header" } trap cleanup EXIT tar_options=( --sort=name "--mtime=@$source_date_epoch" --owner=0 --group=0 --numeric-owner --format=posix '--pax-option=delete=atime,delete=ctime' ) tar -C "$stage" "${tar_options[@]}" \ --transform="s,^\\.$,$artifact_base," \ --transform="s,^\\./,$artifact_base/," \ -cf - . | zstd -T1 -19 --no-progress -o "$archive_tmp" chmod 0644 "$archive_tmp" mv -f -- "$archive_tmp" "$archive" tar -C "$stage" "${tar_options[@]}" -cf - . | gzip -n -9 >"$gzip_payload" cat >"$header" <<'RUN_HEADER' #!/bin/sh set -eu self=$0 case $self in /*) ;; */*) self_directory=$(CDPATH= cd -P -- "$(dirname -- "$self")" && pwd -P) self=$self_directory/$(basename -- "$self") ;; *) self=$(command -v "$self") || { echo "Cannot locate Echoion installer: $0" >&2 exit 2 } case $self in /*) ;; *) self=$(pwd)/$self ;; esac ;; esac temporary=$(mktemp -d "${TMPDIR:-/tmp}/echoion-installer.XXXXXX") cleanup() { rm -rf -- "$temporary" } trap cleanup EXIT trap 'exit 129' HUP trap 'exit 130' INT trap 'exit 143' TERM payload_line=$(awk '/^__ECHOION_PAYLOAD_BELOW__$/ { print NR + 1; exit }' "$self") if [ -z "$payload_line" ]; then echo "Echoion installer payload marker is missing" >&2 exit 2 fi tail -n "+$payload_line" "$self" | tar --same-permissions --no-same-owner -xzf - -C "$temporary" "$temporary/install.sh" "$@" exit 0 __ECHOION_PAYLOAD_BELOW__ RUN_HEADER cat "$header" "$gzip_payload" >"$installer_tmp" chmod 0755 "$installer_tmp" mv -f -- "$installer_tmp" "$installer" trap - EXIT