for isofile in $isopath/pmagic_*.iso; do
  if [ -e "$isofile" ]; then
    regexp --set=isoname "$isopath/(.*)" "$isofile"
    submenu "$isoname ->" "$isofile" {
      iso_path="$2"
      loopback loop "$iso_path"
      menuentry "Default settings (Runs from RAM)" {
        bootoptions="iso_filename=$iso_path edd=on vga=normal vmalloc=384MiB"
        if cpuid -l; then # Check whether CPU is 64-bit
          linux (loop)/pmagic/bzImage64 $bootoptions
        else
          linux (loop)/pmagic/bzImage $bootoptions
        fi
        initrd (loop)/pmagic/initrd.img
      }
      menuentry "Live with default settings" {
        bootoptions="iso_filename=$iso_path edd=on vga=normal boot=live eject=no vmalloc=384MiB"
        if cpuid -l; then # Check whether CPU is 64-bit
          linux (loop)/pmagic/bzImage64 $bootoptions
        else
          linux (loop)/pmagic/bzImage $bootoptions
        fi
        initrd (loop)/pmagic/initrd.img
      }
      menuentry "Alternate graphical server" {
        bootoptions="iso_filename=$iso_path edd=on vga=normal xvesa=yes vmalloc=384MiB"
        if cpuid -l; then # Check whether CPU is 64-bit
          linux (loop)/pmagic/bzImage64 $bootoptions
        else
          linux (loop)/pmagic/bzImage $bootoptions
        fi
        initrd (loop)/pmagic/initrd.img
      }
      menuentry "Failsafe Settings" {
        bootoptions="iso_filename=$iso_path boot=live edd=off load_ramdisk=1 prompt_ramdisk=0 rw vga=normal nopcmcia noscsi nogpm consoleboot nosmart nosshd nosound max_loop=256 no_iscsi vmalloc=384MiB loglevel=9 nolapic"
        if cpuid -l; then # Check whether CPU is 64-bit
          linux (loop)/pmagic/bzImage64 $bootoptions
        else
          linux (loop)/pmagic/bzImage $bootoptions
        fi
        initrd (loop)/pmagic/initrd.img
      }
      menuentry "Plop Boot Manager" {
        bootoptions=""
        linux16 (loop)/boot/plpbt/plpbt.bin $bootoptions
      }
      submenu "Extras Menu ->" "$2" {
        iso_path="$2"
        menuentry "Hardware Detection Tool (HDT)" {
          bootoptions=""
          linux16 (loop)/boot/syslinux/memdisk $bootoptions
          initrd16 (loop)/boot/syslinux/hdt.gz
        }
        menuentry "Memtest86+" {
          bootoptions=""
          linux16 (loop)/boot/syslinux/memtest $bootoptions
        }
        menuentry "Super Grub Disk" {
          bootoptions=""
          linux16 (loop)/boot/syslinux/memdisk $bootoptions
          initrd16 (loop)/boot/sgd/sgd.gz
        }
        menuentry "Super Grub2 Disk" {
          bootoptions=""
          linux16 (loop)/boot/syslinux/memdisk $bootoptions
          initrd16 (loop)/boot/sgd/sgd2.gz
        }
        menuentry "MHDD" {
          bootoptions=""
          linux16 (loop)/boot/syslinux/memdisk $bootoptions
          initrd16 (loop)/boot/mhdd/mhdd.gz
        }
        menuentry "Clonezilla" {
          bootoptions="iso_filename=$iso_path edd=on vga=normal vmalloc=384MiB clonezilla=yes"
          if cpuid -l; then # Check whether CPU is 64-bit
            linux (loop)/pmagic/bzImage64 $bootoptions
          else
            linux (loop)/pmagic/bzImage $bootoptions
          fi
          initrd (loop)/pmagic/initrd.img
        }
        menuentry "Nwipe" {
          bootoptions="iso_filename=$iso_path edd=on vga=normal vmalloc=384MiB nwipe=yes"
          if cpuid -l; then # Check whether CPU is 64-bit
            linux (loop)/pmagic/bzImage64 $bootoptions
          else
            linux (loop)/pmagic/bzImage $bootoptions
          fi
          initrd (loop)/pmagic/initrd.img
        }
        menuentry "Chntpw" {
          bootoptions="vga=1 loglevel=0 initrd=/boot/chntpw/scsi.cgz"
          linux (loop)/boot/chntpw/vmlinuz $bootoptions
          initrd (loop)/boot/chntpw/initrd.cgz
        }
      }
    }
  fi
done
