#!/bin/sh
# Author: Didier Spaier, Paris, France
#
# Before running this script, adapt MIRROR and PATHTOISO below to your
# installation.
#


CWD=$(pwd)
SLACKVERSION="current"
OLDSLACKVERSION="14.2"
SLINTVERSION="14.2.1.2"
TAG="tsound_slack"
TAGVERSION=${SLINTVERSION}$TAG
KVER=4.19.20
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-testing/slint
SOURCE=/storage/repo/x86_64/slint-testing/source
MIRROR=/data/slackware64-$SLACKVERSION
SCRIPTS=../localization/scripts
#SCRIPTS=/slint64/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
sed "s/+version+/$TAGVERSION/g" docs/Slint-HOWTO > $ISO/Slint-HOWTO
echo $DATESTAMP > $ISO/DateBuilt
# Copy to $ISO/{EFI,isolinux,kernels}
rm -rf $ISO/{EFI,isolinux}
mkdir -p $ISO/isolinux
mkdir -p $ISO/EFI/BOOT
cp source/EFI/BOOT/grub.cfg $ISO/EFI/BOOT/
cp source/EFI/BOOT/osdetect.cfg $ISO/EFI/BOOT/
cp source/EFI/BOOT/tools.cfg $ISO/EFI/BOOT/
cp source/EFI/BOOT/grub-embedded.cfg $ISO/EFI/BOOT/
cp /data/slackware64-current/kernels/huge.s/bzImage $ISO/isolinux/linux
(cd $ISO/EFI/BOOT/
grub-mkimage --format=x86_64-efi --output=$ISO/EFI/BOOT/bootx64.efi \
--config=grub-embedded.cfg --compression=xz --prefix=/EFI/BOOT \
play part_gpt part_msdos fat ext2 hfs hfsplus iso9660 udf ufs1 ufs2 \
zfs chain linux boot appleldr configfile normal regexp minicmd \
reboot halt search search_fs_file search_fs_uuid search_label \
gfxterm gfxmenu efi_gop efi_uga all_video loadbios gzio echo true \
probe loadenv bitmap_scale font cat help ls png jpeg tga test \
at_keyboard usb_keyboard
)

cp source/isolinux/{f1.txt,isolinux.bin,BEEP,vesamenu.c32} $ISO/isolinux
iconv -f UTF-8 -t CP437 -o $ISO/isolinux/isolinux.cfg source/isolinux/isolinux-vesamenu.cfg.64
# 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/initrd.img | \
  cpio -i -d -m -H newc --no-absolute-filenames
)
/sbin/installpkg --root $TMP/initrd $(find $SLINTREPO -name brltty.installer*.t?z)
/sbin/installpkg --root $TMP/initrd $(find $SLINTREPO -name installer-translations*.t?z)
/sbin/installpkg --root $TMP/initrd $(find $ISO -name aaa_elflibs*.t?z) 1>/dev/null
/sbin/installpkg --root $TMP/initrd $(find $ISO -name spkg*.t?z)
/sbin/installpkg --root $TMP/initrd $(find $ISO -name json-c*.t?z)
/sbin/installpkg --root $TMP/initrd $(find $ISO -name libsndfile*.t?z)
/sbin/installpkg --root $TMP/initrd $(find $ISO -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)
/sbin/installpkg --root $TMP/initrd $(find $ISO -name portaudio*.t?z)
/sbin/installpkg --root $TMP/initrd $(find $ISO -name flac*.t?z)
/sbin/installpkg --root $TMP/initrd $(find $ISO -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 $ISO -name libyaml*.t?z)
/sbin/installpkg --root $TMP/initrd $(find $ISO -name liblouis*.t?z)
cp -f source/Speakup.txt $TMP/initrd
cp -f source/alarm.wav $TMP/initrd
# For now, we will really kill espeakup instead of stopping it in the
# hope that it allows it to actually restart during installation
( cd $TMP/initrd/etc/rc.d
sed 's/kill $PID/kill -s 9 $PID/' rc.espeakup > bof
mv bof rc.espeakup
sed -n '/kill $PID/p' rc.espeakup
) 
cat << 'FDF' > $TMP/initrd/usr/lib/setup/inst
#!/bin/sh
# Display the keyboard shortcuts for reviewing the screen with speakup
LANG=en_US.utf8 dialog --msgbox "$(</Speakup.txt)" 0 0
FDF
chmod 755 $TMP/initrd/etc/rc.d/rc.espeakup $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 sound modules

