#!/usr/bin/python3
'''check-show-files: show files'''
#
# Copyright (C) 2014 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 3 of the License.
#
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import print_function
import os
import sys

from clickreviews import cr_common
from clickreviews import cr_desktop
from clickreviews import cr_lint
from clickreviews import cr_security
from clickreviews import cr_url_dispatcher
from clickreviews import cr_scope
from clickreviews import cr_content_hub
from clickreviews import cr_online_accounts
from clickreviews import cr_push_helper
from clickreviews import cr_bin_path
from clickreviews import cr_framework
from clickreviews import cr_systemd

# This script just dumps important files to stdout

if __name__ == "__main__":
    if len(sys.argv) < 2:
        cr_common.error("Must give path to click package")

    review = cr_lint.ClickReviewLint(sys.argv[1])

    for i in sorted(review.control_files):
        fh = cr_common.open_file_read(review.control_files[i])
        print("= %s =" % os.path.basename(i))
        for line in fh.readlines():
            print(line, end="")
        fh.close()
        print("")

    print("= hooks =")

    review_content_hub = cr_content_hub.ClickReviewContentHub(sys.argv[1])
    for app in sorted(review_content_hub.content_hub_files):
        f = review_content_hub.content_hub_files[app]
        fh = cr_common.open_file_read(os.path.join(
            review_content_hub.unpack_dir, f))
        print("== content_hub: %s ==" % os.path.basename(f))
        for line in fh.readlines():
            print(line, end="")
        fh.close()
        print("")

    review_desktop = cr_desktop.ClickReviewDesktop(sys.argv[1])
    for app in sorted(review_desktop.desktop_files):
        f = review_desktop.desktop_files[app]
        fh = cr_common.open_file_read(os.path.join(review_desktop.unpack_dir,
                                                   f))
        print("== desktop: %s ==" % os.path.basename(f))
        for line in fh.readlines():
            print(line, end="")
        fh.close()
        print("")

    review_accounts = cr_online_accounts.ClickReviewAccounts(sys.argv[1])
    for app in sorted(review_accounts.accounts_files):
        for account_type in review_accounts.account_hooks:
            if account_type not in review_accounts.accounts_files[app]:
                continue
            f = review_accounts.accounts_files[app][account_type]
            fh = cr_common.open_file_read(os.path.join(
                review_accounts.unpack_dir, f))
            print("== online %s: %s ==" % (account_type, os.path.basename(f)))
            for line in fh.readlines():
                print(line, end="")
            fh.close()
            print("")

    review_push_helper = cr_push_helper.ClickReviewPushHelper(sys.argv[1])
    for app in sorted(review_push_helper.push_helper_files):
        f = review_push_helper.push_helper_files[app]
        fh = cr_common.open_file_read(os.path.join(
            review_push_helper.unpack_dir, f))
        print("== push_helper: %s ==" % os.path.basename(f))
        for line in fh.readlines():
            print(line, end="")
        fh.close()
        print("")

    review_scope = cr_scope.ClickReviewScope(sys.argv[1])
    for app in sorted(review_scope.scopes):
        f = review_scope.scopes[app]["ini_file"]
        fh = cr_common.open_file_read(os.path.join(review_scope.unpack_dir, f))
        print("== scope .INI: %s ==" % os.path.basename(f))
        for line in fh.readlines():
            print(line, end="")
        fh.close()
        print("")

    review_framework = cr_framework.ClickReviewFramework(sys.argv[1])
    for app in sorted(review_framework.frameworks_file):
        f = os.path.join(review_framework.unpack_dir,
                         review_framework.frameworks_file[app])
        fh = cr_common.open_file_read(os.path.join(review_framework.unpack_dir, f))
        print("== click .framework: %s ==" % os.path.basename(f))
        for line in fh.readlines():
            print(line, end="")
        fh.close()
        print("")

    review_bin_path = cr_bin_path.ClickReviewBinPath(sys.argv[1])
    for app in sorted(review_bin_path.bin_paths):
        f = os.path.join(review_bin_path.unpack_dir, review_bin_path.bin_paths[app])
        print("== bin_path: %s ==" % os.path.relpath(f, review_bin_path.unpack_dir))
        print("")

    review_apparmor = cr_security.ClickReviewSecurity(sys.argv[1])
    for f in sorted(review_apparmor.security_manifests):
        fh = cr_common.open_file_read(os.path.join(review_apparmor.unpack_dir,
                                                   f))
        print("== security: %s ==" % os.path.basename(f))
        for line in fh.readlines():
            print(line, end="")
        fh.close()
        print("")

    review_systemd = cr_systemd.ClickReviewSystemd(sys.argv[1])
    for app in sorted(review_systemd.systemd_files):
        f = review_systemd.systemd_files[app]
        fh = cr_common.open_file_read(os.path.join(review_systemd.unpack_dir,
                                                   f))
        print("== systemd: %s ==" % os.path.basename(f))
        for line in fh.readlines():
            print(line, end="")
        fh.close()
        print("")

    review_url_dispatcher = cr_url_dispatcher.ClickReviewUrlDispatcher(sys.argv[1])
    for app in sorted(review_url_dispatcher.url_dispatcher_files):
        f = review_url_dispatcher.url_dispatcher_files[app]
        fh = cr_common.open_file_read(os.path.join(review_url_dispatcher.unpack_dir,
                                                   f))
        print("== url_dispatcher: %s ==" % os.path.basename(f))
        for line in fh.readlines():
            print(line, end="")
        fh.close()
        print("")

    # Cleanup our unpack directory
    cr_common.cleanup_unpack()
