#!/bin/sh -ef
# Turn an installed package into a KISS tarball

db=$KISS_ROOT/var/db/kiss/installed
pkg=${1:-"${PWD##*/}"}

kiss list "$pkg" >/dev/null || {
    printf 'usage: kiss-export [pkg]\n'
    exit 1
}

read -r ver rel 2>/dev/null < "$db/$1/version"

set --

while read -r file; do
    [ -d "$KISS_ROOT/$file" ] || set -- "$@" ".$file"
done < "$db/$pkg/manifest"

cd "$KISS_ROOT/"

tar cf - "$@" | case ${KISS_COMPRESS:-gz} in
    bz2)  bzip2 -z ;;
    gz)   gzip -6  ;;
    lzma) lzma -z  ;;
    lz)   lzip -z  ;;
    xz)   xz -zT 0 ;;
    zst)  zstd -z  ;;
esac > "$OLDPWD/$pkg#$ver-$rel.tar.${KISS_COMPRESS:-gz}"

printf 'created %s\n' "$OLDPWD/$pkg#$ver-$rel.tar.${KISS_COMPRESS:-gz}"