#!/usr/bin/python3

# ------------------------------------------------------------------
#  Copyright (C) 2024 Tianjin KYLIN Information Technology Co., Ltd.
#
#  Author: Fnoily <liuyang@kylinos.cn>
#          Wangdi <wangdi@kylinos.cn>
#          Wangweiran <wangweiran@kylinos.cn>
#          Wangqinghao <wangqinghao@kylinos.cn>
#          Wumuti <wumuti@kylinos.cn>
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published
#  by the Free Software Foundation; either version 3 of the License, or
#  (at your option) any later version.
#
#  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 Lesser General Public
#  License for more details.
#
#  You should have received a copy of the GNU Lesser General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ------------------------------------------------------------------
import argparse
import sys
import os
import getpass

for root, dirs, _ in os.walk("/usr/lib/python3/dist-packages"):
    for dir_name in dirs:
        if dir_name.startswith("ComplianceCheck"):
            sys.path.append(os.path.join(root, dir_name))
        if dir_name.startswith("ABICheck"):
            sys.path.append(os.path.join(root, dir_name))

from ComplianceCheck.Check import Check as c1
from ABICheck.launchpadCheck import LaunchpadCheck, ci_res


def parse_args():
    parser = argparse.ArgumentParser(description='Check if a package can run on a specific OS version')
    parser.add_argument('-p', '--package', type=str, required=False, help='Package name')
    parser.add_argument('-os-runable', type=str, required=False, choices=['uos-1060','uos-1050','uos-1042','uos-1040','kylin-v4', 'kylin-v10', 'kylin-v10-sp1','kylin-v10-sp1-wayland'], help='OS version where the package can run')
    parser.add_argument('-os-to-check', type=str, required=False, choices=['kylin-v4', 'kylin-v10', 'kylin-v10-sp1','kylin-v10-sp1-wayland','kylin-v10-sp1-hwe', 'kylin-v11'], help='OS version to check if the package can run')
    parser.add_argument('-l', "--level", help=argparse.SUPPRESS, type=str, required=False, choices=['debug', 'info'])
    parser.add_argument('-arch-to-check', help=argparse.SUPPRESS, type=str, required=False, choices=['amd64', 'arm64'])
    parser.add_argument("--java-jdk", type=int, required=False,help="Java version to migrate to (between 9 and 17).")
    parser.add_argument("-S", action='store_true', required=False)

    parser.add_argument("-abi-check",action='store_true', required=False ,help='src-abi-check')
    parser.add_argument("-old-version", type=str, required=False,help='src-abi-check-new-version')
    parser.add_argument("-new-version", type=str, required=False,help='src-abi-check-old-version')
    parser.add_argument("-deb", type=str, required=False, help='deb包ABI兼容检测')
    parser.add_argument("-prnumber", type=str, required=False, help='deb包ABI兼容检测参数')
    parser.add_argument("-revisionid", type=str, required=False, help='deb包ABI兼容检测参数')
    parser.add_argument("-kpct-path", type=str, required=False,help='kpct path for cibot')

    parser.add_argument("-os-check",action='store_true', required=False ,help='os-check')
    parser.add_argument("-os-list", action='store_true', required=False, help='os-check')
    parser.add_argument("-old-os-version", type=str, required=False,help='os-new-version')
    parser.add_argument("-new-os-version", type=str, required=False,help='os-old-version')
    return parser.parse_args()

def main():
    # Parse command line arguments
    args = parse_args()

    if not args.level:
        args.level = "info"
        print("===== log Level: info")

    if args.deb:
        print("===== kylin-deb abi check v1.0")
        lc = LaunchpadCheck(args.deb)
        if not lc.init_env():
            # 无对外接口
            with open(lc.ci_report_file, 'w') as f:
                f.write(ci_res.format(-1,-1,-1,-1,'pass'))
            return
        lc.check()


    else:
        print("kylin-src abi check v1.0")
        if args.S:
            sudo_password=sys.stdin.read()
        else:
            sudo_password = getpass.getpass('Enter your password: ')
        kcc = c1(args.level, args.package, args.os_runable, args.os_to_check,args.arch_to_check,sudo_password)
        kcc.get_deb_info()
        kcc.file_check()
        kcc.depend_is_exist()
        res = 0
        if 'java' in kcc.deb_type:
            if not args.java_jdk:
                print("输入目标jdk版本：(直接输入9-17数字)")
                java_jdk = eval(input())
                # print(java_jdk)
                if isinstance(java_jdk,int):
                    args.java_jdk = java_jdk
                else:
                    print("输入错误")
                    exit(0)
            kcc.java_check(args.java_jdk)
        else:
            kcc.sym_check()
        kcc.get_report()


if __name__ == '__main__':
    main()
