#!/bin/sh
# Author: Didier Spaier, Paris, France
#
# Before running this script, adapt MIRROR and PATHTOISO below to your
# installation.
#
# This script build a Slint ISO
#
CWD=$(pwd)
SLACKVERSION="current"
SLINTVERSION="14.2"
usage() {
  printf %b "Usage: $0 32|64 0|1\n       0|1: without|with background\n"
  exit
}
if [ $# -ne 1 ]; then usage; fi
if [ $1 -ne 32 ] && [ $1 -ne 64 ]; then usage; fi
SFX=$(echo $1|grep 64)
PATHTOISO=/tmp/slackware$SFX-current-mini.iso
SLINTREPO=../..
MIRROR=/storage/slackware${SFX}-$SLACKVERSION
SCRIPTS=../localization/slackware_scripts
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=/tmp/iso
rm -rf $ISO/{EFI,isolinux,kernels}
cp -r $MIRROR/isolinux $MIRROR/EFI/BOOT $MIRROR/kernels $ISO
printf "%b" "\nPreparation in progress..."
mkdir $TMP/initrd
( cd $TMP/initrd
  gunzip -cd $ISO/isolinux/initrd.img | \
  cpio -i -d -m -H newc --no-absolute-filenames \
)
for i in $(<$SCRIPTS); do
  mkdir -p $TMP/initrd/$(dirname $i)
echo "cp -p $SCRIPTS/$(basename $i) $TMP/initrd/$(dirname $i)"
  cp -p $SCRIPTS/$(basename $i) $TMP/initrd/$(dirname $i)
  chmod 0755 $TMP/initrd/$(dirname $i)/$(basename $i)
done
# Let's ship nano, a newbie friendly text editor, and its dependency
# libmagic.
mkdir -p $TMP/initrd/usr/$lib$SFX
tar xf $MIRROR/slackware$SFX/a/file-*t?z
tar xf $MIRROR/slackware$SFX/ap/nano-*t?z
cp usr/lib$SFX/libmagic.so.1.0.0 $TMP/initrd/usr/lib$SFX
cp usr/bin/nano $TMP/initrd/bin
# Build the initrd
( cd $TMP/initrd
  find . -print | cpio -o --owner root:root -H newc | gzip -9 > $ISO/isolinux/initrd.img
)
( cd $ISO
rm -f *
if [ "$SFX" = "64" ]; then
  EFIOPTIONS="-eltorito-alt-boot -e isolinux/efiboot.img -isohybrid-gpt-basdat -no-emul-boot"
fi
xorriso -as mkisofs \
    -isohybrid-mbr /usr/share/syslinux/isohdpfx.bin \
    -hide-rr-moved \
    -U \
    -V "SLACKDVD" \
    -J \
    -joliet-long \
    -r \
    -v \
    -x slint \
    -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"

rm -r $TMP

