#!/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"
NICE_FILES="CHANGES_AND_HINTS.TXT CHECKSUMS.md5 README* CRYPTO_NOTICE.TXT ChangeLog.txt FAQ.TXT FILELIST.TXT PACKAGES.TXT READ_DVD.TXT RELEASE_NOTES SPEAK* Slackware-HOWTO UPGRADE.TXT"

# 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
X86_V=$(echo $ISO_X86 | cut -d "-" -f 2)
echo -n "Insert image file for slackware 64 bit: "
read ISO_X64
X64_V=$(echo $ISO_X64 | cut -d "-" -f 2)

# Checking versions
if [ "$X86_V" != "$X64_V" ]; then
	echo "** WARNING **"
	echo "Different versions of Slackware detected:"
	echo " - 32 bit: $X86_V"
	echo " - 64 bit: $X64_V"
	echo
	echo -n "Would you like to continue anyway? "
	read ans
	case "$ans" in
		[YySs]|[Yy][Ee][Ss])
			;;
		*)
			echo
			exit 1
			;;
	esac
fi

# Mounting iso images.
echo
echo -n "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!"

# Copying the script itself into dvd.
cp $0 $TMP/create_SlackDVD-mix.sh || exit 1
cd $TMP

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

# Copying needed files from slackware 32 bit.
# It won't copy pasture/, patches/, source/,
# testing/, and other few files.
echo
echo -n "Copying files from slackware 32 bit iso..."
mkdir 32_bit
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.* .
cp $X86/BOOTING.TXT .
cp $X86/COPY* .
cp $X86/GPG-KEY .
for file in $NICE_FILES; do
	if ls $X86/$file &> /dev/null ; then
		cp $X86/$file 32_bit
	fi
done
cp -R $X86/usb-and-pxe-installers 32_bit
cp -R $X86/slackbook slackbook
cp -R $X86/extra extra32
echo "Done!"

# Removing source/ and aspell-word-lists/ from extra.
rm -Rf extra32/source
rm -Rf extra32/aspell-word-lists/*
cp $X86/extra/aspell-word-lists/aspell-it*.{txt,txz,asc} extra32/aspell-word-lists

# Linking main directory with files for installation.
mkdir slackware
ln -s $X86/slackware/* slackware
rm slackware/PACKAGES.TXT
ln -s 32_bit/PACKAGES.TXT slackware

# Copying needed files from slackware 64 bit.
# It won't copy pasture/, patches/, source/,
# testing/, and other few files.
echo -n "Copying files from slackware 64 bit iso..."
mkdir 64_bit
cp $X64/isolinux/initrd.img isolinux/initrd64.img
cp $X64/kernels/huge.s/* kernels/slack64/
for file in $NICE_FILES; do
	if ls $X64/$file &> /dev/null ; then
		cp $X64/$file 64_bit
	fi
done
cp -R $X64/usb-and-pxe-installers 64_bit
cp -R $X64/extra extra64
echo "Done!"

# Removing source/ and aspell-word-lists/ from extra.
rm -Rf extra64/source
rm -Rf extra64/aspell-word-lists/*
cp $X64/extra/aspell-word-lists/aspell-it*.{txt,txz,asc} extra64/aspell-word-lists

# Linking main directory with files for installation.
mkdir slackware64
ln -s $X64/slackware64/* slackware64
rm slackware64/PACKAGES.TXT
ln -s 64_bit/PACKAGES.TXT slackware64

# Writing file for boot messages.
echo
echo -n "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 -n "Writing configuration file for isolinux bootloader..."
cat > isolinux/isolinux.cfg << EOF
default slack32
prompt 1
timeout 1200
display message.txt
F1 message.txt
label slack32
  kernel /kernels/slack32/bzImage
  append initrd=initrd32.img load_ramdisk=1 prompt_ramdisk=0 rw printk.time=0 SLACK_KERNEL=slack32
label slack64
  kernel /kernels/slack64/bzImage
  append initrd=initrd64.img load_ramdisk=1 prompt_ramdisk=0 rw printk.time=0 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-$X86_V-install-dvd_mix.iso \
	-R -J -A "Slackware-$X86_V" \
	-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 "SlackDVD-mix" .
echo
echo
echo "Congratulation!"
echo "Now you have a powerfull dvd image (with both Slackware and Slackware64) ready to burn."
echo "Enjoy :-)"
echo

# Umounting iso images.
umount $X86
umount $X64

# Cleaning directories.
rm -Rf $TMP /tmp/mount_iso
