#!/bin/sh
# Author: Didier Spaier, Paris, France

CWD=$(pwd)
SLACKVERSION="14.2"
OLDSLACKVERSION="14.2"
SLINTVERSION="14.2.1.3"
TAG="c"
TAGVERSION=${SLINTVERSION}$TAG
KVER=5.4.75
usage() {
  printf %b "Usage: $0"
  exit
}
if [ $# -ne 0 ]; then usage; fi
PATHTOISO=/data/images_iso/slint64-${TAGVERSION}.iso
SLINTREPO=/storage/repo/x86_64/slint-14.2.1/slint
SLACKREPO=/data/slackware64-current/slackware64
SOURCE=/storage/repo/x86_64/slint-14.2.1/source
MIRROR=/data/slackware64-$SLACKVERSION
SCRIPTS=/storage/repo/x86_64/slint-14.2.1/source/localization/scripts
KERNELMODULES=$SLINTREPO/kernel-modules*txz
DATESTAMP=`date -u +%Y%m%d`
if [ ! $UID -eq 0 ]; then
  printf "%b"  "Please execute this script as root.\n"
  exit
fi
TMP=$(mktemp -d) || exit 1
ISO=/data/iso64
. $SCRIPTS/SeTlocales
if [ ! -f $ISO/slint/DONOTREMOVEME.TXT ]; then
  echo "The file $ISO/slint/DONOTREMOVEME.TXT is missing,"
  echo "thus I can't check that the package tree fits the target system."
  exit
fi
if ! grep "Slint version: ${SLINTVERSION}" $ISO/slint/DONOTREMOVEME.TXT 1>/dev/null; then
  echo "Mismatch between the versions of the package tree and target system"
  exit
fi
if ! grep "System: 64" $ISO/slint/DONOTREMOVEME.TXT 1>/dev/null; then
 echo "Mismatch (32-bit vs 64-bit) between the package tree and the target system."
 exit
fi
# Put some docs at the root of the tree
sed "s/<date>/$DATESTAMP/;s/+version+/slint64-${TAGVERSION}.iso/" docs/CONTENT.TXT > $ISO/CONTENT.TXT
sed "s/+version+/$TAGVERSION/g" docs/README.TXT > $ISO/README.TXT
echo $DATESTAMP > $ISO/DateBuilt
rm -rf $ISO/boot/grub
mkdir -p $ISO/boot/grub
cp source/boot/grub/grub.cfg $ISO/boot/grub/
cp source/boot/grub/osdetect.cfg $ISO/boot/grub/
cp source/boot/grub/tools.cfg $ISO/boot/grub/
cp source/boot/grub/main.cfg $ISO/boot/grub/
cp source/boot/grub/instructions $ISO/boot/grub/
cp source/kernel/kernel-huge-${KVER}-x86_64-1/boot/vmlinuz-huge-$KVER \
$ISO/linux
# Third step: prepare and put in $TMP stuff needed for internationalization
# ==========================================
#
printf "%b" "\nPreparation in progress..."
mkdir $TMP/initrd
( cd $TMP/initrd
  xz -cd < $CWD/source/initrd/initrd-current-20191012.img| \
  cpio -i -d -m -H newc --no-absolute-filenames
)
cp /sbin/wipefs \
$TMP/initrd/usr/sbin
chmod 755 $TMP/initrd/usr/sbin/wipefs
rm -r $TMP/initrd/lib/modules
# TODO: cheery pick the libs we need instead of installing whole
# packages, to save some space
/sbin/installpkg --root $TMP/initrd $(find $SLINTREPO -name brltty.installer*.t?z)
/sbin/installpkg --root $TMP/initrd $(find $SLINTREPO -name installer-translations*.t?z)
# libexpat.so.1 is requested by brltty.installer
/sbin/installpkg --root $TMP/initrd $(find $ISO -name expat*.t?z)
# libcre2.32.so.0 is requested by brltty.installer
/sbin/installpkg --root $TMP/initrd $(find $SLACKREPO -name pcre2*.t?z)
/sbin/installpkg --root $TMP/initrd $(find $SLACKREPO -name strace*.t?z)
/sbin/installpkg --root $TMP/initrd $(find $ISO -name spkg*.t?z)
/sbin/installpkg --root $TMP/initrd $(find $SLACKREPO -name json-c*.t?z)
/sbin/installpkg --root $TMP/initrd $(find $SLACKREPO -name libsndfile*.t?z)
/sbin/installpkg --root $TMP/initrd $(find $SLACKREPO -name orc-0*.t?z)
/sbin/installpkg --root $TMP/initrd $(find $ISO -name alsa-lib*.t?z)
/sbin/installpkg --root $TMP/initrd $(find $ISO -name alsa-utils*.t?z)
# alsamixer needs libformw.so.5 shipped in the ncurses package.
#/sbin/installpkg --root $TMP/initrd $(find $SLACKREPO -name nnn*.t?z)
/sbin/installpkg --root $TMP/initrd $(find $SLACKREPO -name ncurses*.t?z)
/sbin/installpkg --root $TMP/initrd $(find $ISO -name portaudio*.t?z)
/sbin/installpkg --root $TMP/initrd $(find $SLACKREPO -name flac*.t?z)
/sbin/installpkg --root $TMP/initrd $(find $SLACKREPO -name libogg-*.t?z)
/sbin/installpkg --root $TMP/initrd $(find $ISO -name libvorbis*.t?z)
/sbin/installpkg --root $TMP/initrd $(find $ISO -name pcaudiolib*.t?z)
/sbin/installpkg --root $TMP/initrd $(find $ISO -name sonic*.t?z)
/sbin/installpkg --root $TMP/initrd $(find $ISO -name espeak-*.t?z)
/sbin/installpkg --root $TMP/initrd $(find $ISO -name espeakup*.t?z)
/sbin/installpkg --root $TMP/initrd $(find $SLACKREPO -name libyaml*.t?z)
/sbin/installpkg --root $TMP/initrd $(find $ISO -name liblouis*.t?z)

chmod 755 $TMP/initrd/etc/rc.d/rc.espeakup
cp -f source/Speakup.txt $TMP/initrd

cat << 'FDF' > $TMP/initrd/usr/lib/setup/inst
#!/bin/sh
# Display the keyboard shortcuts for reviewing the screen with speakup
less /Speakup.txt
FDF
chmod 755 $TMP/initrd/usr/lib/setup/inst
cat << 'FDF' > $TMP/initrd/etc/profile.d/alias.sh
# Make sure LANG be the one chosen during installation, case occurring
[ -f /tmp/.SlintInstallationLANG ] && LANG=$(</tmp/.SlintInstallationLANG)
FDF
chmod 755 $TMP/initrd/etc/profile.d/alias.sh

# Add the modules
/sbin/installpkg --root $TMP/initrd $KERNELMODULES

cd $TMP/initrd
# In the initrd, grep is in /bin, however zgrep expects to find it in
# /usr/bin. Oh, well...
(cd usr/bin
ln -s ../../bin/grep grep
ls -l
)
read -p "Enter or Ctrl+C" dummy
# Not yet as modules are not decompressed? Let's try again.
echo "--- Compressing kernel modules ---"
find ./lib/modules -type f -name "*.ko" -exec xz -9f -C crc32 {} \;
for i in $(find ./lib/modules -type l -name "*.ko") ; do
   ln -s $( readlink $i).xz $i.xz ; rm $i ;
done
cd - 1>/dev/null
echo "--- Kernel modules have been compressed ---"
echo "--- Running depmod ---"-
echo "/sbin/depmod -b $TMP/initrd $KVER"
/sbin/depmod -b $TMP/initrd $KVER
echo "--- depmod has be run ---"
mkdir -p $TMP/initrd/usr/share/fonts
cp fonts/* $TMP/initrd/usr/share/fonts
# Copying new and modified files in $TMP/initrd/
for i in $(<$CWD/source/scripts); do
  mkdir -p $TMP/initrd/$(dirname $i)
  cp -p $SCRIPTS/$(basename $i) $TMP/initrd/$(dirname $i)
  chmod 0755 $TMP/initrd/$(dirname $i)/$(basename $i)
done
# Remove INSURL as we don't provide installation through http/ftp, that
# would need a remote packages tree like on the ISO including the
# unmodified Slackware packages. Users can still provide such a tree
# copied from an ISO and export it through NFS or Samba.
rm $TMP/initrd/usr/lib/setup/INSURL
# Put in the initrd files (mostly programs and shared libaries) that we need
# in addition to those already included in the genuine Slackware initrd
( cd $TMP

	# /etc/rc.d/rc.espeakup calls speakup-restore shipped in the
	# slint-misc package
	tar xf $ISO/slint/slint-misc*txz
	cp  usr/sbin/speakup-restore initrd/sbin/speakup-restore
	tar xf $SLACKREPO/a/gettext-0*.t?z
	tar xf $SLACKREPO/d/gettext-tools-*.t?z
	# We need these programs to display translations
	cp -a usr/bin/{envsubst,gettext*}  initrd/usr/bin
	# To use spkg, we need xzdec shipped in the xz package
	tar xf $SLACKREPO/a/xz-*.t?z
	cp usr/bin/xzdec initrd/bin
	# In addition to the program dialog, we'll need its translation files for
	# included languages
	tar xf $SLACKREPO/a/dialog-*.t?z
	# we'll need the translation files associated to programs like cfdisk for
	# included languages
	tar xf $SLACKREPO/a/util-linux-*.t?z
	# we will include gconv-modules and all libraries in /usr/lib64/gconv
	# associated to encodings of PO files
	mkdir -p initrd/usr/lib64/gconv glibc-solibs
	tar xf $SLACKREPO/a/glibc-solibs-*.t?z
	for i in gconv-modules ISO8859-1.so ISO8859-15.so ISO8859-2.so KOI8-R.so; do
	cp -a usr/lib64/gconv/$i initrd/usr/lib64/gconv
	done
	tar xf $SLACKREPO/l/glibc-i18n-*.t?z
	# Let's ship nano, a newbie friendly text editor, and its dependency
	# libmagic.
	tar xf $ISO/slint/file-*t?z
	tar xf $ISO/slint/nano-*t?z
	cp usr/lib64/libmagic.so.1.0.0 initrd/usr/lib64
	cp usr/bin/nano initrd/bin
	# Copying /usr/lib64/locale/<locale>/* and the (generated) MO files in: $TMP/initrd
	mkdir -p initrd/usr/lib64/locale
	for Locale in $(<$CWD/source/locales_list_sorted_by_name); do
	ll_TT=$(echo $Locale|cut -c 1-5)
	ll=$(echo $Locale|cut -c 1-2)
	SeTCode
	SeTLocaleDir
	# Let's include the files gettext needs in /usr/lib64/locale to work.
	# I could have included (generated) locale_archive instead to save
	# some space on the initrd, but this would have prevented the user to
	# run this script on a machine with a different ARCH than the target.
	# Just in case I change my mind (PS. I did :-)
	# We'll also include translation files for programs used during installation
	# coming from packages unpacked above
	# We'll compile the locale definitions taken from the host system.
	#localedef --add-to-archive /usr/lib64/locale/${ll_TT} \
	#  --prefix=$TMP/initrd 1>/dev/null
	mkdir -p initrd/usr/share/locale/$LocaleDir/LC_MESSAGES
	cp -r  usr/lib64/locale/$Locale \
		initrd/usr/lib64/locale
	if [ -f  usr/share/locale/$LocaleDir/LC_MESSAGES/gettext-runtime.mo ]; then
		cp usr/share/locale/$LocaleDir/LC_MESSAGES/gettext-runtime.mo \
		initrd/usr/share/locale/$LocaleDir/LC_MESSAGES
	elif [ -f usr/share/locale/$ll/LC_MESSAGES/gettext-runtime.mo ]; then
		cp usr/share/locale/$ll/LC_MESSAGES/gettext-runtime.mo \
		initrd/usr/share/locale/$LocaleDir/LC_MESSAGES
	fi
	if [ -f  usr/share/locale/$LocaleDir/LC_MESSAGES/dialog.mo ]; then
		cp usr/share/locale/$LocaleDir/LC_MESSAGES/dialog.mo \
		initrd/usr/share/locale/$LocaleDir/LC_MESSAGES
	elif [ -f usr/share/locale/$ll/LC_MESSAGES/dialog.mo ]; then
		cp usr/share/locale/$ll/LC_MESSAGES/dialog.mo \
		initrd/usr/share/locale/$LocaleDir/LC_MESSAGES
	fi
	if [ -f  usr/share/locale/$LocaleDir/LC_MESSAGES/util-linux.mo ]; then
		cp usr/share/locale/$LocaleDir/LC_MESSAGES/util-linux.mo \
		initrd/usr/share/locale/$LocaleDir/LC_MESSAGES
	elif [ -f usr/share/locale/$ll/LC_MESSAGES/util-linux.mo ]; then
		cp usr/share/locale/$ll/LC_MESSAGES/util-linux.mo \
		initrd/usr/share/locale/$LocaleDir/LC_MESSAGES
	fi
	if [ -f  usr/share/locale/$LocaleDir/LC_MESSAGES/nano.mo ]; then
		cp usr/share/locale/$LocaleDir/LC_MESSAGES/nano.mo \
		initrd/usr/share/locale/$LocaleDir/LC_MESSAGES
	elif [ -f usr/share/locale/$ll/LC_MESSAGES/nano.mo ]; then
		cp usr/share/locale/$ll/LC_MESSAGES/nano.mo \
		initrd/usr/share/locale/$LocaleDir/LC_MESSAGES
	fi
	done
	# Include the alsa-info script for debugging purposes
	cp $CWD/source/alsa/alsa-info.sh initrd/bin
#	wget --no-check-certificate -nv http://www.alsa-project.org/alsa-info.sh 2>/dev/null
	chmod 755 initrd/bin/alsa-info.sh
	# Tune /etc/rc.d/rc.alsa
	rm -f initrd/etc/rc.d/rc.alsa
	cp $CWD/source/alsa/rc.alsa initrd/etc/rc.d/
	chmod 755 initrd/etc/rc.d/rc.alsa
)
# Fourth step: install MO files, include intl packages, rebuild the initrd
# =======================================
# Install translations in the initrd
#
# We won't use Slackware color scheme, for dialog,  but our own. 
# Additionally, --noshadow will give us more usable lines and rows.
cp  $SOURCE/dialog/dialogrc $TMP/initrd/etc
# Register the target
cp $ISO/slint/DONOTREMOVEME.TXT $TMP/initrd/.target
rm $TMP/initrd/.installer-version
# Build the initrd
( cd $TMP/initrd
  find . | cpio -o -H newc | xz -9fv -C crc32 > $ISO/initrd
)
grub-mkrescue -v --locales='none' --themes='' --compress=xz \
-o $PATHTOISO -iso_mbr_part_type 0x83 -partition_offset 16 -J \
--xorriso=./grub-mkrescue-sed.sh $ISO
printf "%b" "** Completed. ***

If writing the ISO was successful an hybrid DVD/USB image:
$PATHTOISO is ready.\n"
rm -r $TMP

