#!python3

import cmd
from lib2to3.pgen2.token import EQUAL
from operator import eq
import os, stat, subprocess, shutil, sys
from tabnanny import check
from mod import log, util
from mod.tools import git

TTBLITSDK_URL = "https://github.com/32blit/32blit-sdk.git"

# this is an error callback function for shutil.rmtree to make
# read-only files writable after rmtree failed to delete them
#
# from: https://docs.python.org/3.5/library/shutil.html#rmtree-example
#
def remove_readonly(func, path, _):
    os.chmod(path, stat.S_IWRITE)
    func(path)

#-------------------------------------------------------------------------------
def get_sdkroot_dir(fips_dir):
    return util.get_workspace_dir(fips_dir) + '/fips-sdks'

#-------------------------------------------------------------------------------
def sdkroot_exists(fips_dir):
    sdkroot_dir = get_sdkroot_dir(fips_dir)
    return os.path.isdir(sdkroot_dir)

#-------------------------------------------------------------------------------
def make_dirs(fips_dir):
    if not sdkroot_exists(fips_dir):
        os.makedirs(get_sdkroot_dir(fips_dir))

#-------------------------------------------------------------------------------
def get_sdk_dir(fips_dir):
    return get_sdkroot_dir(fips_dir) + '/32blit-sdk'

#-------------------------------------------------------------------------------
def sdk_dir_exists(fips_dir):
    sdk_dir = get_sdk_dir(fips_dir)
    return os.path.isdir(sdk_dir)

#-------------------------------------------------------------------------------
def check_exists(fips_dir):
    return os.path.isdir(get_sdk_dir(fips_dir))

#-------------------------------------------------------------------------------
def run(blit_file_abspath):
    print("Installing and launching {}".format(blit_file_abspath))
    cmdline='python3 -m ttblit install --port=AUTO --launch {}'.format(blit_file_abspath)
    subprocess.call(cmdline.split())

#-------------------------------------------------------------------------------
def clone_or_update(fips_dir):
    # returns True on success
    sdk_dir = get_sdk_dir(fips_dir)
    if sdk_dir_exists(fips_dir):
        log.colored(log.YELLOW, "=== updating 32blit SDK at '{}'".format(sdk_dir))
        return git.update_force_and_ignore_local_changes(sdk_dir)
    else:
        log.colored(log.YELLOW, "=== cloning 32blit SDK to '{}'".format(sdk_dir))
        make_dirs(fips_dir)
        sdkroot_dir = get_sdkroot_dir(fips_dir)
        return git.clone(TTBLITSDK_URL, None, 1, "32blit-sdk", sdkroot_dir)

#-------------------------------------------------------------------------------
def setup(fips_dir):
    if not clone_or_update(fips_dir):
        log.error('Failed to install or update 32blit SDK')
        return

    tools_dir = os.path.abspath(os.path.join(fips_dir, '../32blit-tools'))
    print("32blit setup, fips_dir={}".format(fips_dir))

    retcode = subprocess.call(['make', 'install'], cwd=tools_dir)
    
    if retcode != 0:
        log.error('32blit toolkit failed to install with code {}'.format(retcode))
