AtlatestRepositorysigil-wm

sigil-wm / tree / scriptsrun-sigil

1#!/usr/bin/env bash
2set -euo pipefail
3cd "$(dirname "$0")/.."
4
5# The static-musl distribution intentionally has no dlopen support. Use the
6# same release's glibc artifact, without changing the user's selected runtime.
7version=0.23.0
8expected_sha=3a8eeede1ab6c54e8f91ea1a9010d957f365a213489821bdaa1a0e4d17b05e25
9runtime=build/runtime/sigil-linux-amd64
10loader=${GUIX_ENVIRONMENT:?Run inside the pinned Guix shell}/lib/ld-linux-x86-64.so.2
11[[ -x $loader ]]
12mkdir -p build/runtime build/receipts
13if [[ ! -f $runtime ]] || ! printf '%s %s\n' "$expected_sha" "$runtime" | sha256sum --check --status; then
14 download="$runtime.download.$$"
15 trap 'rm -f "$download"' EXIT
16 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 >&2
21 chmod +x "$download"
22 mv "$download" "$runtime"
23 trap - EXIT
24fi
25printf '%s %s\n' "$expected_sha" "$runtime" | sha256sum --check >&2
26sha256sum "$runtime" > build/receipts/sigil-original.sha256
27printf '%s\n' "$loader" > build/receipts/sigil-loader
29# Executing ld.so directly changes /proc/self/exe, which Sigil uses to find
30# its embedded modules. Patch a separate ELF prefix, then restore the exact
31# bundled ZIP and update its absolute-offset trailer. Never modify the asset.
32[[ $(tail -c 16 "$runtime" | head -c 6) == SGLPAK ]]
33offset=$(tail -c 8 "$runtime" | od --endian=little -An -tu8 | tr -d ' ')
34original_size=$(stat -c %s "$runtime")
35[[ $offset -gt 0 && $offset -lt $((original_size - 16)) ]]
36archive_size=$((original_size - offset - 16))
37patched="$runtime.runnable.$$"
38trap 'rm -f "$patched"' EXIT
39head -c "$offset" "$runtime" > "$patched"
40patchelf --set-interpreter "$loader" "$patched"
41patched_offset=$(stat -c %s "$patched")
42dd if="$runtime" bs=64K skip="$offset" count="$archive_size" \
43 iflag=skip_bytes,count_bytes status=none >> "$patched"
44printf 'SGLPAK\0\0' >> "$patched"
45for ((index = 0; index < 8; index++)); do
46 printf -v octet '\\%03o' "$(((patched_offset >> (index * 8)) & 255))"
47 printf '%b' "$octet" >> "$patched"
48done
49original_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)
51runnable_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" ]]
54printf '%s\n' "$original_archive_sha" > build/receipts/sigil-archive.sha256
55chmod +x "$patched"
56mv "$patched" "$runtime.runnable"
57trap - EXIT
58sha256sum "$runtime.runnable" > build/receipts/sigil-executable.sha256
59printf 'original-offset=%s\nrunnable-offset=%s\narchive-bytes=%s\n' \
60 "$offset" "$patched_offset" "$archive_size" > build/receipts/sigil-bundle
61# Pure-FFI text bindings use this pinned runtime's FFI. A prebuilt bundle
62# carries their checked sources alongside the native library.
63export LD_LIBRARY_PATH="${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}$GUIX_ENVIRONMENT/lib"
64text_paths=()
65[[ ! -d build/text-lib ]] || text_paths=(-L "$PWD/build/text-lib")
66exec "$runtime.runnable" "${text_paths[@]}" "$@"