<%page args="kernels, runtime_img, runtime_base, basearch, inroot, outroot, product, isolabel"/>
<%
configdir="tmp/config_files/x86"
SYSLINUXDIR="usr/share/syslinux"
PXEBOOTDIR="images/pxeboot"
STAGE2IMG="images/install.img"
BOOTDIR="isolinux"
KERNELDIR=PXEBOOTDIR
LORAXDIR="usr/share/lorax/"

## Don't allow spaces or escape characters in the iso label
def valid_label(ch):
    return ch.isalnum() or ch == '_'

isolabel = ''.join(ch if valid_label(ch) else '-' for ch in isolabel)

from os.path import basename
%>

## Test ${runtime_img} to see if udf is needed
<%
    import os
    from pylorax.sysutils import joinpaths
    if os.stat(joinpaths(inroot, runtime_img)).st_size >= 4*1024**3:
        udfargs = "-allow-limited-size"
    else:
        udfargs = ""
%>

mkdir images
install ${runtime_img} ${STAGE2IMG}
treeinfo stage2 mainimage images/${runtime_base}

## install bootloader and config files
mkdir ${BOOTDIR}
install ${SYSLINUXDIR}/isolinux.bin ${BOOTDIR}
install ${SYSLINUXDIR}/vesamenu.c32 ${BOOTDIR}
install ${SYSLINUXDIR}/ldlinux.c32 ${BOOTDIR}
install ${SYSLINUXDIR}/libcom32.c32 ${BOOTDIR}
install ${SYSLINUXDIR}/libutil.c32 ${BOOTDIR}
install ${configdir}/isolinux.cfg ${BOOTDIR}
install ${configdir}/boot.msg ${BOOTDIR}
install ${configdir}/grub.conf ${BOOTDIR}
install usr/share/anaconda/boot/syslinux-splash.png ${BOOTDIR}/splash.png
install boot/memtest* ${BOOTDIR}/memtest

## configure bootloader
replace @VERSION@ ${product.version} ${BOOTDIR}/grub.conf ${BOOTDIR}/isolinux.cfg ${BOOTDIR}/*.msg
replace @PRODUCT@ '${product.name}'  ${BOOTDIR}/grub.conf ${BOOTDIR}/isolinux.cfg ${BOOTDIR}/*.msg
replace @ROOT@ 'inst.stage2=hd:LABEL=${isolabel|udev}' ${BOOTDIR}/isolinux.cfg

## install kernels
mkdir ${KERNELDIR}
%for kernel in kernels:
    %if kernel.flavor:
        ## i386 PAE
        installkernel images-xen ${kernel.path} ${KERNELDIR}/vmlinuz-${kernel.flavor}
        installinitrd images-xen ${kernel.initrd.path} ${KERNELDIR}/initrd-${kernel.flavor}.img
    %else:
        ## normal i386, x86_64
        installkernel images-${basearch} ${kernel.path} ${KERNELDIR}/vmlinuz
        installinitrd images-${basearch} ${kernel.initrd.path} ${KERNELDIR}/initrd.img
    %endif
%endfor

hardlink ${KERNELDIR}/vmlinuz ${BOOTDIR}
hardlink ${KERNELDIR}/initrd.img ${BOOTDIR}
%if basearch == 'x86_64':
    treeinfo images-xen kernel ${KERNELDIR}/vmlinuz
    treeinfo images-xen initrd ${KERNELDIR}/initrd.img
%endif

## WHeeeeeeee, EFI.
<% efiargs=""; efigraft=""; efihybrid=""; efiarch32=None; efiarch64=None %>
%if exists("boot/efi/EFI/*/gcdia32.efi"):
    <% efiarch32 = 'IA32' %>
%endif
%if exists("boot/efi/EFI/*/gcdx64.efi"):
    <% efiarch64 = 'X64' %>
%endif
%if (efiarch32 or efiarch64) and basearch != 'i386':
    <%
    efigraft="EFI/BOOT={0}/EFI/BOOT".format(outroot)
    images = ["images/efiboot.img"]
    if domacboot:
        images.append("images/macboot.img")
    %>
    %for img in images:
        <%
        efiargs += " -eltorito-alt-boot -e {0} -no-emul-boot".format(img)
        efigraft += " {0}={1}/{0}".format(img,outroot)
        %>
        treeinfo images-${basearch} ${img|basename} ${img}
    %endfor
    <% efihybrid = "--uefi --mac" if domacboot else "--uefi" %>
    <%include file="efi.tmpl" args="configdir=configdir, KERNELDIR=KERNELDIR, efiarch32=efiarch32, efiarch64=efiarch64, isolabel=isolabel"/>
%endif

# Create optional product.img and updates.img
<% filegraft=""; images=["product", "updates"]; compressargs=""; %>
%if basearch == 'i386':
    # Limit the amount of memory xz uses on i386
    <% compressargs="--xz -9 --memlimit-compress=3700MiB" %>
%endif
%for img in images:
    %if exists("%s/%s/" % (LORAXDIR, img)):
        installimg ${compressargs} ${LORAXDIR}/${img}/ images/${img}.img
        treeinfo images-${basearch} ${img}.img images/${img}.img
        <% filegraft += " images/{0}.img={1}/images/{0}.img".format(img, outroot) %>
    %endif
%endfor

# Inherit iso-graft/ if it exists from external templates
<%
    import os
    if os.path.exists(workdir + "/iso-graft"):
        filegraft += " " + workdir + "/iso-graft"
%>

# Add the license files
%for f in glob("/usr/share/licenses/*-release/*"):
    install ${f} ${f|basename}
    <% filegraft += " {0}={1}/{0}".format(basename(f), outroot) %>
%endfor

## make boot.iso
runcmd mkisofs -o ${outroot}/images/boot.iso \
       -b ${BOOTDIR}/isolinux.bin -c ${BOOTDIR}/boot.cat \
       -boot-load-size 4 -boot-info-table -no-emul-boot \
       ${efiargs} -R -J -V '${isolabel}' -T ${udfargs} \
       -graft-points \
       ${STAGE2IMG}=${outroot}/${STAGE2IMG} \
       ${BOOTDIR}=${outroot}/${BOOTDIR} \
       ${KERNELDIR}=${outroot}/${KERNELDIR} \
       ${efigraft} ${filegraft}
runcmd isohybrid ${efihybrid} ${outroot}/images/boot.iso
treeinfo images-${basearch} boot.iso images/boot.iso
