#!/bin/sh
# mkroot: pack files of THIS (Alpine) system into a .gpkg, for build
# tests (BUILD_DESIGN.txt section 6): a build layer needs real programs,
# and Everest has no toolchain packages yet.
#
#   mkroot [-o DIR] [-p APK]... [-r APK]... [-f PATH]... NAME VERSION REPO
#
#   -o DIR    write DIR/NAME-VERSION.gpkg (+ .sha256; default: .)
#   -p APK    every file the installed Alpine package APK owns
#             (apk info -L), with its symlinks
#   -r APK    the same for APK and everything it depends on
#   -f PATH   one file or symlink (absolute path)
#
# Paths are packed as the merged-/usr layout glacier uses: /bin, /sbin,
# /usr/sbin -> usr/bin, /lib -> usr/lib. For busybox, a symlink per
# applet is added (Alpine makes them at install time, not in the apk).
#
# Examples:
#   mkroot -o /srv -p musl -p busybox testroot 1.0.0 extra
#   mkroot -o /srv -p gcc -p binutils -p make toolchain 1.0.0 extra

usage() { echo "usage: mkroot [-o DIR] [-p APK]... [-r APK]... [-f PATH]... NAME VERSION REPO" >&2; exit 1; }

# The installed package owning a file, by name.
owner() { apk info -qW "$1" 2>/dev/null | head -1 | sed 's/-[0-9][^-]*-r[0-9]*$//'; }

# APK and everything it depends on (installed packages only).
closure() {
	todo=$1 done=" "
	while [ -n "$todo" ]; do
		set -- $todo; p=$1; shift; todo=$*
		case "$done" in *" $p "*) continue ;; esac
		done="$done$p "
		for d in $(apk info -qR "$p" 2>/dev/null); do
			d=${d%%[<>=~]*}
			case $d in
			so:*)
				f=$(ls /lib/${d#so:} /usr/lib/${d#so:} 2>/dev/null | head -1)
				[ -n "$f" ] && d=$(owner "$f") || continue ;;
			*:*) continue ;;
			esac
			[ -n "$d" ] && todo="$todo $d"
		done
	done
	echo $done
}

out=.
apks=""
files=""
while getopts o:p:r:f:h opt; do
	case $opt in
	o) out=$OPTARG ;;
	p) apks="$apks $OPTARG" ;;
	r) apks="$apks $(closure "$OPTARG")" ;;
	f) files="$files $OPTARG" ;;
	*) usage ;;
	esac
done
shift $((OPTIND - 1))
[ $# -eq 3 ] || usage
name=$1 ver=$2 repo=$3

work=$(mktemp -d) || exit 1
trap 'rm -rf "$work"' EXIT
dir=$work/$name-$ver
mkdir -p "$dir/files/usr/bin" "$dir/files/usr/lib"

# The merged-/usr place of an absolute path.
dest() {
	case $1 in
	/bin/*|/sbin/*|/usr/sbin/*) echo "usr/bin/${1##*/}" ;;
	/lib/*) echo "usr/lib/${1#/lib/}" ;;
	*) echo "${1#/}" ;;
	esac
}

list=$work/list
: > "$list"
for p in $apks; do
	apk info -qL "$p" 2>/dev/null | sed -n 's|^|/|p' >> "$list" || exit 1
done
for f in $files; do echo "$f" >> "$list"; done

while read -r f; do
	[ -e "$f" ] || [ -L "$f" ] || continue
	[ -d "$f" ] && [ ! -L "$f" ] && continue
	d=$dir/files/$(dest "$f")
	mkdir -p "${d%/*}"
	cp -P "$f" "$d" 2>/dev/null || true
done < "$list"

if [ -e "$dir/files/usr/bin/busybox" ]; then
	for a in $(busybox --list); do
		[ -e "$dir/files/usr/bin/$a" ] || ln -s busybox "$dir/files/usr/bin/$a"
	done
fi

printf 'PKG_NAME = "%s";\nPKG_VER = "%s";\nPKG_DESC = "mkroot test package";\nPKG_MAINT = "mkroot@localhost";\nPKG_COPYRIGHT = "various";\nPKG_REPO = "%s";\nPKG_DEPS = ();\nPKG_CONFS = ();\n' \
	"$name" "$ver" "$repo" > "$dir/manifest.gpm.cfg"

mkdir -p "$out" || exit 1
pkg=$out/$name-$ver.gpkg
tar czf "$pkg" -C "$work" "$name-$ver" || exit 1
printf 'sha256:%s\n' "$(sha256sum "$pkg" | cut -d' ' -f1)" > "$pkg.sha256"
chmod 644 "$pkg" "$pkg.sha256"
echo "$pkg"
