#!/usr/bin/python3
#
# livecd-creator : Creates Live CD based for Fedora.
#
# Copyright 2007, Red Hat  Inc.
# Copyright 2016, Kevin Kofler
# Copyright 2016, Neal Gompa
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

from __future__ import print_function
import os
import os.path
import sys
import time
import optparse
import logging
from dnf.exceptions import Error as DnfBaseError

import imgcreate
from imgcreate.errors import KickstartError

class Usage(Exception):
    def __init__(self, msg = None, no_error = False):
        Exception.__init__(self, msg, no_error)

def parse_options(args):
    parser = optparse.OptionParser()

    imgopt = optparse.OptionGroup(parser, "Image options",
                                  "These options define the created image.")
    imgopt.add_option("-c", "--config", type="string", dest="kscfg",
                      help="Path or url to kickstart config file")
    imgopt.add_option("-b", "--base-on", type="string", dest="base_on",
                      help="Add packages to an existing live CD iso9660 image.")
    imgopt.add_option("-f", "--fslabel", type="string", dest="fslabel",
                      help="File system label (default based on config name)")
    imgopt.add_option("", "--title", type="string", dest="title",
                      help="Title used by syslinux.cfg file"),
    imgopt.add_option("", "--product", type="string", dest="product",
                      help="Product name used in syslinux.cfg boot stanzas and countdown"),
    imgopt.add_option("", "--build_id", type="string", dest="build_id",
                      help="Build ID", default=None),

    imgopt.add_option("", "--isotype", type="string", dest="isotype",
                      help="ISO Type. <table><number of partitions>. Available:"
                      "gpt2, mbr2, mbr1, efionly", default="mbr2"),

    # Provided for img-create compatibility
    imgopt.add_option("-n", "--name", type="string", dest="fslabel",
                      help=optparse.SUPPRESS_HELP)
    imgopt.add_option("-p", "--plugins", action="store_true", dest="plugins",
                      help="Use DNF plugins during image creation",
                      default=False)
    imgopt.add_option("", "--image-type", type="string", dest="image_type",
                      help=optparse.SUPPRESS_HELP)
    imgopt.add_option("", "--flat-squashfs", action="store_true",
                      dest="flat_squashfs",
                      help="Specify squashing the root filesystem directly. "
                           "This eliminates the intermediate LiveOS directory "
                           "and is suitable only for OverlayFS overlays.",
                      default=False)
    imgopt.add_option("", "--compression-type", type="string", dest="compress_args",
                      help="Compression type recognized by mksquashfs. "
                           "(The default, xz, needs a 2.6.38+ kernel; gzip works "
                           "with all kernels; lzo needs a 2.6.36+ kernel; lz4 "
                           "needs a 3.19+ kernel; lzma needs custom kernel.) "
                           'xz1m is interpreted as "xz -b 1M -Xdict-size 1M -norecovery". '
                           "Set to 'None' to cause the program to read the compression "
                           "type from the base_on image. "
                           "Multiple arguments should be specified in "
                           'one string, i.e., --compression-type "type arg1 arg2 ...".',
                      default="xz")
    imgopt.add_option("", "--releasever", type="string", dest="releasever",
                      default=None,
                      help="Value to substitute for $releasever in kickstart repo urls")
    parser.add_option_group(imgopt)

    # options related to the config of your system
    sysopt = optparse.OptionGroup(parser, "System directory options",
                                  "These options define directories used on your system for creating the live image")
    sysopt.add_option("-t", "--tmpdir", type="string",
                      dest="tmpdir", default="/var/tmp",
                      help="Temporary directory to use (default: /var/tmp)")
    sysopt.add_option("", "--cache", type="string",
                      dest="cachedir", default=None,
                      help="Cache directory to use (default: private cache")
    sysopt.add_option("", "--cacheonly", action="store_true",
                      dest="cacheonly", default=False,
                      help="Work offline from cache, use together with --cache (default: False)")
    sysopt.add_option("", "--nocleanup", action="store_true",
                      dest="nocleanup", default=False,
                      help="Skip cleanup of temporary files")

    parser.add_option_group(sysopt)

    imgcreate.setup_logging(parser)

    # debug options not recommended for "production" images
    # Start a shell in the chroot for post-configuration.
    parser.add_option("-l", "--shell", action="store_true", dest="give_shell",
                      help=optparse.SUPPRESS_HELP)
    # Don't compress the image.
    parser.add_option("-s", "--skip-compression", action="store_true", dest="skip_compression",
                      help=optparse.SUPPRESS_HELP)
    parser.add_option("", "--skip-minimize", action="store_true", dest="skip_minimize",
                      help=optparse.SUPPRESS_HELP)

    (options, args) = parser.parse_args()

    # Pretend to be a image-creator if called with that name
    if not options.image_type:
        if sys.argv[0].endswith('image-creator'):
            options.image_type = 'image'
        else:
            options.image_type = 'livecd'
    if options.image_type not in ('livecd', 'image'):
        raise Usage("'%s' is not a recognized image type" % options.image_type)

    # image-create compatibility: Last argument is kickstart file
    if len(args) == 1:
        options.kscfg = args.pop()
    if len(args):
        raise Usage("Extra arguments given")

    if not options.kscfg:
        raise Usage("Kickstart file must be provided")
    if not os.path.exists(options.kscfg):
        raise Usage("Kickstart file '%s' does not exist" % (options.kscfg))
    if options.base_on and not os.path.isfile(options.base_on):
        raise Usage("Image file '%s' does not exist" %(options.base_on,))
    if options.image_type == 'livecd':
        if options.fslabel and len(options.fslabel) > imgcreate.FSLABEL_MAXLEN:
            raise Usage("CD labels are limited to 32 characters")
        if options.fslabel and options.fslabel.find(" ") != -1:
            raise Usage("CD labels cannot contain spaces.")

    return options

