#!/bin/sh
. "$(dirname $0)"/libcomain
tbname="$name-$version"
if test $# -ge 1; then
from="$1"
tbname="$tbname++$from"
shift
else
from=master
fi
if test $# -gt 0; then die 1 "usage: $0 [branch]"; fi
echo " => Prepare new directory..."
tb="$(pwd)/$tbname"
if test -e $tb; then error 2 "File '$tb' already exists"; fi
if test -e $tb.tar.xz; then error 2 "File '$tb.tar.xz' already exists"; fi
if test ! -e meta/desc; then error 2 "File 'meta/desc' missing"; fi
echo " -> Check out new worktree (branch tarball from $from)..."
run git worktree add $tb -b tarball $from
addcpnt()
{
local dep=$2
local depname="${dep##*/}"
local ref="$(cat $dep/ref 2>/dev/null)"
if test -z "$ref"; then ref="$(cat $dep/version)"; fi
echo " -> Check out $depname new worktree (branch tarball from $ref)..."
tryrun mkdir -p $tb/deps
local dst=$tb/$depname
tryrun git -C ../deps/$depname worktree add $dst.full -b tarball $ref
tryrun ln -s ../$depname $tb/deps
if test -e $dep/files; then
echo " -> Copy needed files..."
tryrun mkdir $dst
while IFS= read -r file; do
tryrun mkdir -p $dst/$(dirname $file)
tryrun cp $dst.full/$file $dst/$file
done < $dep/files
tryrun mkdir $dst/meta
tryrun cp $dep/version $dst/meta
else
echo " -> Copy files..."
tryrun cp -aR $dst.full $dst
fi
echo " -> Remove $depname worktree..."
tryrun git -C ../deps/$depname worktree remove --force $dst.full
tryrun git -C ../deps/$depname branch -D tarball
}
echo " -> Add components..."
cd $tb
loaddeps
foreach cpnt $nb_cpnt addcpnt 0
cd ..
echo " -> Set up configure & common.mk..."
make -C $tb
echo " -> Generate README..."
test -e $tb/README && tryrun rm $tb/README
tryrun "$(dirname $0)"/mkreadme > $tb/README
if ! test -e $tb/src/include; then mkdir $tb/src/include; fi
echo " => Create tarball..."
tryrun tar -cJp --owner=0 --group=0 --numeric-owner --exclude=".git"* -f $tb.tar.xz "$tbname"
echo " => Clean up..."
echo " -> Remove worktree & branch tarball..."
tryrun git worktree remove --force $tb
tryrun git branch -D tarball
echo "done."