# -*- coding: utf-8 -*-
#
# Chris Lumens <clumens@redhat.com>
#
# Copyright 2008 Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use, modify,
# copy, or redistribute it subject to the terms and conditions of the GNU
# General Public License v.2.  This program is distributed in the hope that it
# will be useful, but WITHOUT ANY WARRANTY expressed or implied, including the
# implied warranties 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., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  Any Red Hat
# trademarks that are incorporated in the source code or documentation are not
# subject to the GNU General Public License and may only be used or replicated
# with the express permission of Red Hat, Inc. 
#
import gtk
import libuser
import os, string, sys, time
import os.path

from firstboot.config import *
from firstboot.constants import *
from firstboot.functions import *
from firstboot.module import *

import gettext
_ = lambda x: gettext.ldgettext("firstboot", x)
N_ = lambda x: x

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

class moduleClass(Module):
    def __init__(self):
        Module.__init__(self)
        self.priority = 100
        self.sidebarTitle = N_("OpenAFS")
        self.title = N_("OpenAFS")
        self.icon = "openafs.png"

        self.admin = libuser.admin()
        self.nisFlag = None

        self._problemFiles = []

        self._count = 0

    def apply(self, interface, testing=False):
        if testing:
            return RESULT_SUCCESS

        newcellname = self.afscellEntry.get_text()
        newcellname = string.strip(newcellname)
	
	if newcellname == "":
            dlg = gtk.MessageDialog(None, 0, gtk.MESSAGE_WARNING, gtk.BUTTONS_NONE,
                                    (_("It is highly recommended that a AFS cell be set. "
                                       "AFS cannot start without a cell being set.")))

            dlg.set_position(gtk.WIN_POS_CENTER)
            dlg.set_modal(True)

            dlg.add_button(_("Continue"), 0)
            b = dlg.add_button(_("Add AFS Cell"), 1)
            b.grab_focus()

            rc = dlg.run()
            dlg.destroy()

            if rc == 0:
                return 0
            else:
                self.afscellEntry.grab_focus()
                return RESULT_FAILURE
	
	f=open('/usr/vice/etc/ThisCell' , 'w')
	f.write("%s\n" % newcellname)
	f.close()
	
	AFSStartup = self.afsCheckButton.get_active()
	if AFSStartup == True:
		os.system('/sbin/chkconfig --level 345 afs on')
	else:
		os.system('/sbin/chkconfig --level 345 afs off')
	
	TestCell = os.system('T=`cat /usr/vice/etc/ThisCell` ; grep -w -q -s \>$T /usr/vice/etc/CellServDB')
	if TestCell != 0:
            os.system('echo no > /tmp/testfile')
            dlg = gtk.MessageDialog(None, 0, gtk.MESSAGE_WARNING, gtk.BUTTONS_NONE,
                                    (_("Your AFS cell is not found in the AFS list of cells.  "
                                       "AFS cannot start without a cell being set.  " 
                                       "If you continue AFS will not be started.")))

            dlg.set_position(gtk.WIN_POS_CENTER)
            dlg.set_modal(True)

            dlg.add_button(_("Continue"), 0)
            b = dlg.add_button(_("Change AFS Cell"), 1)
            b.grab_focus()

            rc = dlg.run()
            dlg.destroy()

            if rc == 0:
                os.system('/sbin/chkconfig --level 345 afs off')
		return 0
            else:
                self.afscellEntry.grab_focus()
                return RESULT_FAILURE

	if newcellname != "openafs.org":
		os.system('/etc/init.d/afs start')

	
        return RESULT_SUCCESS

    def createScreen(self):
        self.vbox = gtk.VBox(spacing=10)

        label = gtk.Label(_("This is the OpenAFS configuration Window.\n"
                            "There are many AFS cells that you might want to bind to.\n"
                            "\n"
                            "Some of the most common AFS cells are\n"
                            "\n"
                            "Cell Name   Lab Site\n"
                            "========  ======\n"
                            "anl.gov      Argonne\n"
                            "cern.ch      CERN\n"
                            "desy.de     DESY\n"
                            "fnal.gov     Fermilab\n"
                            "\n"))

        label.set_line_wrap(True)
        label.set_alignment(0.0, 0.5)
        label.set_size_request(500, -1)

	f=open('/usr/vice/etc/ThisCell')
	oldcellname = f.readline()
	f.close()
        oldcellname = string.strip(oldcellname)

        self.afscellEntry = gtk.Entry()
        self.afscellEntry.set_text(oldcellname)

        self.vbox.pack_start(label, False, True)

        table = gtk.Table(1, 2)
        table.set_row_spacings(6)
        table.set_col_spacings(6)
        label = gtk.Label(_("AFS Cell:"))
        label.set_mnemonic_widget(self.afscellEntry)
        label.set_alignment(0.0, 0.5)
        table.attach(label, 0, 1, 0, 1, gtk.FILL)
        table.attach(self.afscellEntry, 1, 2, 0, 1, gtk.SHRINK, gtk.FILL, 5)

        self.vbox.pack_start(table, False)

	self.afsCheckButton = gtk.CheckButton(_("_Start AFS on startup"))
	self.afsCheckButton.set_active(True)
        a = gtk.Alignment(0.0, 0.5)
        a.add(self.afsCheckButton)
	self.vbox.pack_start(a, False)

    def focus(self):
        self.afscellEntry.grab_focus()

    def initializeUI(self):
        pass
