#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@Contact :   liuyuqi.gov@msn.cn
@Time    :   2024/04/09 12:56:07
@License :   Copyright © 2017-2022 liuyuqi. All Rights Reserved.
@Desc    :   client enter point
"""

import os,sys,re
import argparse

class Client:
    help_str ="""
# clone repo
fgh clone https://github.com/xx/yy.git
fgh push https://github.com/xx/yy.git

# download file
fgh wget https://ghproxy.org/https://github.com/microsoft/vscode/archive/refs/tags/1.84.2.zip
fgh wget https://ghproxy.org/https://raw.githubusercontent.com/microsoft/vscode/main/README.md

fgh curl -O https://ghproxy.org/https://github.com/microsoft/vscode/archive/refs/tags/1.84.2.zip
fgh curl -O https://ghproxy.org/https://raw.githubusercontent.com/microsoft/vscode/main/README.md
"""

    def __init__(self):
        self.fgit_host = ''
        self.token = ''
        self.read_config()
        self.args = self.parse_args()

    def read_config(self):
        import dotenv
        dotenv.load_dotenv()
        fgit_host = os.getenv('FGH_HOST')
        try:
            if fgit_host[-1] == '/':
                self.fgit_host = fgit_host[:-1]
            self.token = os.getenv('FGh_TOKEN')
        except Exception as e:
            print(".env配置错误"+e)

    @staticmethod
    def parse_args():
        parser = argparse.ArgumentParser(description='fgit client')
        parser.add_argument('command', type=str, help='fgit command', 
                            choices=['git','wget','curl','clone', 'push', 'pull', 'commit', 'add', 'status', 'log', 'diff', 'branch', 'checkout', 'merge', 'rebase', 'reset', 'tag', 'fetch', 'remote', 'init', 'config', 'help'])
        # all args behind command
        parser.add_argument('args', nargs=argparse.REMAINDER, help='git command args')
        return parser.parse_args()

    def choose_host(self):
        """choose host"""
        pass

    def run(self):   
        args = self.parse_args()
        command = args.command
        if len(args.args) == 0 and command != 'help':
            print('Usage: fgh <command> [<args>]')
            sys.exit(1)
        if command == "wget":
            if len(args.args) > 0:
                args.args = [re.sub(r'https://github.com', self.fgit_host+'/https://github.com', arg) for arg in args.args]
                # print('wget ' + ' '.join(args.args))
                os.system('wget ' + ' '.join(args.args))
        elif command =="curl":
            if len(args.args) > 0:
                args.args = [re.sub(r'https://github.com', self.fgit_host+'/https://github.com', arg) for arg in args.args]
                print('curl ' + ' '.join(args.args))
                os.system('curl ' + ' '.join(args.args))
        elif command == "help":
            print(Client.help_str)
        else:
            if len(args.args) > 0:
                args.args = [re.sub(r'https://github.com', self.fgit_host+'/https://github.com', arg) for arg in args.args]
                cmd_str = 'git ' + command + ' ' + ' '.join(args.args)
                try:
                    if command == 'git':
                        cmd_str = command + ' ' + ' '.join(args.args)
                    os.system(cmd_str)
                    # print(cmd_str)
                except Exception as e:
                    print(cmd_str + e)

if __name__=='__main__':
    client = Client()
    client.run()