#!/usr/bin/python

import pygtk
pygtk.require('2.0')
import gtk
import gobject

import random
import string
import crypt
import pwd
import subprocess

def event_delete(widget, event, data=None):
    return False

def dialog_response(dialog, response):
    dialog.destroy()

def event_combobox(widget, data=None):
    if widget.get_active() == 0:
        try:
            subprocess.call('setxkbmap fr', shell=True)
            subprocess.call('sysconfig-cli keyboard KEYTABLE fr-latin1.map.gz', shell=True)
        except:
            alert = gtk.MessageDialog(type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_OK)
            alert.set_markup("Can't set keymap to FR")
            alert.connect('response', dialog_response)
            alert.run()
    elif widget.get_active() == 1:
        try:
            subprocess.call('setxkbmap us', shell=True)
            subprocess.call('sysconfig-cli keyboard KEYTABLE us', shell=True)
        except:
            alert = gtk.MessageDialog(type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_OK)
            alert.set_markup("Can't set keymap to US")
            alert.connect('response', dialog_response)
            alert.run()

    return

def exitbutton_clicked(widget, data):
    if not data:
        gtk.main_quit()

    login = data['login'].get_text()
    name = data['name'].get_text()
    firstname = data['firstname'].get_text()
    passwd1 =  data['passwd'].get_text()
    passwd2 = data['passwd_confirm'].get_text()

    if login == "":
        alert = gtk.MessageDialog(type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_OK)
        alert.set_markup("Empty login")
        alert.connect('response', dialog_response)
        alert.run()
        return

    if name == "":
        alert = gtk.MessageDialog(type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_OK)
        alert.set_markup("Empty name")
        alert.connect('response', dialog_response)
        alert.run()
        return

    if firstname == "":
        alert = gtk.MessageDialog(type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_OK)
        alert.set_markup("Empty firstname")
        alert.connect('response', dialog_response)
        alert.run()
        return

    if passwd1 == "":
        alert = gtk.MessageDialog(type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_OK)
        alert.set_markup("Empty password")
        alert.connect('response', dialog_response)
        alert.run()
        return

    try:
        pwd.getpwnam(login)
        alert = gtk.MessageDialog(type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_OK)
        alert.set_markup("User already exists")
        alert.connect('response', dialog_response)
        alert.run()
        return
    except KeyError:
        pass

    if passwd1 != passwd2:
        alert = gtk.MessageDialog(type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_OK)
        alert.set_markup("Password doesn't match")
        alert.connect('response', dialog_response)
        alert.run()
        return

    salt = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for x in range(12))
    crypted = crypt.crypt(passwd1, '$6$' + salt + '$')
    try:
        subprocess.call('useradd -m -c \'{0} {1}\' -d /home/{2} -k /etc/skel-BLINUX/ -g users -p \'{3}\' {4}'.format(firstname,
                                                                                                                 name,
                                                                                                                 login,
                                                                                                                 crypted,
                                                                                                                 login),
                        shell=True)
    except OSError:
        alert = gtk.MessageDialog(type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_OK)
        alert.set_markup("Can't add user")
        alert.connect('response', dialog_response)
        alert.run()
        return

    try:
        subprocess.call('hostnamectl set-hostname pc-{0}'.format(login), shell=True)
    except OSError:
        alert = gtk.MessageDialog(type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_OK)
        alert.set_markup("Can't set hostname")
        alert.connect('response', dialog_response)
        alert.run()
        return

    try:
        subprocess.call('sysconfig-cli displaymanager DISPLAYMANAGER lightdm', shell=True)
    except OSError:
        alert = gtk.MessageDialog(type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_OK)
        alert.set_markup("Can't set DISPLAYMANAGER")
        alert.connect('response', dialog_response)
        alert.run()

    try:
        subprocess.call('salt-setup', shell=True)
    except OSError:
        alert = gtk.MessageDialog(type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_OK)
        alert.set_markup("Can't setup salt")
        alert.connect('response', dialog_response)
        alert.run()
        return

    try:
        subprocess.call('shutdown -r now', shell=True)
    except OSError:
        alert = gtk.MessageDialog(type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_OK)
        alert.set_markup("Can't reboot the machine")
        alert.connect('response', dialog_response)
        alert.run()
        return

    gtk.main_quit()

def destroy(widget, data=None):
    gtk.main_quit()

def mainwin():
    window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    window.connect("delete_event", event_delete)
    window.connect("destroy", destroy)
    window.set_border_width(10)

    vbox = gtk.VBox(False, 0)

    welcome = gtk.Label("Blinux Setup")
    welcome.show()
    vbox.pack_start(welcome, False, True, 50)

    table = gtk.Table(6, 3, False)
    table.show()
    vbox.pack_start(table, True, True, 0)

    login_label = gtk.Label("Login : ")
    login_label.show()
    table.attach(login_label, 0, 1, 0, 1)

    name_label = gtk.Label("Name : ")
    name_label.show()
    table.attach(name_label, 0, 1, 1, 2)

    firstname_label = gtk.Label("Firstname : ")
    firstname_label.show()
    table.attach(firstname_label, 0, 1, 2, 3)

    password_label = gtk.Label("Password : ")
    password_label.show()
    table.attach(password_label, 0, 1, 3, 4)

    password_label2 = gtk.Label("Confirm Password : ")
    password_label2.show()
    table.attach(password_label2, 0, 1, 4, 5)

    login_entry = gtk.Entry()
    login_entry.set_max_length(16)
    login_entry.show()
    table.attach(login_entry, 1, 2, 0, 1)

    name_entry = gtk.Entry()
    name_entry.set_max_length(32)
    name_entry.show()
    table.attach(name_entry, 1, 2, 1, 2)

    firstname_entry = gtk.Entry()
    firstname_entry.set_max_length(32)
    firstname_entry.show()
    table.attach(firstname_entry, 1, 2, 2, 3)

    password_entry = gtk.Entry()
    password_entry.set_max_length(50)
    password_entry.set_visibility(False)
    password_entry.show()
    table.attach(password_entry, 1, 2, 3, 4)

    password_entry2 = gtk.Entry()
    password_entry2.set_max_length(50)
    password_entry2.set_visibility(False)
    password_entry2.show()
    table.attach(password_entry2, 1, 2, 4, 5)

    hbox = gtk.HBox(False, 0)
    hbox.show()
    vbox.pack_start(hbox, False, False, 0)

    combobox = gtk.combo_box_new_text()
    combobox.append_text("FR")
    combobox.append_text("US")
    combobox.set_active(0)
    combobox.connect("changed", event_combobox)
    combobox.show()
    hbox.pack_start(combobox, False, False, 0)

    exitbutton = gtk.Button("Exit (Discard Changes)")
    exitbutton.connect("clicked", exitbutton_clicked, None)
    exitbutton.show()
    hbox.pack_start(exitbutton, False, False, 0)

    exitbutton2 = gtk.Button("Exit (Save Changes)")
    exitbutton2.connect("clicked", exitbutton_clicked, {'mainwin' : window, 'login' : login_entry, 'name' : name_entry, 'firstname' : firstname_entry, 'passwd' : password_entry, 'passwd_confirm' : password_entry2})
    exitbutton2.show()
    hbox.pack_start(exitbutton2, False, False, 0)

    vbox.show()
    window.add(vbox)

    window.show()

mainwin()
gtk.main()
