Thanks to the hints in warl0ck's answer I wrote this script fakepkg:
#!/bin/bash
set -e
OLDDIR=$PWD
NEWDIR=$(mktemp -dt fakepack.XXX)
cd $NEWDIR
yaourt -G $1
cd $1
#TODO this can probably be retrieved from the pacman desc file
while true; do
read -p "Edit PKGBUILD? [yn]" -n1 yn
case $yn in
[Yy]* ) $EDITOR PKGBUILD; break;;
[Nn]* ) echo; break;;
* ) echo ;;
esac
done
. PKGBUILD
srcdir=$NEWDIR/$1/src
# In case you are wondering: this tries to compensate for packages not using
# a simple src/packagename-pkgver/ structure. It will probably still break...
PKSUB=$(eval "echo $(grep -o '\${\?srcdir.*pkgver}\?' PKGBUILD | head -n1 )")
SRC=${PKSUB##$NEWDIR/$1/}
echo "Putting the $1 source to $SRC"
PKG=$pkgname-$pkgver
# SRC="src/$PKG/"
FILES="/var/lib/pacman/local/$PKG-$pkgrel/files"
if [ ! -f $FILES ]; then
echo "$FILES not found, is $1 actually installed?"
exit 1
fi
#TODO use existing sources or skip this entirely
# but it's the easiest way to later use makepkg -R
echo "Reloading source"
makepkg -o
echo "Collecting $1 files"
mkdir -p $SRC/files/
while IFS= read -r line; do
if [ -f "/$line" ]; then
mkdir -p $SRC/files/$(dirname $line)
rsync -a /$line $SRC/files/$line
fi
done < $FILES
echo "Creating fake Makefile"
echo 'install:' > $SRC/Makefile
echo ' mv files/* $(DESTDIR)' >> $SRC/Makefile
rm -f $SRC/GNUmakefile
echo "Creating package"
makepkg -R
mv *.xz $OLDDIR
cd $OLDDIR
rm -rf $NEWDIR
Run fakepkg packagename, adapt PKGBUILD if necessary and hope for a package---.pak.tar.xz in your current directory.
There's plenty of room for improvements of course, e.g. automatically modifying PKGBUILD according to the installation's desc and not downloading the entire source. But for now it works ok enough.