#!/bin/bash

# Script that creates a single face/mono layer dvd with both slackware 32 and 64 bit.
# To run that script you need root privileges and the iso images of slackware 32 bit
# and slackware 64 bit.
# Heavily based on ZeroUno (from www.slacky.eu) script idea. Thanks mate :-)

# Checking if you are really root.
if [ "$(whoami)" != "root" ]; then
	echo "** ERROR **"
	echo "You need to be root to launch this script!"
	echo
	exit 1
fi

# Isolating variable space.
set -e

# Some variables that we need to proceed.
CWD=$(pwd)
TMP="/tmp/slack_dvd_mix"
X86="/tmp/mount_iso/slack32"
X64="/tmp/mount_iso/slack64"

# Clean directories.
rm -Rf $TMP /tmp/mount_iso
mkdir -p $TMP
mkdir -p $X86
mkdir -p $X64

# Asking for iso images.
echo -n "Insert image file for slackware 32 bit: "
read ISO_X86
echo -n "Insert image file for slackware 64 bit: "
read ISO_X64

# Mounting iso images.
echo
echo "Mounting iso images..."
if ! mount -o loop $ISO_X86 $X86 ; then
	echo "** ERROR **"
	echo "Error mounting $ISO_X86"
	echo
	exit 1
fi
if ! mount -o loop $ISO_X64 $X64 ; then
	echo "** ERROR **"
	echo "Error mounting $ISO_X64"
	echo
	exit 1
fi
echo "Done!"

# Creating some important directories.
cd $TMP
mkdir -p isolinux kernels/slack32 kernels/slack64

# Copying needed files from slackware 32 bit.
# It won't copy pasture/, patches/, source/, testing/,
# usb-and-pxe-installers/, and other few files.
echo
echo "Copying files from slackware 32 bit iso..."
cp $X86/isolinux/initrd.img isolinux/initrd32.img
cp $X86/kernels/hugesmp.s/* kernels/slack32/
cp $X86/isolinux/iso.sort isolinux/
cp $X86/isolinux/isolinux.bin isolinux/
cp $X86/isolinux/isolinux.boot isolinux/
cp $X86/{ANNOUNCE.*,BOOTING:TXT,CHANGES_AND_HINTS.TXT,\
	COPYING*,COPYRIGHT.TXT,FAQ.TXT,README*,\
	Slackware-HOWTO,UPGRADE.TXT} .
cp -R $X86/slackbook slackbook
cp -R $X86/extra extra32
echo "Done!"

# Removing source/, from extra.
rm -Rf extra32/source

# Linking main directory with files for installation.
ln -s $X86/slackware slackware

# Copying needed files from slackware 64 bit.
# It won't copy pasture/, patches/, source/, testing/,
# usb-and-pxe-installers/, and other few files.
echo "Copying files from slackware 64 bit iso..."
cp $X64/isolinux/initrd.img isolinux/initrd64.img
cp $X64/kernels/huge.s/* kernels/slack64/
cp -R $X64/extra extra64
echo "Done!"

# Removing source/ from extra.
rm -Rf extra64/source

# Linking main directory with files for installation.
ln -s $X64/slackware64 slackware64

# Writing file for boot messages.
echo
echo "Writing file for boot messages..."
cat $X86/isolinux/message.txt | grep -m1 Welcome > isolinux/message.txt

cat >> isolinux/message.txt << EOF

This DVD contain both Slackware 32bit and Slackware 64bit in a single face :-)

You can start Slackware 32bit Installer typing 'slack32' or simply press ENTER
You can start Slackware 64bit Installer typing 'slack64'

If you need to pass extra parameters to the kernel, enter them at the prompt
below after the name of the kernel to boot (slack32 or slack64).

In a pinch, you can boot your system from here with a command like:

boot: slack32 root=/dev/sda1 rdinit=ro

In the example above, /dev/sda1 is the / Linux partition.


EOF
echo "Done!"

# Writing configuration file for isolinux bootloader.
echo "Writing configuration file for isolinux bootloader..."
cat > isolinux/isolinux.cfg << EOF
default slack32
prompt 1
timeout 1200
display message.txt
label slack32
  kernel /kernels/slack32/bzImage
  append initrd=initrd32.img load_ramdisk=1 prompt_ramdisk=0 rw SLACK_KERNEL=slack32
label slack64
  kernel /kernels/slack64/bzImage
  append initrd=initrd64.img load_ramdisk=1 prompt_ramdisk=0 rw SLACK_KERNEL=slack64
EOF
echo "Done!"

# Creating iso image :-)
echo
echo "Start writing iso image in 5 seconds..."
sleep 5
mkisofs -f -o $CWD/slackware-install-dvd_mix.iso \
	-R -J -A "Slackware-32_64" \
	-hide-rr-moved \
	-v -d -N \
	-no-emul-boot -boot-load-size 4 -boot-info-table \
	-sort isolinux/iso.sort \
	-b isolinux/isolinux.bin \
	-c isolinux/isolinux.boot \
	-V "Slack-DVD-mix" .
echo "Congratulation!"
echo "Now you have a powerfull dvd image (with both Slackware and Slackware64) ready to burn."
echo "Enjoy :-)"
echo

#EOF
