#!/bin/sh
set -e
ISO=$(mktemp -d)
STORE=$(mktemp -d)
PATCHES=/home/didier/Slint/repo/x86_64/slint-14.2/source/global-building/patches
PATHTOISO=$STORE/fake_slackware64-14.2-2.iso
mount /storage/images_iso/slackware64-14.2-install-dvd.iso /mnt
printf %b "Copying the genuine ISO in a temp directory...\n"
cp -a /mnt/* $ISO
umount /mnt
TMP=$(mktemp -d)
mkdir $TMP/initrd
( cd $TMP/initrd
  gunzip -cd $ISO/isolinux/initrd.img | \
  cpio -i -d -m -H newc --no-absolute-filenames \
)
(cd $TMP/initrd/sbin
	patch -p0 < $PATCHES/probe_nvme.diff  
)
(cd $TMP/initrd/usr/lib/setup
    patch -p0 < $PATCHES/SeTEFI.diff
)
( cd $TMP/initrd
  find . -print | cpio -o --owner root:root -H newc | gzip -9 > $ISO/isolinux/initrd.img
)
printf %b "Rebuilding the new ISO...\n"
( cd $ISO
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 "FAKESLACK" \
    -J \
    -joliet-long \
    -r \
    -v \
    -x source \
    -x slackware64 \
    -x extra \
    -x patches \
    -x testing \
    -o $PATHTOISO \
    -b isolinux/isolinux.bin \
    -c isolinux/boot.cat \
    -no-emul-boot \
    -boot-info-table $EFIOPTIONS . \
)  2>${PATHTOISO%iso}log.txt
printf %b "Hopefully, $PATHTOISO has been built\n"
printf %b "Check the log file ${PATHTOISO%iso}log.txt\n"
printf %b "Don't forget to remove $STORE $ISO and $TMP when you are done\n"
