|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# update-flathub-manifest.sh |
| 5 | +# - copy all regular files (not directories) from Properties/linux/flatpak -> Properties/linux/flathub |
| 6 | +# - download (or read locally) flathub-bundle.tar.gz, compute sha256 and update |
| 7 | +# `Properties/linux/flathub/io.github.HyPrismTeam.HyPrism.yml` by replacing the placeholder |
| 8 | + |
| 9 | +usage() { |
| 10 | + cat <<EOF |
| 11 | +Usage: $0 [-s SRC_DIR] [-d DST_DIR] [-a ARCHIVE_URL_OR_PATH] [-r README_SRC] |
| 12 | +
|
| 13 | +Options: |
| 14 | + -s SRC_DIR source folder with flatpak files (default: Properties/linux/flatpak) |
| 15 | + -d DST_DIR destination folder for flathub files (default: Properties/linux/flathub) |
| 16 | + -a ARCHIVE URL or local path to flathub-bundle.tar.gz (optional — if given the |
| 17 | + script will download/inspect it and insert the sha256 into the manifest) |
| 18 | + -r README_SRC optional path to a README file which will replace the README.md in the |
| 19 | + destination folder (useful to replace with the repo README) |
| 20 | + -h show this help |
| 21 | +
|
| 22 | +Examples: |
| 23 | + # copy files only |
| 24 | + $0 |
| 25 | +
|
| 26 | + # copy files + download archive and patch manifest sha256 |
| 27 | + $0 -a https://github.com/OWNER/REPO/releases/download/vX.Y/flathub-bundle.tar.gz |
| 28 | +
|
| 29 | + # copy files and replace README.md in the flathub folder |
| 30 | + $0 -r README.md |
| 31 | +EOF |
| 32 | + exit 1 |
| 33 | +} |
| 34 | + |
| 35 | +SRC_DIR="Properties/linux/flatpak" |
| 36 | +DST_DIR="Properties/linux/flathub" |
| 37 | +ARCHIVE="" |
| 38 | +README_SRC="" |
| 39 | + |
| 40 | +while getopts ":s:d:a:r:h" opt; do |
| 41 | + case $opt in |
| 42 | + s) SRC_DIR="$OPTARG" ;; |
| 43 | + d) DST_DIR="$OPTARG" ;; |
| 44 | + a) ARCHIVE="$OPTARG" ;; |
| 45 | + r) README_SRC="$OPTARG" ;; |
| 46 | + h) usage ;; |
| 47 | + \?) echo "Invalid option: -$OPTARG" >&2; usage ;; |
| 48 | + esac |
| 49 | +done |
| 50 | + |
| 51 | +mkdir -p "$DST_DIR" |
| 52 | + |
| 53 | +# 1) copy regular files (no directories) |
| 54 | +echo "Copying regular files from '$SRC_DIR' -> '$DST_DIR'..." |
| 55 | +if [ ! -d "$SRC_DIR" ]; then |
| 56 | + echo "Source directory '$SRC_DIR' does not exist." >&2 |
| 57 | + exit 2 |
| 58 | +fi |
| 59 | +find "$SRC_DIR" -maxdepth 1 -type f -exec cp -a -- '{}' "$DST_DIR/" \; |
| 60 | + |
| 61 | +# optional: replace README in destination with provided source |
| 62 | +if [ -n "${README_SRC:-}" ]; then |
| 63 | + if [ -f "$README_SRC" ]; then |
| 64 | + echo "Replacing README in '$DST_DIR' with '$README_SRC'" |
| 65 | + cp -a "$README_SRC" "$DST_DIR/README.md" |
| 66 | + else |
| 67 | + echo "README source not found: $README_SRC" >&2 |
| 68 | + exit 5 |
| 69 | + fi |
| 70 | +fi |
| 71 | + |
| 72 | +MANIFEST="$DST_DIR/io.github.HyPrismTeam.HyPrism.yml" |
| 73 | +# if no manifest in DST, try to copy from SRC |
| 74 | +if [ ! -f "$MANIFEST" ]; then |
| 75 | + if [ -f "$SRC_DIR/io.github.HyPrismTeam.HyPrism.yml" ]; then |
| 76 | + echo "Creating manifest '$MANIFEST' by copying from $SRC_DIR" |
| 77 | + cp -a "$SRC_DIR/io.github.HyPrismTeam.HyPrism.yml" "$MANIFEST" |
| 78 | + else |
| 79 | + echo "Manifest not found in source or destination: $MANIFEST" >&2 |
| 80 | + exit 3 |
| 81 | + fi |
| 82 | +fi |
| 83 | + |
| 84 | +# helper: insert/replace sha256 in manifest |
| 85 | +insert_sha_in_manifest() { |
| 86 | + local sha="$1" |
| 87 | + # 3 possible states in manifest: |
| 88 | + # 1) placeholder `REPLACE_WITH_FLATHUB_BUNDLE_SHA256` — replace it |
| 89 | + # 2) already `type: archive` — update sha field |
| 90 | + # 3) still has the two `type: dir` blocks — replace those with archive + sha |
| 91 | + |
| 92 | + if grep -q "REPLACE_WITH_FLATHUB_BUNDLE_SHA256" "$MANIFEST"; then |
| 93 | + sed -i "s/REPLACE_WITH_FLATHUB_BUNDLE_SHA256/$sha/" "$MANIFEST" |
| 94 | + return |
| 95 | + fi |
| 96 | + |
| 97 | + if grep -q "type: archive" "$MANIFEST"; then |
| 98 | + # replace existing sha value |
| 99 | + sed -i -E "s/(sha256:\s*)[a-f0-9]{64}/\1$sha/" "$MANIFEST" |
| 100 | + return |
| 101 | + fi |
| 102 | + |
| 103 | + # replace the two `type: dir` blocks with the archive block |
| 104 | + perl -0777 -pe "s{(\n\s*sources:\n)\s*- type: dir\n\s*path: ../../../bin/Release/net10.0/linux-x64/publish/linux-unpacked\n\s*dest: linux-unpacked\n\s*- type: dir\n\s*path: ../../../wwwroot\n\s*dest: wwwroot\n}{\1 - type: archive\n url: release, latest, flathub-bundle.tar.gz\n sha256: $sha\n}s" -i "$MANIFEST" |
| 105 | +} |
| 106 | + |
| 107 | +# 2) if archive provided -> download (or use local path), compute sha and patch manifest |
| 108 | +if [ -n "$ARCHIVE" ]; then |
| 109 | + TMPDIR="$(mktemp -d)" |
| 110 | + trap 'rm -rf "$TMPDIR"' EXIT |
| 111 | + |
| 112 | + if [[ "$ARCHIVE" =~ ^https?:// ]]; then |
| 113 | + TARPATH="$TMPDIR/flathub-bundle.tar.gz" |
| 114 | + echo "Downloading archive from $ARCHIVE..." |
| 115 | + curl -fL -o "$TARPATH" "$ARCHIVE" |
| 116 | + else |
| 117 | + TARPATH="$ARCHIVE" |
| 118 | + if [ ! -f "$TARPATH" ]; then |
| 119 | + echo "Archive file not found: $TARPATH" >&2 |
| 120 | + exit 4 |
| 121 | + fi |
| 122 | + fi |
| 123 | + |
| 124 | + echo "Computing sha256..." |
| 125 | + if command -v sha256sum >/dev/null 2>&1; then |
| 126 | + SHA256="$(sha256sum "$TARPATH" | awk '{print $1}')" |
| 127 | + else |
| 128 | + SHA256="$(shasum -a 256 "$TARPATH" | awk '{print $1}')" |
| 129 | + fi |
| 130 | + echo "sha256=$SHA256" |
| 131 | + |
| 132 | + # quick verification that the tar contains both expected directories |
| 133 | + if tar -tzf "$TARPATH" | awk -F/ '{print $1}' | sort -u | grep -E "^(wwwroot|linux-unpacked)$" >/dev/null; then |
| 134 | + echo "Archive contains expected entries (wwwroot, linux-unpacked)" |
| 135 | + else |
| 136 | + echo "Warning: archive does NOT appear to contain both 'wwwroot' and 'linux-unpacked' (continuing)" >&2 |
| 137 | + fi |
| 138 | + |
| 139 | + insert_sha_in_manifest "$SHA256" |
| 140 | + echo "Patched manifest: $MANIFEST" |
| 141 | +else |
| 142 | + echo "No archive provided (-a); manifest left with placeholder or will contain archive block after manual update." |
| 143 | +fi |
| 144 | + |
| 145 | +echo "Done. Files copied to '$DST_DIR' and manifest updated (if -a was used)." |
| 146 | +exit 0 |
0 commit comments