#! /usr/bin/env python
## netconf - A switch mail tool
## Copyright (C) 2002 Red Hat, Inc.
## Copyright (C) 2002 Than Ngo <than@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 sys

SWITCHMAILDIR = '/usr/share/redhat-switch-mail/'
PROGNAME = 'redhat-switch-mail'

if not "/usr/lib/rhs/python" in sys.path:
    sys.path.append("/usr/lib/rhs/python")

if not SWITCHMAILDIR in sys.path:
    sys.path.append(SWITCHMAILDIR)

import getopt
import signal
import os
import os.path
import string
import gettext
from functions import *


##
## I18N
##
gettext.bindtextdomain(PROGNAME, '/usr/share/locale')
gettext.textdomain(PROGNAME)

try:
    gettext.install(PROGNAME, '/usr/share/locale')
except IOError:
    import __builtin__
    __builtin__.__dict__['_'] = unicode    

INFO_TEXT = _("This software is distributed under the GPL. Please Report bugs to Red Hat's Bug Tracking System: http://bugzilla.redhat.com/")

def main():
    hasGui = 0
    signal.signal(signal.SIGINT, signal.SIG_DFL)
    try:
        if os.access(SWITCHMAILDIR + 'switchmail_gui.py', os.R_OK) \
               or os.access('switchmail_gui.py', os.R_OK):
            if os.environ['DISPLAY'] != '':
                hasGui = 1
    except:
        pass

    if os.path.basename(sys.argv[0]) == 'redhat-switch-mail-nox':
        hasGui = 0

    if os.getuid() != 0:
        rootWarning(hasGui)
        sys.exit(1)

    if hasGui:
        from switchmail_gui import mainDialog
        dlg = mainDialog().main()
    else:
        from switchmail_tui import mainDialog
        dlg = mainDialog().main()
        
def rootWarning(hasGui):
    errMsg =  _('You must run Red Hat Switch Mail as root!')
    if hasGui:
        from switchmail_gui import mainDialog
        mainDialog().rootWarning(errMsg)
    else:
        print errMsg


if __name__ == '__main__':
    main()
    sys.exit(0)
