#!/bin/sh -e
# A hacky script to generate .pkg.tar.?z files using satellites.
# It's a fairly simple wrapper to astronaut, that generates a pacman package at the end.

# This may also serve as an example on how you can wrap astronaut to package in any format.
# Sure, no dependency resolution, and the packages won't make it in any official repositories,
#   but it isn't meant for that anyway.

_topdir=$PWD
_astronaut="$(dirname "$0")/astronaut"
[ ! -f "$_astronaut" ] && _astronaut="astronaut"

_nuke_dir_install=true

. "$_astronaut"

# _dir_pacman should be set from a configuration file (e.g. ~/.astronaut.conf), and points to wherever you want your packages to be placed.
[ "$_dir_pacman" ] && mkdir -p "$_dir_pacman" && cd "$_dir_pacman" || cd "$_topdir"

cat << EOF > "$dir_install/.PKGINFO"
pkgname = $_satname
pkgver = $version-astro
pkgdesc = Generated with pacman-astronaut
url = $update_url
builddate = $(date -u '+%s')
size = $(du -sb --apparent-size "$dir_install" | awk '{print $1}')
arch = $(uname -m)
EOF

bsdtar --strip-components 1 -C "$dir_install" -czf .MTREE --format=mtree --options='!all,use-set,type,uid,gid,mode,time,size,md5,sha256,link' .
mv .MTREE "$dir_install/.MTREE"  # bsdtar doesn't like the file being in the same directory.
fakeroot -- bsdtar --strip-components 1 -C "$dir_install" -cf - . > "$_satname-$version-astro-$(uname -m).pkg.tar"
repo-add astronaut.db.tar.gz "$_satname-$version-astro-$(uname -m).pkg.tar"