#!/usr/bin/env python3
"""LICENSE
Copyright 2017 Hermann Krumrey <hermann@krumreyh.com>

This file is part of ci-scripts.

ci-scripts 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, either version 3 of the License, or
(at your option) any later version.

ci-scripts 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 ci-scripts.  If not, see <http://www.gnu.org/licenses/>.
LICENSE"""

import os
import sys
import shutil
import argparse
from ci_scripts.npm import install_package
from ci_scripts.common import process_call


def main():
    """
    Builds a webextension package
    :return: None
    """

    parser = argparse.ArgumentParser()
    parser.add_argument("--signed", action="store_true",
                        help="Specifies whether or not to "
                             "create a signed build")
    parser.add_argument("--publish", action="store_true",
                        help="Specifies wether or not to publish")

    args = parser.parse_args()
    signed = args.signed
    publish = args.publish

    if publish and not signed:
        print("Can't publish unsigned package")
        sys.exit(1)

    install_package("web-ext")
    web_ext = os.path.join("node_modules", ".bin", "web-ext")

    if os.path.isdir("web-ext-artifacts"):
        shutil.rmtree("web-ext-artifacts")

    if signed:
        command = [web_ext, "sign",
                   "--api-key", os.environ["JWT_ISSUER"],
                   "--api-secret", os.environ["JWT_SECRET"],
                   "-i", "virtual"]

        if publish:
            command += ["--id", os.environ["AMO_ID"]]

        process_call(command, publish)  # Ignore error during publish

    else:
        process_call([web_ext, "build", "-i", "virtual"])

    if not os.path.isdir("artifacts"):
        os.makedirs("artifacts")

    for child in os.listdir("web-ext-artifacts"):
        child_path = os.path.join("web-ext-artifacts", child)
        dest_path = os.path.join("artifacts", child)
        os.rename(child_path, dest_path)


if __name__ == "__main__":
    main()
