#!/bin/sh -e
#
# script to generate CRUX-MUSL rootfs

HERE=$PWD
ROOTFS=/tmp/rootfs
TARBALL=$HERE/CRUX-MUSL-$(date +%Y%m%d).tar.xz

rm -rf $ROOTFS
mkdir -p $ROOTFS/var/lib/pkg
touch $ROOTFS/var/lib/pkg/db

# avoid installing obsolete libtool files (*.la) that caused continuous broken build
# and on musl some packages generates same /usr/lib/charset.alias files that cause file conflicts
echo "# tmp pkgadd.conf
INSTALL ^usr/lib/charset.alias NO
" > /tmp/pkgadd.conf

echo "# tmp prt-get.conf
prtdir /usr/ports/musl
prtdir /usr/ports/core
runscripts no

addcommand pkgadd -c /tmp/pkgadd.conf
" > /tmp/prt-get.conf

prt-get depinst --ignore=glibc,libxcrypt --config=/tmp/prt-get.conf --install-root=$ROOTFS $(cat basepkg)

chroot $ROOTFS revdep-musl -v

cd $ROOTFS
echo "Compressing CRUX-MUSL, please wait..."
tar -cJpf $TARBALL *
echo "Done: $TARBALL"

rm -f /tmp/prt-get.conf /tmp/pkgadd.conf
rm -rf $ROOTFS

exit 0
