#!/usr/bin/python3
#
# This file is part of FreedomBox.
#
# 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/>.
#

"""
Wrapper to list and handle system services
"""

import argparse
import os

from plinth import action_utils, cfg

cfg.read()
module_config_path = os.path.join(cfg.config_dir, 'modules-enabled')

I2P_CONF_DIR = "/var/lib/i2p/i2p-config"


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

    subparsers.add_parser('start', help='start i2p service')
    subparsers.add_parser('stop', help='stop i2p service')
    subparsers.add_parser('enable', help='enable i2p service')
    subparsers.add_parser('disable', help='disable i2p service')
    subparsers.add_parser('restart', help='restart i2p service')
    subparsers.add_parser('is-running', help='status of a service')
    subparsers.add_parser('is-enabled', help='status a service')

    subparser = subparsers.add_parser('add-favorite', help='Add an eepsite to the list of favorites')
    subparser.add_argument("-n", "--name", help="Name of the entry", required=True)
    subparser.add_argument("-u", "--url", help="URL of the entry", required=True)

    subparsers.required = True
    return parser.parse_args()


def subcommand_start(arguments):
    action_utils.service_start("i2p")


def subcommand_stop(arguments):
    action_utils.service_stop("i2p")


def subcommand_enable(arguments):
    action_utils.service_enable("i2p")
    action_utils.webserver_enable("i2p-plinth")


def subcommand_disable(arguments):
    action_utils.service_disable("i2p")
    action_utils.webserver_disable("i2p-plinth")


def subcommand_restart(arguments):
    action_utils.service_restart("i2p")


def subcommand_is_enabled(arguments):
    print(action_utils.service_is_enabled("i2p"))


def subcommand_is_running(arguments):
    print(action_utils.service_is_running("i2p"))


def subcommand_add_favorite(arguments):
    """
    Adds a favorite to the router.config

    :param arguments:
    :type arguments:
    """
    router_config_path = os.path.join(I2P_CONF_DIR, "router.config")
    # Read config
    with open(router_config_path) as config_file:
        config_lines = config_file.readlines()

    found_favorites = False
    treated = False
    url = arguments.url
    for i in range(len(config_lines)):
        line = config_lines[i]

        # Find favorites line
        if line.startswith("routerconsole.favorites"):
            found_favorites = True
            if url in line:
                print("URL already in favorites")
                exit(0)

            # Append favorite
            line = "%(line)s%(name)s,%(description)s,%(url)s,%(icon)s,\n" % {
                'line': line.strip(),
                'name': arguments.name,
                'description': "",
                'url': arguments.url,
                'icon': "/themes/console/images/eepsite.png"
            }
            config_lines[i] = line
            treated = True
            break

    if not found_favorites:
        print("No favorites line found")
        exit(1)
    if not treated:
        print("Nothing to do")
        exit(0)

    # Update config
    with open(router_config_path, mode='w') as config_file:
        config_file.writelines(config_lines)

    print("Added '%s' to favorites" % url)


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()
