#!/usr/bin/env python3
import os
import sys
import subprocess
import argparse

guru_home = os.path.expanduser('~/.guru')

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Process some integers.")
    parser.add_argument("--version", type=str, help="version")
    parser.add_argument("--android_sdk", type=str, help="android sdk path")

    args = parser.parse_args()
    version = args.version
    android_sdk = args.android_sdk
    target_dir = f"guru_lib-{version}"
    guru_lib_dir = f'{guru_home}/{target_dir}'
    print(guru_home, guru_lib_dir)
    current_path = os.getcwd()
    if (os.path.exists(guru_lib_dir) != True):
        print("setup guru_lib...")
        if (os.path.exists(guru_home) != True):
            os.makedirs(guru_home)
        os.chdir(guru_home)
        os.system(f"git clone --depth 1 --branch {version} git@git.chengdu.pundit.company:castbox/guru_lib.git guru_lib-{version}")
        os.chdir(current_path)
    else:
        os.chdir(guru_lib_dir)
        os.system("git pull --rebase")
        os.chdir(current_path)

    os.environ['ANDROID_SDK_ROOT'] = android_sdk

    print("GuruConsent-Android")
    consentDir = f'{guru_lib_dir}/GuruConsent-Android'
    os.chdir(consentDir)
    os.system("./gradlew publish")

    print("guru_analytics")
    analyticsDir = f'{guru_lib_dir}/guru_analytics'
    os.chdir(analyticsDir)
    os.system("./gradlew publish")

    print("Guru Checker")
    checkerDir = f'{guru_lib_dir}/guru_checker'
    os.chdir(checkerDir)
    os.system("./gradlew publish")

