#!/bin/sh
# Author: Didier Spaier, Paris, France
#
# this version inspired by this script from George Vlahavas:
# https://github.com/gapan/iso-creation/blob/master/01-downloadpackages.sh
# previous version is build_list_of_packages.sh.prev in the same directory.
# But while Salix arrange the packages in the ISO in sub directories of salix,
# we will instead put all of them directly in slint
. ./01_SetVariables.sh
USER=$(stat -c %U .)
[ "$(id -u)" -ne 0 ] && echo "Only root can run this script." && exit
[ -d "$ISODIR" ] && echo "Please (re)move $ISODIR first." && exit
mkdir -p "$ISODIR/slint/locales"
echo "Copy in $ISODIR the packages to put in the ISO"
slapt-get -u -c "$ROOTDIR"/slapt-getrc.iso
slapt-get --clean
rm -f /tmp/errors_from_populate_iso.sh
for SET in bootstrap core full; do
	while read -r i; do
		slapt-get -d --no-dep --reinstall -c slapt-getrc.iso -i "$i"
	done < "$ROOTDIR"/files-in-initrd/"$SET" 2>>/tmp/errors_from_populate_iso.sh
done
if [ -s /tmp/errors_from_populate_iso.sh ]; then
	echo "Some packages could not be found. They are listed in:"
	echo " check/tmp/errors_from_populate_iso.sh"
	exit
fi
find /var/slapt-get -name "*txz"|while read -r i; do
	cp "$i" "$ISODIR/slint"
done
echo "Copy in $ISODIR the packages in $SLACKROOT/extra/aspell-word-lists"
cp "$SLACKROOT"/extra/aspell-word-lists/*t?z "$ISODIR"/slint/locales
echo "Copy in $ISODIR the packages in $SLINTREPO/locales"
cp "$SLINTREPO"/locales/*t?z "$ISODIR"/slint/locales
echo "Copy in $ISODIR selected flite voice packages" "$ISODIR"/slint/locales
while read -r i; do
    cp "$SLINTREPO"/voices/flite/"${i}"*t?z "$ISODIR"/slint/locales
done <"$ROOTDIR"/doc/flite_voices_included_in_the_ISO
echo "Copy in $ISODIR selected mbrola voice packages"
while read -r i ; do
    cp "$SLINTREPO"/voices/mbrola/"${i}"*t?z "$ISODIR"/slint/locales
done < "$ROOTDIR"/doc/mbrola_voices_included_in_the_ISO
cp "$SLINTREPO"/DONOTREMOVEME.TXT "$ISODIR"/slint/
chown -R "$USER":users "$ISODIR"
