#!/usr/bin/env python3
import argparse
import os
from subprocess import check_call
from pathlib import Path

# right, specifying id in manifest doesn't seem to work
# AMO responds with: Server response: Duplicate add-on ID found. (status: 400)
IDS = {
    'firefox'       : '{07c6b8e1-94f7-4bbf-8e91-26c0a8992ab5}',
    'chrome'        : 'kdmegllpofldcpaclldkopnnjjljoiio',
}


TARGETS = [
    'chrome',
    'firefox',
]

def main():
    p = argparse.ArgumentParser()
    p.add_argument('--release', action='store_true', help="Use release flavor of build")
    p.add_argument('--lint'   , action='store_true')
    p.add_argument('--publish', action='store_true', help="Publish on chrome web store/addons.mozilla.org")

    tg = p.add_mutually_exclusive_group(required=True)
    tg.add_argument('--target', type=str, choices=TARGETS)
    for b in TARGETS:
        tg.add_argument('--' + b, action='store_const', const=b, dest='target')
    args = p.parse_args()
    target = args.target

    assert target is not None

    dist_dir = Path(__file__).absolute().parent / 'dist'
    src_dir = (dist_dir / target).resolve() # webext can't into symlinks
    def webext(*args, **kwargs):
        check_call([
            # TODO use npm run web-ext??
            'npm', 'run', 'web-ext',
            '--',
            '--source-dir'   , src_dir,
            '--artifacts-dir', src_dir / 'web-ext-artifacts',
            *args,
        ], **kwargs)

    env = {
        'TARGET': target,
        'RELEASE': 'YES' if args.release else 'NO',
        'PUBLISH': 'YES' if args.publish else 'NO',
        **os.environ,
    }
    check_call([
        'npm', 'run', 'build',
    ], env=env, cwd=str(Path(__file__).absolute().parent))

    if args.lint:
        # TODO --self-hosted
        # TODO warnings as errors??
        webext('lint')

        # TODO move somewhere more appropriate..
        webext('build')


    if args.release:
        assert args.lint # TODO not sure..

    def webext_args():
        from firefox_dev_secrets import API_KEY, API_SECRET
        return [
            '--artifacts-dir', str(dist_dir),
            '--api-key'      , API_KEY,
            '--api-secret'   , API_SECRET,
            '--id'           , IDS[target],
        ]

    if args.publish:
        assert args.release
        if 'firefox' in target:
            check_call([
                'npm', 'run', 'release:amo',
                '--',
                '--channel', 'listed',
                '--source-dir', str(src_dir),
                *webext_args(),
            ])
        elif target == 'chrome':
            from chrome_dev_secrets import CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN
            check_call([
                'npm', 'run', 'release:cws',
                '--',
                'upload',
                # '--auto-publish',
                '--source'        , str(src_dir),
                '--client-id'     , CLIENT_ID,
                '--client-secret' , CLIENT_SECRET,
                '--refresh--token', REFRESH_TOKEN,
                '--extension-id'  , IDS[target],
                # TODO trusted testers?
            ])
        else:
            raise RuntimeError("{target} is not supported for publishing yet".format(target=target))

if __name__ == '__main__':
    main()
