def generate_script(group, entry_point, header, template):
    """Generate the script based on the template.

    :param str group:
        The entry-point group name, e.g., "console_scripts".
    :param str header:
        The first line of the script, e.g., "!#/usr/bin/env python".
    :param str template:
        The script template.
    :returns:
        The templated script content
    :rtype:
        str
    """
    if not entry_point.attrs or len(entry_point.attrs) > 2:
        raise ValueError("Script targets must be of the form "
                         "'func' or 'Class.class_method'.")
    script_text = template % dict(
        group=group,
        module_name=entry_point.module_name,
        import_target=entry_point.attrs[0],
        invoke_target='.'.join(entry_point.attrs),
    )
    return header + script_text