#!/usr/bin/python

# $Progeny: do-apt-cdrom,v 1.11 2002/05/28 20:35:39 jlicquia Exp $

import sys
import os
import GDK
import gtk
import gnome.ui

TRUE = 1
FALSE = 0

def cdrom_add():
    # XXX: this assumes that the CD-ROM device can be found at /cdrom
    retval = os.system("mount /cdrom")
    if retval:
        return FALSE

    if os.path.isdir("/cdrom/.disk"):
        retval = os.system("apt-cdrom -d=/cdrom --no-mount add")
    else:
        retval = 1

    os.system("umount /cdrom")

    if retval:
        return FALSE
    else:
        return TRUE

def main():
    found = FALSE
    finished = FALSE

    # Check to see if we need to handle sources.list munging;
    # we won't if aptconf was available during configlet run.

    if not os.path.exists("/usr/share/configlets/aptconf"):
        gtk._root_window().set_cursor(gtk.cursor_new(GDK.LEFT_PTR))

        dialog = gnome.ui.GnomeMessageBox("""CD-ROM or DVD-ROM discs with Debian packages can be registered in
the APT package tool's list of package sources.

Would you like to scan the CD/DVD-ROM drive for discs with Debian
package archives?
""",
                                          gnome.ui.MESSAGE_BOX_QUESTION,
                                          gnome.ui.STOCK_BUTTON_YES,
                                          gnome.ui.STOCK_BUTTON_NO)
        dialog.set_position(gtk.WIN_POS_CENTER)
        retval = dialog.run_and_close()

        if retval == 0:
            gtk._root_window().set_cursor(gtk.cursor_new(GDK.WATCH))

            while not finished:
                if found:
                    gtk._root_window().set_cursor(gtk.cursor_new(GDK.LEFT_PTR))
                    dialog = gnome.ui.GnomeMessageBox("""Please insert the installer disc (or another disc containing a
Debian package archive) and click OK to continue.""",
                                                      gnome.ui.MESSAGE_BOX_INFO,
                                                      gnome.ui.STOCK_BUTTON_OK)
                    dialog.set_position(gtk.WIN_POS_CENTER)
                    dialog.run_and_close()

                gtk._root_window().set_cursor(gtk.cursor_new(GDK.WATCH))
                if cdrom_add():
                    found = TRUE
                    message = """Disc registered as an APT source.  Would you like to register
another one?"""
                else:
                    if not found:
                        finished = TRUE
                        continue
                    message = """There was an error registering the disc as an APT source.  There
may be no disc in the drive, or the disc may not have a package
archive on it.

Would you like to try again?"""

                gtk._root_window().set_cursor(gtk.cursor_new(GDK.LEFT_PTR))
                dialog = gnome.ui.GnomeMessageBox(message,
                                                  gnome.ui.MESSAGE_BOX_QUESTION,
                                                  gnome.ui.STOCK_BUTTON_YES,
                                                  gnome.ui.STOCK_BUTTON_NO)
                dialog.set_position(gtk.WIN_POS_CENTER)
                retval = dialog.run_and_close()

                finished = (retval != 0)

    # Update the dpkg available file.
    message = None

    gtk._root_window().set_cursor(gtk.cursor_new(GDK.WATCH))
    if os.system("apt-get -yq update") != 0:
        message = """Failed to retrieve package information.  Corrupt or absent
/etc/apt/sources.list?"""
    elif os.system("apt-cache dumpavail > /tmp/avail") != 0:
        message = "Failed to generate available file."
    elif os.system("dpkg --update-avail /tmp/avail") != 0:
        message = "Failed to update available file."

    if message:
        gtk._root_window().set_cursor(gtk.cursor_new(GDK.LEFT_PTR))
        dialog = gnome.ui.GnomeMessageBox(message, gnome.ui.MESSAGE_BOX_ERROR, gnome.ui.STOCK_BUTTON_OK)
        dialog.set_position(gtk.WIN_POS_CENTER)
        dialog.run_and_close()
        gtk._root_window().set_cursor(gtk.cursor_new(GDK.X_CURSOR))
        sys.exit(1)
    else:
        gtk._root_window().set_cursor(gtk.cursor_new(GDK.X_CURSOR))
        sys.exit(0)

if __name__ == "__main__":
    main()

# vim:ai:et:sts=4:sw=4:tw=0:
