from Tkinter import *
from rhtkinter import *
from buttonbar import ButtonBar
from rhutil import rhsystem

import rhdialog

class RootPasswordWin(RHFrame):

    def createWidgets(self):
	m = Message(self, { 'text' : 'Pick a root password. You must ' +
			'type it twice so I can be sure you know what it ' +
			'is. None of the password will be shown in the box.',
			    'aspect' : '600' })
	m.pack()

	f = Frame(self)
	lf = Frame(f)
	rf = Frame(f)

	lf.pack({'side' : 'left'})
	rf.pack({'side' : 'left', 'fill' : 'x', 'expand' : '1' })

	l = Label(lf, { 'text' : 'Root Password' })
	l.pack({ 'anchor' : 'w' })
	l = Label(lf, { 'text' : 'Root Password (again)' })
	l.pack({ 'anchor' : 'w' })

	self.first = RHEntry(rf, { 'relief' : 'sunken' })
	self.first.pack({ 'fill' : 'x'})
	self.first['show'] = '#'
	self.first.focus_set()
	self.second = RHEntry(rf, { 'relief' : 'sunken' })
	self.second.pack({ 'fill' : 'x'})
	self.second['show'] = '#'

	self.first.bind('<Return>', lambda event, f = self.second.focus_set: f())
	self.second.bind('<Return>', self.okay)
	f.pack({ 'expand' : '1', 'fill' : 'both' })

	b = ButtonBar(self)
	b.addButton("Okay", self.okay)
	b.addButton("Cancel", self.cancel)
	b.pack({ 'fill' : 'x' })

	self.pack()

    def okay(self, event = None):
	pw = self.first.get()
	if (pw != self.second.get()):
	    rhdialog.error("Those passwords don't match!")
	    self.first.delete('0', 'end')
	    self.second.delete('0', 'end')
	    self.first.focus_set()
	else:
	    self.pw = pw
	    self.quit()

    def getpw(self):
	return self.pw

    def cancel(self):
	self.quit()

    def __init__(self):
	self.pw = None
	master = Toplevel()
	RHFrame.__init__(self, master)
	master.title("Root Password")

	self.createWidgets()

def setrootpw(test):
    p = RootPasswordWin()
    p.update()
    p.wait_window(p)

    pw = p.getpw()
    if (pw):
	rhsystem("/usr/rhs/install/rhcrypt", ("--root", "/mnt", pw), test)
