#! /usr/bin/env python

# configlets-text -- Text-mode configlet frontend (uses dpkg-reconfigure)

# $Progeny$

# Copyright (C) 2001  Progeny Linux Systems, Inc.
# AUTHORS: Eric Gillespie, Jr. <epg@progeny.com>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2,  as
# published by the Free Software Foundation.

# 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 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

import os
import re
import string

from configlet import BasicConfigGroup

class TextConfigGroup(BasicConfigGroup):
    def show(self):
        packages = []
        shared_questions = []
        for configlet in self:
            # It should be this simple...
            #packages.extend(configlet.get_packages())
            # But stupid dpkg-reconfigure exits as soon as it sees a
            # package where package.config does not exist.  >sigh<

            for pkg in configlet.get_packages():
                if os.path.exists("/var/lib/dpkg/info/%s.config"
                                  % (pkg,)):
                    if pkg not in packages:
                        packages.append(pkg)

            shared_questions.extend(configlet.get_shared_questions())

        dcdata = self.dc.get()
        if os.environ.has_key("DEBUG"):
            dcpipe_command = "debconf-communicate"
        else:
            dcpipe_command = "debconf-communicate > /dev/null"
        dcpipe = os.popen(dcpipe_command, "w")
        for i in dcdata:
            (template, question, value) = re.split(r"\s+", i, 2)
            (pkg, var) = re.split("/", question, 1)

            if pkg in packages:
                dcpipe.write("fset %s seen false\n" % (question,))

        for i in shared_questions:
            dcpipe.write("fset %s seen false\n" % (i,))

        dcpipe.close()

        packages_string = string.join(packages)
        os.system("dpkg-reconfigure --unseen %s" % (packages_string,))

if __name__ == "__main__":
    os.environ["DEBIAN_PRIORITY"] = "low"
    os.environ["DEBIAN_FRONTEND"] = "teletype"

    cfggroup = TextConfigGroup("/usr/share/configlets")
    cfggroup.show()
