#!/usr/bin/python
# -*- encoding: utf8 -*-
#
# The Qubes OS Project, http://www.qubes-os.org
#
# Copyright (C) 2012  Marek Marczykowski <marmarek@invisiblethingslab.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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
#

from qubes.qubes import QubesVmCollection
from qubes.qubes import QubesHost
from qubes.qubes import system_path
from optparse import OptionParser
import subprocess
import os
import sys
from qubes.qubes import vmm


def handle_vm(vms, label, new_value = None):
    functions = { # label: [ getter, setter ],
        'default-netvm': [ 'get_default_netvm', 'set_default_netvm' ],
        'default-fw-netvm': [ 'get_default_fw_netvm', 'set_default_fw_netvm' ],
        'default-template': [ 'get_default_template', 'set_default_template' ],
        'clockvm': [ 'get_clockvm_vm', 'set_clockvm_vm' ],
        'updatevm': [ 'get_updatevm_vm', 'set_updatevm_vm' ],
    }
    assert label in functions.keys()

    if new_value:
        if new_value == "none":
            try:
                vms.__getattribute__(functions[label][1])(None)
            except Exception as e:
                print >> sys.stderr, "ERROR: {0}".format(str(e))
                exit(1)
        else:
            vm = vms.get_vm_by_name (new_value)
            if vm is None:
                print >> sys.stderr, "A VM with the name '{0}' does not exist in the system.".format(new_value)
                exit(1)
            try:
                vms.__getattribute__(functions[label][1])(vm)
            except Exception as e:
                print >> sys.stderr, "ERROR: {0}".format(str(e))
                exit(1)
    else:
        vm = vms.__getattribute__(functions[label][0])()
        if vm is not None:
            return vm.name
        else:
            return ""

def handle_kernel(vms, label, new_value = None):
    if new_value is not None:
        if not os.path.exists(os.path.join(system_path["qubes_kernels_base_dir"], new_value)):
            print >> sys.stderr, "Kernel version {0} not installed.".format(new_value)
            print >> sys.stderr, "Available versions:"
            for k in os.listdir(system_path["qubes_kernels_base_dir"]):
                print >> sys.stderr, "  -", k
            exit(1)
        vms.set_default_kernel(new_value)
    else:
        return vms.get_default_kernel()

preferences = {
    "default-netvm": handle_vm,
    "default-fw-netvm": handle_vm,
    "default-template": handle_vm,
    "clockvm": handle_vm,
    "updatevm": handle_vm,
    "default-kernel": handle_kernel,
}

def do_list(vms):
    label_width = 18
    fmt="{{0:<{0}}}: {{1}}".format(label_width)
    for pref in sorted(preferences.items()):
        print fmt.format (pref[0], pref[1](vms, pref[0]))

def main():
    usage = "usage: %prog [-l]\n"\
            "usage: %prog [-g] <property>\n"\
            "usage: %prog [-s] <property> <new-value>\n"\
            "List/set various global properties."

    parser = OptionParser (usage)
    parser.add_option ("-l", "--list", action="store_true", dest="do_list", default=False)
    parser.add_option ("-s", "--set", action="store_true", dest="do_set", default=False)
    parser.add_option ("-g", "--get", action="store_true", dest="do_get", default=False)

    (options, args) = parser.parse_args ()

    if options.do_list + options.do_set + options.do_get > 1:
        print >> sys.stderr, "You can provide only one action at once!"
        exit (1)

    # Select action based on args count:
    if not options.do_list and not options.do_get and not options.do_set:
        if (len (args) < 1):
            options.do_list = True
        elif (len (args) == 1):
            options.do_get = True
        else:
            options.do_set = True

    vmm.offline_mode = True

    if options.do_set:
        qvm_collection = QubesVmCollection()
        qvm_collection.lock_db_for_writing()
        qvm_collection.load()
    else:
        qvm_collection = QubesVmCollection()
        qvm_collection.lock_db_for_reading()
        qvm_collection.load()
        qvm_collection.unlock_db()

    if options.do_set:
        if len (args) < 2 or args[0] not in preferences.keys():
            print >> sys.stderr, "You must specify the property and the new value you wish to set..."
            print >> sys.stderr, "Available properties:"
            for p in sorted(preferences.keys()):
                print >> sys.stderr, "--> '{0}'".format(p)
            exit (1)

        pref = args[0]
        new_value = args[1]
        preferences[pref](qvm_collection, pref, new_value)
        qvm_collection.save()
        qvm_collection.unlock_db()

    elif options.do_get:
        if len (args) < 1 or args[0] not in preferences.keys():
            print >> sys.stderr, "You must specify the property you wish to get..."
            print >> sys.stderr, "Available properties:"
            for p in sorted(preferences.keys()):
                print >> sys.stderr, "--> '{0}'".format(p)
            exit (1)
        pref = args[0]
        print preferences[pref](qvm_collection, pref)

    else:
        # do_list
        do_list(qvm_collection)

main()