#!/usr/bin/python3
# -*- mode: python -*-
#
# This file is part of Plinth.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

"""
Configuration helper for Minetest server.
"""

import argparse
import augeas

from plinth import action_utils


CONFIG_FILE = '/etc/minetest/minetest.conf'
AUG_PATH = '/files' + CONFIG_FILE + '/.anon'

def load_augeas():
    """Initialize Augeas."""
    aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD +
                        augeas.Augeas.NO_MODL_AUTOLOAD)
    aug.set('/augeas/load/Php/lens', 'Php.lns')
    aug.set('/augeas/load/Php/incl[last() + 1]', CONFIG_FILE)
    aug.load()
    return aug

aug = load_augeas()

def parse_arguments():
    """Return parsed command line arguments as dictionary"""
    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers(dest='subcommand', help='Sub command')

    configure = subparsers.add_parser('configure', help='Configure Minetest')
    configure.add_argument('--max_players', help='Set maximum number of players')
    configure.add_argument('--creative_mode', choices=['true', 'false'],
                           help='Set creative mode true/false')
    configure.add_argument('--enable_pvp', choices=['true', 'false'],
                           help='Set player Vs player true/false')
    configure.add_argument('--enable_damage', choices=['true', 'false'],
                           help='Set damage true/false')

    return parser.parse_args()

def subcommand_configure(arguments):
    """Configure Minetest."""
    if arguments.max_players is not False:
        set_max_players(arguments.max_players)
    if arguments.creative_mode:
        set_creative_mode(arguments.creative_mode)
    if arguments.enable_pvp:
        enable_pvp(arguments.enable_pvp)
    if arguments.enable_damage:
        enable_damage(arguments.enable_damage)
    action_utils.service_restart('minetest-server')

def set_max_players(max_players):
    """Sets the number of max players"""

    aug.set(AUG_PATH + '/max_users', max_players)

    aug.save()

def enable_pvp(choice):
    """Enables pvp"""

    aug.set(AUG_PATH + '/enable_pvp', choice)

    aug.save()

def set_creative_mode(choice):
    """Enables or disables creative mode"""

    aug.set(AUG_PATH + '/creative_mode', choice)

    aug.save()

def enable_damage(choice):
    """Enables damage"""

    aug.set(AUG_PATH + '/enable_damage', choice)

    aug.save()

def main():
    """Parse arguments and perform all duties"""
    arguments = parse_arguments()

    subcommand = arguments.subcommand.replace('-', '_')
    subcommand_method = globals()['subcommand_' + subcommand]
    subcommand_method(arguments)


if __name__ == '__main__':
    main()