#/sbin/installpkg --root $TMP $KERNELMODULES
#mkdir -p $TMP/initrd/lib/modules/$KVER/kernel
#cp -r $TMP/lib/modules/*/kernel/sound $TMP/initrd/lib/modules/$KVER/kernel
## Add any dependency modules:
#for MODULE in $(find $TMP/lib/modules/$KVER/kernel/sound -type f -exec basename {} .ko \;) ; do
#    /sbin/modprobe --dirname $TMP/initrd --set-version $KVER --show-depends --ignore-install $MODULE 2>/dev/null |grep "^insmod " |cut -f 2 -d ' ' |while read SRCMOD; do
#        if [ "$(basename $SRCMOD .ko)" != "$MODULE" ]; then
#        ( cd $TMP
#          cp -a ${SRCMOD#/} initrd/
#        )
#        fi
#    done
#done
#/sbin/depmod -b $TMP/initrd $KVER
mkdir -p $TMP/initrd/usr/share/fonts
cp fonts/* $TMP/initrd/usr/share/fonts
Copying new and modified files in $ISO/isolinux/

# We'll include a font (encoded in CP437), as if no font is set, text files are
# displayed using native font (confirmed by Gene Cumm on syslinux mailing list),
# but native (or hardware) code page can differ upon country, according to:
# http://www.drdos.net/documentation/usergeng/17ugch17.htm
cp -p source/isolinux/ter-i16b.psf $ISO/isolinux
# 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
tar xf $ISO/slint/gettext-0*.t?z
tar xf $ISO/slint/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 $ISO/slint/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 $ISO/slint/dialog-*.t?z
# we'll need the translation files associated to programs like cfdisk for
# included languages
tar xf $ISO/slint/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 $ISO/slint/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 $ISO/slint/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
)

# 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
chown -R root:root $ISO/isolinux
# Register the target
cp $ISO/slint/DONOTREMOVEME.TXT $TMP/initrd/.target
rm $TMP/initrd/.installer-version
# Build the initrd
( cd $TMP/initrd
  # find . -print | cpio -o --owner root:root -H newc | gzip -9 > $ISO/isolinux/initrd.img
  find . | cpio -o -H newc | xz -9fv -C crc32 > $ISO/isolinux/initrd.img
)
mv $ISO/isolinux/initrd.img $ISO/isolinux/initrd
# Fifth step: build the ISO file
# ================

( cd $ISO
#  size=$(BLOCKSIZE=1M du -s EFI|sed "s/\([[:digit:]][[:digit:]]\).*/\1/")
#  rsize=$(( $size + 1 ))
  dd if=/dev/zero of=efiboot.img bs=1M count=2
  # Format the image as FAT16:
  mkfs.fat efiboot.img
  # Create a temporary mount point:
  MOUNTPOINT=$(mktemp -d)
  # Mount the image there:
  mount -o loop efiboot.img $MOUNTPOINT
  # Copy the the whole EFI directory to the mountpoint as elilo needs to
  # find everything it deals with there.
  # On the other hand, no need to include EFI in the ISO.
  cp -a EFI $MOUNTPOINT
  # Unmount and clean up:
  umount $MOUNTPOINT
  rmdir $MOUNTPOINT
  # Move the efiboot.img to ../../isolinux:
  mv efiboot.img $ISO/isolinux
# Now $ISO contains all files to be put on the ISO image, so we can
# complete $ISO/CONTENT.TXT with the file tree
sed -i "s/<version>/64-${SLINTVERSION}$SUFFIX/g" CONTENT.TXT
tree >> CONTENT.TXT
EFIOPTIONS="-eltorito-alt-boot -e isolinux/efiboot.img -isohybrid-gpt-basdat -no-emul-boot"
xorriso -as mkisofs \
    -isohybrid-mbr /usr/share/syslinux/isohdpfx.bin \
    -hide-rr-moved \
    -U \
    -V "ISO64_CURRENT" \
    -J \
    -joliet-long \
    -r \
    -v \
    -x kernels \
    -o $PATHTOISO \
    -b isolinux/isolinux.bin \
    -c isolinux/boot.cat \
    -no-emul-boot \
    -boot-info-table $EFIOPTIONS . \
)  2>${PATHTOISO%iso}log.txt
chown -R `stat -c %U:%G $CWD` $PATHTOISO ${PATHTOISO%iso}log.txt $ISO
printf "%b" "** Completed. ***

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

