'Write a configuration file for the boot loader(s).
More system-specific configuration may come in later, if so
that would appear inside the system object in api.py
Can be used for different formats, "pxe" (default) and "grub".'

def write_pxe_file(self, filename, system, profile, distro, arch, image=None, include_header=True, metadata=None, format='pxe'):

    if (arch is None):
        raise 'missing    arch'
    if (image and (not os.path.exists(image.file))):
        return None
    if (metadata is None):
        metadata = {}
    (rval, settings) = utils.input_string_or_dict(self.settings.to_dict())
    if rval:
        for key in settings.keys():
            metadata[key] = settings[key]
    template = None
    buffer = ''
    autoinstall_path = None
    kernel_path = None
    initrd_path = None
    img_path = None
    if (image is None):
        img_path = os.path.join('/images', distro.name)
        if ('nexenta' == distro.breed):
            kernel_path = os.path.join('/images', distro.name, 'platform', 'i86pc', 'kernel', 'amd64', os.path.basename(distro.kernel))
            initrd_path = os.path.join('/images', distro.name, 'platform', 'i86pc', 'amd64', os.path.basename(distro.initrd))
        elif (('http' in distro.kernel) and ('http' in distro.initrd)):
            kernel_path = distro.kernel
            initrd_path = distro.initrd
        else:
            kernel_path = os.path.join('/images', distro.name, os.path.basename(distro.kernel))
            initrd_path = os.path.join('/images', distro.name, os.path.basename(distro.initrd))
        if system:
            blended = utils.blender(self.api, True, system)
        else:
            blended = utils.blender(self.api, True, profile)
        autoinstall_path = blended.get('autoinstall', '')
        metadata.update(blended)
    elif (image.image_type == 'direct'):
        kernel_path = os.path.join('/images2', image.name)
    elif (image.image_type == 'memdisk'):
        kernel_path = '/memdisk'
        initrd_path = os.path.join('/images2', image.name)
    else:
        kernel_path = None
        initrd_path = None
    if ((img_path is not None) and ('img_path' not in metadata)):
        metadata['img_path'] = img_path
    if ((kernel_path is not None) and ('kernel_path' not in metadata)):
        metadata['kernel_path'] = kernel_path
    if ((initrd_path is not None) and ('initrd_path' not in metadata)):
        metadata['initrd_path'] = initrd_path
    if system:
        if (format == 'grub'):
            if system.netboot_enabled:
                template = os.path.join(self.settings.boot_loader_conf_template_dir, 'grubsystem.template')
            else:
                local = os.path.join(self.settings.boot_loader_conf_template_dir, 'grublocal.template')
                if os.path.exists(local):
                    template = local
        elif system.netboot_enabled:
            template = os.path.join(self.settings.boot_loader_conf_template_dir, 'pxesystem.template')
            if arch.startswith('ppc'):
                blended_system = utils.blender(self.api, False, system)
                if (blended_system['boot_loader'] == 'pxelinux'):
                    template = os.path.join(self.settings.boot_loader_conf_template_dir, 'pxesystem_ppc.template')
                elif ((distro.boot_loader == 'grub2') or (blended_system['boot_loader'] == 'grub2')):
                    template = os.path.join(self.settings.boot_loader_conf_template_dir, 'grub2_ppc.template')
                else:
                    template = os.path.join(self.settings.boot_loader_conf_template_dir, 'yaboot_ppc.template')
            elif arch.startswith('arm'):
                template = os.path.join(self.settings.boot_loader_conf_template_dir, 'pxesystem_arm.template')
            elif (distro and distro.os_version.startswith('esxi')):
                template = os.path.join(self.settings.boot_loader_conf_template_dir, 'pxesystem_esxi.template')
        elif ((arch is not None) and arch.startswith('ppc')):
            for (name, interface) in system.interfaces.iteritems():
                filename = ('%s' % utils.get_config_filename(system, interface=name).lower())
                f3 = os.path.join(self.bootloc, 'ppc', filename)
                if os.path.lexists(f3):
                    utils.rmfile(f3)
                f3 = os.path.join(self.bootloc, 'boot/grub', ('grub.cfg-' + filename))
                if os.path.lexists(f3):
                    utils.rmfile(f3)
                f3 = os.path.join(self.bootloc, 'etc', filename)
                if os.path.lexists(f3):
                    utils.rmfile(f3)
            return None
        else:
            template = os.path.join(self.settings.boot_loader_conf_template_dir, 'pxelocal.template')
    elif arch.startswith('arm'):
        template = os.path.join(self.settings.boot_loader_conf_template_dir, 'pxeprofile_arm.template')
    elif (format == 'grub'):
        template = os.path.join(self.settings.boot_loader_conf_template_dir, 'grubprofile.template')
    elif (distro and distro.os_version.startswith('esxi')):
        template = os.path.join(self.settings.boot_loader_conf_template_dir, 'pxeprofile_esxi.template')
    elif ('nexenta' == format):
        template = os.path.join(self.settings.boot_loader_conf_template_dir, 'nexenta_profile.template')
    else:
        template = os.path.join(self.settings.boot_loader_conf_template_dir, 'pxeprofile.template')
    if (kernel_path is not None):
        metadata['kernel_path'] = kernel_path
    if (initrd_path is not None):
        metadata['initrd_path'] = initrd_path
    kernel_options = self.build_kernel_options(system, profile, distro, image, arch, autoinstall_path)
    metadata['kernel_options'] = kernel_options
    if (distro and distro.os_version.startswith('esxi') and (filename is not None)):
        append_line = ('BOOTIF=%s' % os.path.basename(filename))
    elif (('initrd_path' in metadata) and ((not arch) or (arch not in ['ppc', 'ppc64', 'arm']))):
        append_line = ('append    initrd=%s' % metadata['initrd_path'])
    else:
        append_line = 'append    '
    append_line = ('%s%s' % (append_line, kernel_options))
    if arch.startswith('ppc'):
        append_line = append_line[7:]
    if (distro and distro.os_version.startswith('xenserver620')):
        append_line = ('%s' % kernel_options)
    metadata['append_line'] = append_line
    metadata['menu_label'] = ''
    if profile:
        if (arch not in ['ppc', 'ppc64']):
            metadata['menu_label'] = ('MENU    LABEL    %s' % profile.name)
            metadata['profile_name'] = profile.name
    elif image:
        metadata['menu_label'] = ('MENU    LABEL    %s' % image.name)
        metadata['profile_name'] = image.name
    if system:
        metadata['system_name'] = system.name
    if (kernel_path is not None):
        template_fh = open(template)
        template_data = template_fh.read()
        template_fh.close()
    else:
        template_data = '\n'
    buffer = self.templar.render(template_data, metadata, None)
    if (filename is not None):
        self.logger.info(('generating:    %s' % filename))
        fd = open(filename, 'w')
        fd.write(buffer)
        fd.close()
    return buffer
