system

the Chamesle handbook, init, live ISO and install helpers
git clone https://github.com/tortr-rs/system
Log | Files | Refs | README | LICENSE

mkapk (2870B)


      1 #!/bin/sh
      2 # mkapk: packs DIR into a package mag installs, signed with KEY, and leaves its index entry in OUT/.idx for mkrepo.
      3 #   mkapk -k KEY -n NAME -v VERSION -a ARCH -d DESC [-D DEPS] [-s SCRIPTS] [-t TRIGGERS] [-o OUT] DIR
      4 # SCRIPTS is a directory with any of post-install, pre-upgrade and the rest
      5 set -e
      6 # GNU tar, for --record-size and --sort
      7 tar=$(command -v gtar || echo tar)
      8 deps= scripts= triggers= out=.
      9 while getopts k:n:v:a:d:D:s:t:o: o; do
     10 	case $o in
     11 	k) key=$OPTARG;; n) name=$OPTARG;; v) ver=$OPTARG;; a) arch=$OPTARG;; d) desc=$OPTARG;;
     12 	D) deps=$OPTARG;; s) scripts=$OPTARG;; t) triggers=$OPTARG;; o) out=$OPTARG;;
     13 	*) exit 2;;
     14 	esac
     15 done
     16 shift $((OPTIND - 1))
     17 dir=$1
     18 if [ -z "$key" ] || [ -z "$name" ] || [ -z "$ver" ] || [ -z "$arch" ] || [ ! -d "$dir" ]; then
     19 	echo "usage: mkapk -k KEY -n NAME -v VERSION -a ARCH -d DESC [-D DEPS] [-s SCRIPTS] [-t TRIGGERS] [-o OUT] DIR" >&2
     20 	exit 2
     21 fi
     22 mkdir -p "$out/.idx"
     23 tmp=$(mktemp -d "$out/.mkapk.XXXXXX")
     24 trap 'rm -rf "$tmp"' EXIT
     25 mkdir "$tmp/c" "$tmp/s"
     26 
     27 # the signature and the control part are tar without the two zero blocks at the end, so the pieces run on into each other
     28 seg() { $tar --format=ustar --record-size=512 --owner=0 --group=0 --numeric-owner -C "$1" -cf - "$2" $3 | head -c -1024 | gzip -9n; }
     29 
     30 $tar --format=gnu --owner=0 --group=0 --numeric-owner --sort=name -C "$dir" -cf - . | gzip -9n > "$tmp/data.tar.gz"
     31 {
     32 	echo "pkgname = $name"
     33 	echo "pkgver = $ver"
     34 	echo "pkgdesc = $desc"
     35 	echo "url = https://chamesle.org"
     36 	echo "builddate = $(date +%s)"
     37 	echo "size = $(du -sb "$dir" | cut -f1)"
     38 	echo "arch = $arch"
     39 	echo "license = BSD-2-Clause"
     40 	for d in $deps; do echo "depend = $d"; done
     41 	[ -z "$triggers" ] || echo "triggers = $triggers"
     42 	echo "datahash = $(sha256sum "$tmp/data.tar.gz" | cut -d' ' -f1)"
     43 } > "$tmp/c/.PKGINFO"
     44 more=
     45 for s in pre-install post-install pre-upgrade post-upgrade pre-deinstall post-deinstall trigger; do
     46 	[ -n "$scripts" ] && [ -f "$scripts/$s" ] || continue
     47 	install -m755 "$scripts/$s" "$tmp/c/.$s"
     48 	more="$more .$s"
     49 done
     50 seg "$tmp/c" .PKGINFO "$more" > "$tmp/control.tar.gz"
     51 
     52 kn=$(basename "$key").pub
     53 openssl dgst -sha256 -sign "$key" -out "$tmp/s/.SIGN.RSA256.$kn" "$tmp/control.tar.gz"
     54 seg "$tmp/s" ".SIGN.RSA256.$kn" > "$tmp/sig.tar.gz"
     55 
     56 old=$(cat "$out/.idx/$name" 2>/dev/null | sed -n "s/^V://p")
     57 if [ -n "$old" ] && [ "$old" != "$ver" ]; then rm -f "$out/$name-$old.apk"; fi
     58 cat "$tmp/sig.tar.gz" "$tmp/control.tar.gz" "$tmp/data.tar.gz" > "$out/$name-$ver.apk"
     59 {
     60 	echo "C:Q1$(openssl dgst -sha1 -binary "$tmp/control.tar.gz" | base64 -w0)"
     61 	echo "P:$name"
     62 	echo "V:$ver"
     63 	echo "A:$arch"
     64 	echo "S:$(wc -c < "$out/$name-$ver.apk")"
     65 	echo "I:$(du -sb "$dir" | cut -f1)"
     66 	echo "T:$desc"
     67 	echo "U:https://chamesle.org"
     68 	echo "L:BSD-2-Clause"
     69 	if [ -n "$deps" ]; then echo "D:$(echo $deps)"; fi
     70 	echo
     71 } > "$out/.idx/$name"
     72 echo "$out/$name-$ver.apk"