#!/usr/bin/python3

from __future__ import print_function

import io
import os
import re
import sys
import gettext
import locale
import argparse
import subprocess

from softwareproperties.shortcuthandler import ShortcutException
from softwareproperties.shortcuts import shortcut_handler
from softwareproperties.ppa import PPAShortcutHandler
from softwareproperties.cloudarchive import CloudArchiveShortcutHandler
from softwareproperties.sourceslist import SourcesListShortcutHandler
from softwareproperties.uri import URIShortcutHandler

from aptsources.distro import get_distro

from softwareproperties.extendedsourceslist import (SourceEntry,
                                                    SourcesList,
                                                    CollapsedSourcesList)

from gettext import gettext as _


class AddAptRepository(object):
    def __init__(self):
        gettext.textdomain("software-properties")
        self.distro = get_distro()
        self.sourceslist = SourcesList()
        self.distro.get_sources(self.sourceslist)

    def parse_args(self, args):
        description = "Only ONE of -P, -C, -U, -S, or old-style 'line' can be specified"

        parser = argparse.ArgumentParser(description=description)
        parser.add_argument("-d", "--debug", action="store_true",
                            help=_("Print debug"))
        parser.add_argument("-r", "--remove", action="store_true",
                            help=_("Disable repository"))
        parser.add_argument("-s", "--enable-source", action="count", default=0,
                            help=_("Allow downloading of the source packages from the repository"))
        parser.add_argument("-c", "--component", action="append", default=[],
                            help=_("Components to use with the repository"))
        parser.add_argument("-p", "--pocket",
                            help=_("Add entry for this pocket"))
        parser.add_argument("-y", "--yes", action="store_true",
                            help=_("Assume yes to all queries"))
        parser.add_argument("-n", "--no-update", dest="update", action="store_false",
                            help=_("Do not update package cache after adding"))
        parser.add_argument("-u", "--update", action="store_true", default=True,
                            help=argparse.SUPPRESS)
        parser.add_argument("-l", "--login", action="store_true",
                            help=_("Login to Launchpad."))
        parser.add_argument("--dry-run", action="store_true",
                            help=_("Don't actually make any changes."))

        group = parser.add_mutually_exclusive_group()
        group.add_argument("-L", "--list", action="store_true",
                           help=_("List currently configured repositories"))
        group.add_argument("-P", "--ppa",
                           help=_("PPA to add"))
        group.add_argument("-C", "--cloud",
                           help=_("Cloud Archive to add"))
        group.add_argument("-U", "--uri",
                           help=_("Archive URI to add"))
        group.add_argument("-S", "--sourceslist", nargs='+', default=[],
                           help=_("Full sources.list entry line to add"))
        group.add_argument("line", nargs='*', default=[],
                           help=_("sources.list line to add (deprecated)"))

        self.parser = parser
        self.options = self.parser.parse_args(args)

    @property
    def dry_run(self):
        return self.options.dry_run

    @property
    def enable_source(self):
        return self.options.enable_source > 0

    @property
    def components(self):
        return self.options.component

    @property
    def pocket(self):
        return self.options.pocket

    @property
    def source_type(self):
        return self.distro.source_type

    @property
    def binary_type(self):
        return self.distro.binary_type

    def is_components(self, comps):
        if not comps:
            return False
        return set(comps.split()) <= set([comp.name for comp in self.distro.source_template.components])

    def apt_update(self):
        if self.options.update and not self.dry_run:
            # We prefer to run apt-get update here. The built-in update support
            # does not have any progress, and only works for shortcuts. Moving
            # it to something like save() and using apt.progress.text would
            # solve the problem, but the new errors might cause problems with
            # the dbus server or other users of the API. Also, it's unclear
            # how good the text progress is or how to pass it best.
            subprocess.run(['apt-get', 'update'])

    def prompt_user(self):
        if self.dry_run:
            print(_("DRY-RUN mode: no modifications will be made"))
            return
        if not self.options.yes and sys.stdin.isatty() and not "FORCE_ADD_APT_REPOSITORY" in os.environ:
            try:
                input(_("Press [ENTER] to continue or Ctrl-c to cancel."))
            except KeyboardInterrupt:
                print(_("Aborted."))
                sys.exit(1)

    def prompt_user_shortcut(self, shortcut):
        '''Display more information about the shortcut / ppa info'''
        print(_("Repository: '%s'") % shortcut.SourceEntry().line)
        if shortcut.description:
            # strip ANSI escape sequences
            description = re.sub(r"(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]",
                                 "", shortcut.description)
            print(_("Description:"))
            print(description)
        if shortcut.web_link:
            print(_("More info: %s") % shortcut.web_link)
        if self.options.remove:
            print(_("Removing repository."))
        else:
            print(_("Adding repository."))
        self.prompt_user()

    def change_components(self):
        for c in self.components:
            if self.options.remove:
                self.distro.disable_component(c)
                print(_("Removed component %s") % c)
            else:
                self.distro.enable_component(c)
                print(_("Added component %s") % c)
        if not self.dry_run:
            self.sourceslist.save()

    def _add_pocket(self):
        l = self.sourceslist
        # ignore invalid or disabled entries
        l = filter(lambda e: not e.invalid, list(l))
        l = filter(lambda e: not e.disabled, list(l))
        # add new pocket ONLY for templated sources
        l = filter(lambda e: not e.template, list(l))
        # add new pocket ONLY for sources currently using -release
        codename = self.distro.codename
        releasepocket = [codename, '%s-release' % codename]
        l = filter(lambda e: e.dist in releasepocket, list(l))

        collapsedlist = CollapsedSourcesList(self.sourceslist)
        for e in list(l):
            new_dist = '%s-%s' % (codename, self.pocket)
            new_entry = e._replace(dist=new_dist)
            if collapsedlist.has_entry(new_entry):
                print(_("Existing: %s") % str(new_entry).strip())
            else:
                new_entry = collapsedlist.add_entry(new_entry, after=e)
                print(_("Adding: %s") % str(new_entry).strip())

    def _remove_pocket(self):
        dist = '%s-%s' % (self.distro.codename, self.pocket)
        for s in [s for s in self.sourceslist if not s.invalid and not s.disabled]:
            if s.dist == dist:
                s.set_enabled(False)
                print(_("Disabled: %s") % str(s).strip())

    def change_pocket(self):
        if self.options.remove:
            self._remove_pocket()
        else:
            self._add_pocket()
        if not self.dry_run:
            self.sourceslist.save()

    def _enable_source(self):
        collapsedlist = CollapsedSourcesList(self.sourceslist)
        for s in [s for s in collapsedlist if
                  not s.invalid and s.disabled and s.type == self.source_type]:
            entry = collapsedlist.add_entry(s._replace(disabled=False))
            print(_("Enabled: %s") % str(entry).strip())

        if self.options.enable_source > 1:
            collapsedlist.refresh()
            # if -s passed twice, add new deb-src entries if needed for all deb entries
            for s in [s for s in collapsedlist if
                      not s.invalid and not s.disabled and s.type == self.binary_type]:
                entry = s._replace(type=self.source_type)
                if collapsedlist.has_entry(entry):
                    print(_("Existing: %s") % str(entry).strip())
                else:
                    collapsedlist.add_entry(entry, after=s)
                    print(_("Added: %s") % str(entry).strip())

    def _disable_source(self):
        for s in [s for s in self.sourceslist if not s.invalid and not s.disabled]:
            if s.type == self.source_type:
                s.set_enabled(False)
                print(_("Disabled: %s") % str(s).strip())

    def change_source(self):
        if self.options.remove:
            self._disable_source()
        else:
            self._enable_source()
        if not self.dry_run:
            self.sourceslist.save()

    def global_change(self):
        if self.components:
            if self.options.remove:
                print(_("Removing component(s) '%s' from all repositories.") % ', '.join(self.components))
            else:
                print(_("Adding component(s) '%s' to all repositories.") % ', '.join(self.components))
        if self.pocket:
            if self.options.remove:
                print(_("Removing pocket %s for all repositories.") % self.pocket)
            else:
                print(_("Adding pocket %s for all repositories.") % self.pocket)
        if self.enable_source:
            if self.options.remove:
                print(_("Disabling %s for all repositories.") % self.source_type)
            else:
                print(_("Enabling %s for all repositories.") % self.source_type)
        self.prompt_user()
        if self.components:
            self.change_components()
        if self.pocket:
            self.change_pocket()
        if self.enable_source:
            self.change_source()

    def show_list(self):
        for s in CollapsedSourcesList(self.sourceslist):
            if s.invalid or s.disabled:
                continue
            if not self.enable_source and s.type == self.source_type:
                continue
            print(s)

    def main(self, args=sys.argv[1:]):
        self.parse_args(args)

        if not any((self.dry_run, self.options.list, os.geteuid() == 0)):
            print(_("Error: must run as root"))
            return False

        line = ' '.join(self.options.line)
        if line == '-':
            line = sys.stdin.readline().strip()

        # if 'line' is only (valid) components, handle as if only -c was used with no line
        if self.is_components(line):
            self.options.component += line.split()
            line = ''

        if self.options.ppa:
            source = self.options.ppa
            if not ':' in source:
                source = 'ppa:' + source
            handler = PPAShortcutHandler
        elif self.options.cloud:
            source = self.options.cloud
            if not ':' in source:
                source = 'uca:' + source
            handler = CloudArchiveShortcutHandler
        elif self.options.uri:
            source = self.options.uri
            handler = URIShortcutHandler
        elif self.options.sourceslist:
            source = ' '.join(self.options.sourceslist)
            handler = SourcesListShortcutHandler
        elif line:
            source = line
            handler = shortcut_handler
        elif self.options.list:
            self.show_list()
            return True
        elif any((self.enable_source, self.components, self.pocket)):
            self.global_change()
            self.apt_update()
            return True
        else:
            print(_("Error: no actions requested."))
            self.parser.print_help()
            return False

        try:
            shortcut_params = {
                'login': self.options.login,
                'enable_source': self.enable_source,
                'dry_run': self.dry_run,
                'components': self.components,
                'pocket': self.pocket,
            }
            shortcut = handler(source, **shortcut_params)
        except ShortcutException as e:
            print(e)
            return False

        self.prompt_user_shortcut(shortcut)

        if self.options.remove:
            shortcut.remove()
        else:
            shortcut.add()

        self.apt_update()
        return True

if __name__ == '__main__':
    addaptrepo = AddAptRepository()
    sys.exit(0 if addaptrepo.main() else 1)
