#!/bin/sh

# pm2pxe.sh -- Convert a PM ISO to PXE format and optionally split the SQFS
#           -- Or split the SQFS from a PM PXE ZIP
# =========================================================================
# Written by Dick Burggraaff (burdi01)

SPLIT_SIZE=30     # split size in MB

# -------------------------------------------------------------------------

# To convert a PM ISO to PXE format mount the ISO and run this script:
# # mkdir /tmp/cdrom
# # mount -oloop pmagic-X.X.iso /tmp/cdrom
# # sh /tmp/cdrom/boot/pxelinux/pm2pxe.sh [split]

# Or if you want to amend this script beforehand copy the ISO:
# # mkdir /tmp/cdrom
# # mount -oloop pmagic-X.X.iso /tmp/cdrom
# # cp -a /tmp/cdrom .
# # umount /tmp/cdrom
# # .....
# # sh cdrom/boot/pxelinux/pm2pxe.sh [split]

# To split the SQFS from a 3-file PXE ZIP unzip the ZIP and run this script:
# # unzip pmagic-pxe-X.X.zip
# # .....
# # sh pmagic-pxe-X.X/boot/pxelinux pm2pxe.sh

# -------------------------------------------------------------------------

# This script MUST be located in .../boot/pxelinux/
PMAGIC="$(dirname "$(dirname "$(dirname "$(readlink -f "$0")")")")/pmagic"

SPLIT=${1:+split}       # [split] ???

HERE="$(pwd)"           # the current directory

# If this a PXE ZIP then convert it to ISO format
FILE="$(basename "$(ls "$PMAGIC"/pmagic-*.sqfs.cgz 2> /dev/null)")"

if [ ".$FILE" != "." ]; then

   mkdir -p "$PMAGIC/pmodules"
   cd "$PMAGIC/pmodules"

   echo -n "cpio ... "
   # Not all gzip implementations handle this gracefully
   #gzip -cd "$PMAGIC/$FILE" | cpio -imu
   gzip -d < "$PMAGIC/$FILE" | cpio -imu

   SPLIT=PXE
   
fi

# This is the ISO
FILE="$(basename "$(ls "$PMAGIC"/pmodules/pmagic-*.sqfs 2> /dev/null)")"

if [ ".$FILE" = "." ]; then
   echo "The '$PMAGIC/pmodules/pmagic-<version>.sqfs'"
   echo "nor the '$PMAGIC/pmagic-<version>.sqfs.cgz' file could be found."
   exit 1
fi

WORKS="$(dirname "$(dirname "$PMAGIC")")"
rm -f "$WORKS/$FILE"* "$WORKS/$FILE#"* "$WORKS/stanza.txt"

INITRD="pmagic/initramfs"

if [ ".$SPLIT" = "." ]; then

   cd "$PMAGIC/pmodules"
   echo -n "cpio ... "
   echo "$FILE" | cpio -o -H newc | gzip -9 > "$WORKS/$FILE.cgz"

   INITRD="$INITRD,pmagic/$FILE.cgz"

else

   # Newer versions of 'split' seem to not accept a suffix length = 2
   #split -a 2 -b ${SPLIT_SIZE}m -d --verbose ${FILE} ${FILE}
   split -b ${SPLIT_SIZE}m -d --verbose "$PMAGIC/pmodules/$FILE" \
      "$WORKS/$FILE#"

   cd "$WORKS"
   for PPART in "$FILE#"* ; do
      # Not all shells support substrings
      #PART=${PPART: -2}    # Note the space between ":" and "-" !!!
      PART="$FILE"${PPART#${PPART%??}}
      mv "$PPART" "$PART"
      echo -n "cpio ... "
      echo "$PART" | cpio -o -H newc | gzip -9 > "$PART.cgz"
      rm -f "$PART"
      INITRD="$INITRD,pmagic/$PART.cgz"
   done

   if [ ".$SPLIT" = ".PXE" ]; then
      rm -f "$PMAGIC/pmodules/$FILE"
      rmdir "$PMAGIC/pmodules" &> /dev/null
   fi

   FILE="$FILE??"

fi

cd "$HERE"

echo "DEFAULT pmagic

LABEL pmagic
LINUX pmagic/bzImage
INITRD $INITRD
APPEND edd=off load_ramdisk=1 prompt_ramdisk=0 rw vga=normal loglevel=9 max_loop=256
" > "$WORKS/stanza.txt"

echo "
Copy the following files to your PXE server:
-- $PMAGIC/bzImage
-- $PMAGIC/initramfs
-- $WORKS/$FILE.cgz
and use
-- $WORKS/stanza.txt
as the basis for your PXE configuration.
"


