# Path: mod/log.py
# RED = '\033[1;31m'
# GREEN = '\033[1;32m'
# YELLOW = '\033[1;33m'
# BLUE = '\033[1;36m'
# DEF = '\033[0;0m'
# def error(msg, fatal=True) :
# def warn(msg) :
# def ok(item, status) :
# def failed(item, status) :
# def optional(item, status) :
# def info(msg) :
# def colored(color, msg) :
# 
# Path: mod/util.py
# def fix_path(path) :
# def get_workspace_dir(fips_dir) :
# def get_project_dir(fips_dir, proj_name) :
# def get_build_root_dir(fips_dir, proj_name):
# def get_deploy_root_dir(fips_dir, proj_name):
# def get_build_dir(fips_dir, proj_name, cfg) :
# def get_deploy_dir(fips_dir, proj_name, cfg) :
# def get_fips_dir(proj_dir, name):
# def get_configs_dir(proj_dir):
# def get_verbs_dir(proj_dir):
# def get_generators_dir(proj_dir):
# def get_toolchains_dir(proj_dir):
# def get_giturl_from_url(url) :
# def get_gitbranch_from_url(url) :
# def get_project_name_from_url(url) :
# def get_project_name_from_dir(proj_dir) :
# def load_fips_yml(proj_dir) :
# def lookup_target_cwd(proj_dir, target) :
# def is_valid_project_dir(proj_dir) :
# def ensure_valid_project_dir(proj_dir) :
# def is_git_url(url) :
# def confirm(question) :
# def url_download_hook(count, block_size, total_size) :
# def get_host_platform() :
# def get_cfg_target_list(fips_dir, proj_dir, cfg):
# def get_cfg_headersdirs_by_target(fips_dir, proj_dir, cfg):
# def get_cfg_defines_by_target(fips_dir, proj_dir, cfg):
# def get_num_cpucores():
# 
# Path: mod/tools/java.py
# def check_exists(fips_dir) :
# 
# Path: mod/tools/javac.py
# def check_exists(fips_dir) :


import os
import sys
import zipfile
import subprocess
import hashlib
    from urllib.request import urlretrieve
    from urllib import urlretrieve
from mod import log, util
from mod.tools import java, javac

#-------------------------------------------------------------------------------
def setup(fips_dir) :
    """setup the Android SDK and NDK"""
    log.colored(log.YELLOW, '=== setup Android SDK/NDK :')

    # first make sure that java is present, otherwise the Android
    # SDK setup will finish without errors, but is not actually usable
    if not java.check_exists(fips_dir) or not javac.check_exists(fips_dir) :
        log.error("please install Java JDK 8 (see './fips diag tools')")
    ensure_sdk_dirs(fips_dir)

    # download the command line tools archive
    tools_archive_path = get_tools_archive_path(fips_dir)
    tools_url = get_tools_url()
    log.info("downloading '{}'...".format(tools_url))
    urlretrieve(tools_url, tools_archive_path, util.url_download_hook)
    log.info("\nunpacking '{}'...".format(tools_archive_path))
    uncompress(fips_dir, tools_archive_path)

    # install the required SDK components through sdkmanager
    install_package(fips_dir, '"platforms;android-28"')
    install_package(fips_dir, '"build-tools;29.0.3"')
    install_package(fips_dir, 'platform-tools')
    install_package(fips_dir, 'ndk-bundle')

    # check for potentially breaking changes in build setup
    fips_cmake = fips_dir + '/cmake-toolchains/android.toolchain.orig'
    ndk_cmake = get_sdk_dir(fips_dir) + '/ndk-bundle/build/cmake/android.toolchain.cmake'
    if compute_sha256(ndk_cmake, strip_whitespace) != compute_sha256(fips_cmake, strip_whitespace) :

# Path: mod/log.py
# RED = '\033[1;31m'
# GREEN = '\033[1;32m'
# YELLOW = '\033[1;33m'
# BLUE = '\033[1;36m'
# DEF = '\033[0;0m'
# def error(msg, fatal=True) :
# def warn(msg) :
# def ok(item, status) :
# def failed(item, status) :
# def optional(item, status) :
# def info(msg) :
# def colored(color, msg) :
# 
# Path: mod/util.py
# def fix_path(path) :
# def get_workspace_dir(fips_dir) :
# def get_project_dir(fips_dir, proj_name) :
# def get_build_root_dir(fips_dir, proj_name):
# def get_deploy_root_dir(fips_dir, proj_name):
# def get_build_dir(fips_dir, proj_name, cfg) :
# def get_deploy_dir(fips_dir, proj_name, cfg) :
# def get_fips_dir(proj_dir, name):
# def get_configs_dir(proj_dir):
# def get_verbs_dir(proj_dir):
# def get_generators_dir(proj_dir):
# def get_toolchains_dir(proj_dir):
# def get_giturl_from_url(url) :
# def get_gitbranch_from_url(url) :
# def get_project_name_from_url(url) :
# def get_project_name_from_dir(proj_dir) :
# def load_fips_yml(proj_dir) :
# def lookup_target_cwd(proj_dir, target) :
# def is_valid_project_dir(proj_dir) :
# def ensure_valid_project_dir(proj_dir) :
# def is_git_url(url) :
# def confirm(question) :
# def url_download_hook(count, block_size, total_size) :
# def get_host_platform() :
# def get_cfg_target_list(fips_dir, proj_dir, cfg):
# def get_cfg_headersdirs_by_target(fips_dir, proj_dir, cfg):
# def get_cfg_defines_by_target(fips_dir, proj_dir, cfg):
# def get_num_cpucores():
# 
# Path: mod/tools/java.py
# def check_exists(fips_dir) :
# 
# Path: mod/tools/javac.py
# def check_exists(fips_dir) :


# Path: mod/android.py
import os
import sys
import zipfile
import subprocess
import hashlib
    from urllib.request import urlretrieve
    from urllib import urlretrieve
from mod import log, util
from mod.tools import java, javac


#-------------------------------------------------------------------------------
def setup(fips_dir) :
    """setup the Android SDK and NDK"""
    log.colored(log.YELLOW, '=== setup Android SDK/NDK :')

    # first make sure that java is present, otherwise the Android
    # SDK setup will finish without errors, but is not actually usable
    if not java.check_exists(fips_dir) or not javac.check_exists(fips_dir) :
        log.error("please install Java JDK 8 (see './fips diag tools')")
    ensure_sdk_dirs(fips_dir)

    # download the command line tools archive
    tools_archive_path = get_tools_archive_path(fips_dir)
    tools_url = get_tools_url()
    log.info("downloading '{}'...".format(tools_url))
    urlretrieve(tools_url, tools_archive_path, util.url_download_hook)
    log.info("\nunpacking '{}'...".format(tools_archive_path))
    uncompress(fips_dir, tools_archive_path)

    # install the required SDK components through sdkmanager
    install_package(fips_dir, '"platforms;android-28"')
    install_package(fips_dir, '"build-tools;29.0.3"')
    install_package(fips_dir, 'platform-tools')
    install_package(fips_dir, 'ndk-bundle')

    # check for potentially breaking changes in build setup
    fips_cmake = fips_dir + '/cmake-toolchains/android.toolchain.orig'
    ndk_cmake = get_sdk_dir(fips_dir) + '/ndk-bundle/build/cmake/android.toolchain.cmake'
    if compute_sha256(ndk_cmake, strip_whitespace) != compute_sha256(fips_cmake, strip_whitespace) :

        log.warn('android.toolchain.cmake in fips might be outdated...')