#!/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 handle package installation with apt-get.
"""

import argparse
import os
import subprocess
import sys
from importlib import import_module

from plinth import cfg

LOCK_FILE = '/var/lib/dpkg/lock'


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('update', help='update the package lists')

    subparser = subparsers.add_parser('install', help='install packages')
    subparser.add_argument(
        '--skip-recommends', action='store_true',
        help='whether to skip installing recommended packages')
    subparser.add_argument(
        '--force-configuration', choices=['new', 'old'],
        help='force old/new configuration files during install')
    subparser.add_argument(
        'module', help='name of module for which package is being installed')
    subparser.add_argument('packages', nargs='+',
                           help='list of packages to install')
    subparsers.add_parser('is-package-manager-busy',
                          help='Return whether package manager is busy')

    subparsers.required = True
    return parser.parse_args()


def _run_apt_command(arguments):
    """Run apt-get with provided arguments."""
    # Ask apt-get to output its progress to file descriptor 3.
    command = [
        'apt-get', '--assume-yes', '--quiet=2', '--option', 'APT::Status-Fd=3'
    ] + arguments

    # Duplicate stdout to file descriptor 3 for this process.
    os.dup2(1, 3)

    # Pass on file descriptor 3 instead of closing it.  Close stdout
    # so that regular output is ignored.
    env = os.environ.copy()
    env['DEBIAN_FRONTEND'] = 'noninteractive'
    process = subprocess.run(command, stdin=subprocess.DEVNULL,
                             stdout=subprocess.DEVNULL, close_fds=False,
                             env=env)
    sys.exit(process.returncode)


def subcommand_update(arguments):
    """Update apt package lists."""
    _run_apt_command(['update'])


def subcommand_install(arguments):
    """Install packages using apt-get."""
    try:
        _assert_managed_packages(arguments.module, arguments.packages)
    except Exception as exception:
        print('Access check failed:', exception, file=sys.stderr)
        sys.exit(99)

    extra_arguments = []
    if arguments.skip_recommends:
        extra_arguments.append('--no-install-recommends')

    if arguments.force_configuration == 'old':
        extra_arguments += [
            '-o', 'Dpkg::Options::=--force-confdef', '-o',
            'Dpkg::Options::=--force-confold'
        ]
    elif arguments.force_configuration == 'new':
        extra_arguments += ['-o', 'Dpkg::Options::=--force-confnew']

    _run_apt_command(['install'] + extra_arguments + arguments.packages)


def _assert_managed_packages(module, packages):
    """Check that list of packages are in fact managed by module."""
    cfg.read()
    module_file = os.path.join(cfg.config_dir, 'modules-enabled', module)

    with open(module_file, 'r') as file_handle:
        module_path = file_handle.read().strip()

    module = import_module(module_path)
    for package in packages:
        assert package in module.managed_packages


def subcommand_is_package_manager_busy(_):
    """Return whether package manager is busy.
    This command uses the `lsof` command to check whether the dpkg lock file
    is open which indicates that the package manager is busy"""
    try:
        subprocess.check_output(['lsof', LOCK_FILE])
    except subprocess.CalledProcessError:
        sys.exit(-1)


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