#!/home/vlt-os/env/bin/python
"""This file is part of Vulture OS.

Vulture OS 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 3 of the License, or
(at your option) any later version.

Vulture OS 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 Vulture OS.  If not, see http://www.gnu.org/licenses/.
"""

__author__ = "Jérémie JOURDIN"
__credits__ = []
__license__ = "GPLv3"
__version__ = "4.0.0"
__maintainer__ = "Vulture OS"
__email__ = ""
__doc__ = 'wrapper arround global Config class'

import os
import sys

sys.path.append("/home/vlt-os/vulture_os/")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", 'vulture_os.settings')

import django

try:
    django.setup()
    from django.core.management import call_command
    with open(os.devnull, 'w') as f:
        call_command('check', stdout = f)
except:
    print ("Error: This node is not bootstraped.")
    sys.exit(1)

from system.config.models import Config
config = Config.objects.get()

if len(sys.argv) <= 1:
    print("Usage: /home/vlt-adm/gui/configure <param-name>=<param_value>")
    print("")
    print("This command set global cluster configuration. Available parameters are:")
    for p in config.__dict__:
        if str(p)=="_state" or str(p)=="id":
            continue
        print("\t- {}".format(str(p)))

    sys.exit(1)


for p in sys.argv[1:]:
    if p == sys.argv[0]:
        continue
    try:
        k = p.split('=')[0]
        v = '='.join(p.split('=')[1:])
        setattr(config, k, v)
        print("Setting '{}' to '{}'".format(k,v))

    except:
        print("Invalid syntax on {}".format(p))
        sys.exit(1)

print("Syncing config with mongodb")
config.save()
sys.exit(0)



