#!/usr/bin/python

# $Progeny: do-apt-cdrom,v 1.7 2002/04/23 21:35:59 branden Exp $

import sys
import os
import gtk
import gnome.ui

TRUE = 1
FALSE = 0

def cdrom_add():
    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
    update_apt = TRUE

    while not finished:
        if found:
            dialog = gnome.ui.GnomeMessageBox("Please insert the installer disc 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()

        if cdrom_add():
            found = TRUE
            message = "Disc registered.  Would you like to register another one?"
        else:
            if not found:
                finished = TRUE
                continue
            message = "There was an error registering the disc.  Would you like to try again?"

        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
    if found or update_apt:
        if os.system("apt-get -yq update") != 0:
            message = "Failed to retrieve package information.  Corrupt or absent /etc/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:
        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()
        sys.exit(1)
    else:
        sys.exit(0)

if __name__ == "__main__":
    main()

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