AtlatestRepositorysigil-wm
1
#!/usr/bin/env bash2
set -euo pipefail3
cd "$(dirname "$0")/.."5
# The static-musl distribution intentionally has no dlopen support. Use the6
# same release's glibc artifact, without changing the user's selected runtime.7
version=0.23.08
expected_sha=3a8eeede1ab6c54e8f91ea1a9010d957f365a213489821bdaa1a0e4d17b05e259
runtime=build/runtime/sigil-linux-amd6410
loader=${GUIX_ENVIRONMENT:?Run inside the pinned Guix shell}/lib/ld-linux-x86-64.so.211
[[ -x $loader ]]12
mkdir -p build/runtime build/receipts13
if [[ ! -f $runtime ]] || ! printf '%s %s\n' "$expected_sha" "$runtime" | sha256sum --check --status; then14
download="$runtime.download.$$"15
trap 'rm -f "$download"' EXIT16
curl --fail --location --silent --show-error --retry 2 \17
--connect-timeout 10 --max-time 120 \18
"https://codeberg.org/sigil/sigil/releases/download/v$version/sigil-linux-amd64" \19
--output "$download"20
printf '%s %s\n' "$expected_sha" "$download" | sha256sum --check >&221
chmod +x "$download"22
mv "$download" "$runtime"23
trap - EXIT24
fi25
printf '%s %s\n' "$expected_sha" "$runtime" | sha256sum --check >&226
sha256sum "$runtime" > build/receipts/sigil-original.sha25627
printf '%s\n' "$loader" > build/receipts/sigil-loader29
# Executing ld.so directly changes /proc/self/exe, which Sigil uses to find30
# its embedded modules. Patch a separate ELF prefix, then restore the exact31
# bundled ZIP and update its absolute-offset trailer. Never modify the asset.32
[[ $(tail -c 16 "$runtime" | head -c 6) == SGLPAK ]]33
offset=$(tail -c 8 "$runtime" | od --endian=little -An -tu8 | tr -d ' ')34
original_size=$(stat -c %s "$runtime")35
[[ $offset -gt 0 && $offset -lt $((original_size - 16)) ]]36
archive_size=$((original_size - offset - 16))37
patched="$runtime.runnable.$$"38
trap 'rm -f "$patched"' EXIT39
head -c "$offset" "$runtime" > "$patched"40
patchelf --set-interpreter "$loader" "$patched"41
patched_offset=$(stat -c %s "$patched")42
dd if="$runtime" bs=64K skip="$offset" count="$archive_size" \43
iflag=skip_bytes,count_bytes status=none >> "$patched"44
printf 'SGLPAK\0\0' >> "$patched"45
for ((index = 0; index < 8; index++)); do46
printf -v octet '\\%03o' "$(((patched_offset >> (index * 8)) & 255))"47
printf '%b' "$octet" >> "$patched"48
done49
original_archive_sha=$(dd if="$runtime" bs=64K skip="$offset" count="$archive_size" \50
iflag=skip_bytes,count_bytes status=none | sha256sum | cut -d ' ' -f 1)51
runnable_archive_sha=$(dd if="$patched" bs=64K skip="$patched_offset" count="$archive_size" \52
iflag=skip_bytes,count_bytes status=none | sha256sum | cut -d ' ' -f 1)53
[[ $original_archive_sha == "$runnable_archive_sha" ]]54
printf '%s\n' "$original_archive_sha" > build/receipts/sigil-archive.sha25655
chmod +x "$patched"56
mv "$patched" "$runtime.runnable"57
trap - EXIT58
sha256sum "$runtime.runnable" > build/receipts/sigil-executable.sha25659
printf 'original-offset=%s\nrunnable-offset=%s\narchive-bytes=%s\n' \60
"$offset" "$patched_offset" "$archive_size" > build/receipts/sigil-bundle61
# Pure-FFI text bindings use this pinned runtime's FFI. A prebuilt bundle62
# carries their checked sources alongside the native library.63
export LD_LIBRARY_PATH="${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}$GUIX_ENVIRONMENT/lib"64
text_paths=()65
[[ ! -d build/text-lib ]] || text_paths=(-L "$PWD/build/text-lib")66
exec "$runtime.runnable" "${text_paths[@]}" "$@"