#!/usr/bin/python3
#
# This program is meant to be called by the debian installer in order to
# create configuration files for the checkbox package and derived packages
# based on the preseed values.

import os
import re
import sys
import subprocess
import posixpath

from configparser import ConfigParser
from optparse import OptionParser

from debconf import Debconf, DebconfCommunicator


DEFAULT_SECTION = "DEFAULT"


_delegate_methods = ("fileno", "flush",)


class fdopen_wrapper:

    _uses_bytes = False

    def __init__(self, fileno, mode):
        self.file = os.fdopen(fileno, mode)

        for method in _delegate_methods:
            setattr(self, method, getattr(self.file, method))

    def readline(self):
        line = self.file.readline()
        if fdopen_wrapper._uses_bytes:
            line = line.encode()

        return line

    def write(self, data):
        if isinstance(data, bytes):
            fdopen_wrapper._uses_bytes = True
            data = data.decode()

        return self.file.write(data)


class Config(ConfigParser):

    def write(self, fp):
        """Write an .ini-format representation of the configuration state."""
        if self._defaults:
            fp.write("[%s]\n" % DEFAULT_SECTION)
            defaults = dict(self._defaults)

            # Write includes first
            if 'includes' in defaults:
                key = 'includes'
                value = defaults.pop(key)
                value = str(value).replace('\n', '\n\t')
                fp.write("%s = %s\n" % (key, value))

            for (key, value) in defaults.items():
                value = str(value).replace('\n', '\n\t')
                fp.write("%s = %s\n" % (key, value))

            fp.write("\n")

        # Sort sections and options to prevent diffs
        sections = sorted(self._sections)
        for section in sections:
            fp.write("[%s]\n" % section)
            options = self._sections[section]
            options = [(k, options[k]) for k in sorted(options.keys())]
            for (key, value) in options:
                if key != "__name__":
                    fp.write("%s = %s\n" %
                             (key, str(value).replace('\n', '\n\t')))

            fp.write("\n")


class Install:
    """Install module for generating checkbox configuration files.

    The checkbox module and derivatives use a configuration file format
    compatible with ConfigParser. The values for the keys defined in
    this file can be preseeded during the installation of the package by
    using this module during the config phase of the package installation
    process.
    """
    question_separator = "/"
    template_separator = ": "

    configs_base = "/usr/share/%(base_name)s/configs/%(name)s.ini"
    examples_base = "/usr/share/%(base_name)s/examples/%(name)s.ini"

    def __init__(self, name, base_name=None, configs_path=None,
                 examples_path=None, templates_path=None):
        self.name = name
        self.base_name = name if base_name is None else base_name
        self._configs_path = configs_path or self.configs_base \
            % {"name": name, "base_name": self.base_name}
        self._examples_path = examples_path or self.examples_base \
            % {"name": name, "base_name": self.base_name}
        self._templates_path = subprocess.getoutput(
            "dpkg-query --control-path %(name)s templates"
            % {"name": name, "base_name": self.base_name})

        self._config = Config()
        if os.environ.get("DEBIAN_HAS_FRONTEND"):
            if os.environ.get("DEBCONF_REDIR"):
                write_fileno = 3
            else:
                write_fileno = 1

            write = fdopen_wrapper(write_fileno, "w")
            read = fdopen_wrapper(0, "r")
            self._debconf = Debconf(read=read, write=write)
        else:
            self._debconf = DebconfCommunicator(self.name)

    def write(self, file):
        """
        Write phase of the config process which takes a file object
        as argument.
        """
        for path in [self._examples_path, self._configs_path]:
            if path and posixpath.isfile(path):
                self._config.read(path)

        # Hack to retrieve questions from templates file
        if posixpath.exists(self._templates_path):
            templates_file = open(self._templates_path, encoding="utf-8")
            buffer = templates_file.read()
            for block in re.split(r"\n{2,}", buffer):
                if not block:
                    continue

                element = {}
                for line in block.split("\n"):
                    if not line or line.startswith(" "):
                        continue

                    (key, value) = line.split(self.template_separator, 1)
                    key = key.lower()
                    element[key] = value

                question = element["template"]
                old_value = element.get("default", "")
                new_value = self._debconf.get(question)
                # Only set different values
                if old_value != new_value:
                    section, name = question.rsplit(self.question_separator, 1)
                    if not self._config.has_section(section):
                        self._config.add_section(section)
                    self._config.set(section, name, new_value)

        # Write config file
        self._config.write(file)


def main(args):
    """
    Main routine for running this script. The arguments are:

    package_name    Name of the package to configure.
    optional        Optional arguments specific to the given command.
    """
    parser = OptionParser()
    parser.add_option("-o", "--output",
      default="-",
      help="Output file, - for stdout.")
    (options, args) = parser.parse_args(args)

    if len(args) < 1:
        return 1

    install = Install(*args)

    if options.output == "-":
         file = sys.stdout
    else:
         file = open(options.output, "w")
    install.write(file)

    return 0


if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))
