#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""TcEx Library Builder."""
import argparse
import sys
import traceback

import colorama as c
from tcex.bin import Lib

# temp workaround for proxy arg issue
for n, i in enumerate(sys.argv):
    if i == 'proxy_host':
        sys.argv[n] = '--proxy_host'
    elif i == 'proxy_port':
        sys.argv[n] = '--proxy_port'
    elif i == 'proxy_user':
        sys.argv[n] = '--proxy_user'
    elif i == 'proxy_pass':
        sys.argv[n] = '--proxy_pass'

# autoreset colorama
c.init(autoreset=True, strip=False)

parser = argparse.ArgumentParser()
parser.add_argument('--app_name', help='(Advanced) Fully qualified name of App.')
parser.add_argument('--app_path', help='(Advanced) Fully qualified path of App.')
parser.add_argument('--no_cache_dir', action='store_true', help='Do not use pip cache directory.')
parser.add_argument(
    '--branch', help='Build tcex from specified git branch instead of downloading from PyPi.'
)
parser.add_argument('--proxy_host', help='(Advanced) Hostname of the proxy.', default=None)
parser.add_argument('--proxy_port', help='(Advanced) Port of the proxy.', default=None)
parser.add_argument('--proxy_user', help='(Advanced) Username for the proxy.', default=None)
parser.add_argument('--proxy_pass', help='(Advanced) Password for the proxy.', default=None)
args, extra_args = parser.parse_known_args()


if __name__ == '__main__':
    try:
        tcl = Lib(args)
        tcl.install_libs()
        sys.exit(tcl.exit_code)
    except Exception:
        # TODO: Update this, possibly raise
        print(f'{c.Style.BRIGHT}{c.Fore.RED}{traceback.format_exc()}')
        sys.exit(1)
