#!/usr/bin/python3
#
# 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 searx.
"""

import argparse
import os
import secrets
import shutil

import yaml

from plinth import action_utils
from plinth.utils import gunzip

SETTINGS_FILE = '/etc/searx/settings.yml'


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('enable', help='Enable Searx')
    subparsers.add_parser('disable', help='Disable Searx')
    subparsers.add_parser(
        'setup', help='Perform post-installation operations for Searx')

    safe_search = subparsers.add_parser(
        'set-safe-search',
        help='Set the default filter for safe search on Searx')
    safe_search.add_argument(
        'filter', type=int,
        help='Filter results. 0: None, 1: Moderate, 2: Strict')

    subparsers.add_parser('get-safe-search',
                          help='Print the value of the safe search setting.')

    subparsers.required = True
    return parser.parse_args()


def _copy_uwsgi_configuration():
    """Copy example uwsgi configuration

    Copy the example uwsgi configuration shipped with Searx documentation to
    the appropriate uwsgi directory.
    """
    example_config = ('/usr/share/doc/searx/examples/'
                      'uwsgi/apps-available/searx.ini')
    destination = '/etc/uwsgi/apps-available/'

    if not os.path.exists(os.path.join(destination, 'searx.ini')):
        shutil.copy(example_config, destination)


def _generate_secret_key(settings):
    """Generate a secret key for the Searx installation."""
    secret_key = secrets.token_hex(64)
    settings['server']['secret_key'] = secret_key


def _set_title(settings):
    """Set the page title to '{box_name} Web Search'."""
    title = 'FreedomBox Web Search'
    settings['general']['instance_name'] = title


def _set_timeout(settings):
    """Set timeout to 20 seconds."""
    settings['outgoing']['request_timeout'] = 20.0


def subcommand_set_safe_search(arguments):
    """Set safe search filter for search results."""
    value = arguments.filter
    settings = read_settings()
    settings['search']['safe_search'] = value
    write_settings(settings)


def subcommand_get_safe_search(_):
    """Print the value of the safe search setting."""
    if os.path.exists(SETTINGS_FILE):
        settings = read_settings()
        print(settings['search']['safe_search'])
    else:
        print(0)


def read_settings():
    """Load settings as dictionary from YAML config file."""
    with open(SETTINGS_FILE, 'rb') as settings_file:
        return yaml.load(settings_file)


def write_settings(settings):
    """Write settings from dictionary to YAML config file."""
    with open(SETTINGS_FILE, 'w') as settings_file:
        yaml.dump(settings, settings_file)


def subcommand_setup(_):
    """Post installation actions for Searx"""
    _copy_uwsgi_configuration()
    action_utils.webserver_enable('proxy_uwsgi', kind='module')

    if not os.path.exists(SETTINGS_FILE):
        example_settings_file = '/usr/share/doc/searx/examples/settings.yml.gz'
        gunzip(example_settings_file, SETTINGS_FILE)

    settings = read_settings()
    _generate_secret_key(settings)
    _set_title(settings)
    _set_timeout(settings)
    write_settings(settings)

    action_utils.service_restart('uwsgi')


def subcommand_enable(_):
    """Enable web configuration and reload."""
    if not os.path.exists('/etc/uwsgi/apps-enabled/searx.ini'):
        os.symlink('/etc/uwsgi/apps-available/searx.ini',
                   '/etc/uwsgi/apps-enabled/searx.ini')

    action_utils.service_enable('uwsgi')
    action_utils.service_restart('uwsgi')
    action_utils.webserver_enable('searx-freedombox')


def subcommand_disable(_):
    """Disable web configuration and reload."""
    action_utils.webserver_disable('searx-freedombox')
    os.unlink('/etc/uwsgi/apps-enabled/searx.ini')
    action_utils.service_restart('uwsgi')


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