#!/usr/bin/python2
#
# firstboot - Script to run the firstboot system configuration program.
#
# Copyright (C) 2002, 2003, 2005 Red Hat, Inc.
# Copyright (C) 2002, 2003 Brent Fox <bfox@redhat.com>
# Copyright (C) 2005 Chris Lumens <clumens@redhat.com>
#
# 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; either version 2 of the License, or
# (at your option) any later version.
#
# 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., 675 Mass Ave, Cambridge, MA 02139, USA.

import os
import signal
import string
import sys
import rhpl.arch

sys.path.append("/usr/share/firstboot")

def getRunlevel():
    line = os.popen('/sbin/runlevel', 'r').readline()
    line = string.strip(line)

    # This can happen in kadischi, for instance.
    if line.startswith("unknown"):
        return 3
    else:
        tokens = string.split(line)
        return int(tokens[-1])

def rhgbIsRunning():
    return os.access("/usr/bin/rhgb-client", os.R_OK|os.X_OK) and os.system("/usr/bin/rhgb-client --ping") == 0

# Since X may be starting up immediately after firstboot crashes, we might
# not have any way to see the traceback.  This lets it be logged somewhere
# useful.
def defaultExnHandler(type, value, tb):
    import traceback
    f = open("/tmp/firstboot-crash.log", "w")
    traceback.print_exception(type, value, tb, None, f)
    f.close()

    # Also, print on stderr still just in case we have time to see the
    # problem.
    traceback.print_tb(tb)

sys.excepthook = defaultExnHandler

if __name__ == "__main__":

    # First, check to see whether this is installed on a system with X or not.
    # We do this by trying to import something that only exists in the
    # graphical firstboot package.
    try:
        from xfirstboot import XFirstboot
        noX = False
        fb = XFirstboot()
    except ImportError:
        from firstboot import Firstboot
        noX = True
        fb = Firstboot()

    for arg in sys.argv:
        if arg == '--reconfig':
            print "starting reconfig mode"
            fb.doReconfig = True
        if arg == '--debug':
            print "starting with debugging options"
            fb.doDebug = True
        if arg == '--lowres':
            print "starting in lowres mode"
            fb.lowRes = True
        if arg == '--autoscreenshot':
            fb.autoscreenshot = True
        if arg == '--forcegui':
            fb.forcegui = True

    signal.signal (signal.SIGINT, signal.SIG_DFL)

    if not fb.mayRun():
        print "firstboot cannot run, exiting"
        os._exit(0)

    # If we have a $DISPLAY set, we are either running in debug mode or in
    # reconfig mode from a terminal under an already running X setup.
    # Otherwise, run in text mode if in runlevel 3, or take over rhgb's X
    # server if running, or start up our own if not.
    if os.environ.has_key("DISPLAY") or fb.doDebug:
        from firstbootWindow import firstbootWindow
        firstbootWindow(fb)
    elif (getRunlevel() == 3 and not fb.forcegui) or noX == True:
        fb.runTextUI()
    elif rhgbIsRunning():
        fb.startRhgbUI()
        from firstbootWindow import firstbootWindow
        firstbootWindow(fb)
    elif not os.environ.has_key("DISPLAY"):
        fb.startGraphicalUI()
        from firstbootWindow import firstbootWindow
        firstbootWindow(fb)
    else:
        raise RuntimeError, "Could not start any firstboot interface"