#!/usr/bin/env zsh
# This script transforms the current directory into a full-blown apk package.
# It still requires a .PKGINFO file though.
set -e
# Output package name.
apk="$1"
# data.tar.gz
echo "Compressing data..."
set -- *
tar --xattrs -f - -c * | \
abuild-tar --hash | \
gzip -9 > data.tar.gz
echo "Create checksum..."
# append the hash for data.tar.gz
local sha256=$(sha256sum data.tar.gz | cut -f1 -d' ')
echo "datahash = $sha256" >> .PKGINFO
# control.tar.gz
cd "$dir"
metafiles=.PKGINFO
[ -f .metafiles ] && metafiles=$(cat .metafiles)
tar -f - -c ${metafiles[@]} | abuild-tar --cut \
| gzip -9 > control.tar.gz
abuild-sign -q control.tar.gz || exit 1
echo "Create $apk"
cat control.tar.gz data.tar.gz > $apk