def main():
    try:
        options = parse_options(sys.argv[1:])
    except Usage as livecd_creator_optparse_except:
        (msg, no_error) = livecd_creator_optparse_except.args
        if no_error:
            out = sys.stdout
            ret = 0
        else:
            out = sys.stderr
            ret = 2
        if msg:
            print(msg, file=out)
        return ret

    if os.geteuid () != 0:
        print("You must run %s as root" % sys.argv[0], file=sys.stderr)
        return 1

    if options.fslabel:
        fslabel = options.fslabel
        name = fslabel
    else:
        name = imgcreate.build_name(options.kscfg, options.image_type + "-")

        fslabel = imgcreate.build_name(options.kscfg,
                                        options.image_type + "-",
                                        maxlen = imgcreate.FSLABEL_MAXLEN,
                                        suffix = "%s-%s" %(os.uname()[4], time.strftime("%Y%m%d%H%M")))

        logging.info("Using label '%s' and name '%s'" % (fslabel, name))

    if options.title:
        title = options.title
    else:
        try:
            title = " ".join(name.split("-")[:2])
            title = title.title()
        except:
            title = "Linux"
    if options.product:
        product = options.product
    else:
        try:
            product = " ".join(name.split("-")[:2])
            product = product.title()
        except:
            product = "Linux"
    logging.info("Using title '%s' and product '%s'" % (title, product))

    try:
        ks = imgcreate.read_kickstart(options.kscfg)
    except KickstartError as e:
        logging.error("kickstart error: %s", e)
        return 1

    if not ks.handler.repo.seen:
        print("Kickstart (%s) must have at least one repository." % (options.kscfg), file=sys.stderr)
        return 1

    try:
        if options.image_type == 'livecd':
            creator = imgcreate.LiveImageCreator(ks, name,
                                            fslabel=fslabel,
                                            releasever=options.releasever,
                                            tmpdir=os.path.abspath(options.tmpdir),
                                            useplugins=options.plugins,
                                            title=title, product=product,
                                            cacheonly=options.cacheonly,
                                            docleanup=not options.nocleanup,
                                            build_id=options.build_id,
                                            isotype=options.isotype)
        elif options.image_type == 'image':
            creator = imgcreate.LoopImageCreator(ks, name,
                                            fslabel=fslabel,
                                            releasever=options.releasever,
                                            useplugins=options.plugins,
                                            tmpdir=os.path.abspath(options.tmpdir),
                                            cacheonly=options.cacheonly,
                                            docleanup=not options.nocleanup)
    except imgcreate.CreatorError as e:
        logging.error(u"%s creation failed: %s", options.image_type, e)
        return 1

    creator.compress_args = options.compress_args
    creator.skip_compression = options.skip_compression
    creator.skip_minimize = options.skip_minimize
    if options.cachedir:
        options.cachedir = os.path.abspath(options.cachedir)

    try:
        creator.mount(options.base_on, options.cachedir)
        creator.install()
        if (options.flat_squashfs and
          'rd.live.overlay.overlayfs' not in ks.handler.bootloader.appendLine):
            ks.handler.bootloader.appendLine += 'rd.live.overlay.overlayfs'
        creator.configure()
        if options.give_shell:
            print("Launching shell. Exit to continue.")
            print("----------------------------------")
            creator.launch_shell()
        creator.unmount()
        ops = []
        if 'rd.live.overlay.overlayfs' in ks.handler.bootloader.appendLine:
            ops += ['flatten-squashfs']
        creator.package(ops=ops)
    except (imgcreate.CreatorError, DnfBaseError) as e:
        logging.error(u"Error creating Live CD : %s" % e)
        return 1
    finally:
        creator.cleanup()

    return 0

def do_nss_sss_hack():
    import ctypes as forgettable
    hack = forgettable._dlopen('libnss_sss.so.2')
    del forgettable
    return hack

if __name__ == "__main__":
    #hack = do_nss_sss_hack()
    sys.exit(main())
