"""
mbed SDK
Copyright (c) 2011-2013 ARM Limited

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import sys
from os.path import join, abspath, dirname, exists
ROOT = abspath(join(dirname(__file__), ".."))
sys.path.insert(0, ROOT)

from shutil import move

from workspace_tools.paths import *
from workspace_tools.utils import mkdir, cmd
from workspace_tools.export import export, setup_user_prj


USR_PRJ_NAME = "usr_prj"
USER_PRJ = join(EXPORT_WORKSPACE, USR_PRJ_NAME)
USER_SRC = join(USER_PRJ, "src")


def setup_test_user_prj():
    if exists(USER_PRJ):
        print 'Test user project already generated...'
        return

    setup_user_prj(USER_PRJ, join(TEST_DIR, "rtos", "mbed", "basic"), [join(LIB_DIR, "rtos")])

    # FAKE BUILD URL
    open(join(USER_SRC, "mbed.bld"), 'w').write("http://mbed.org/users/mbed_official/code/mbed/builds/976df7c37ad5\n")


def fake_build_url_resolver(url):
    # FAKE BUILD URL: Ignore the URL, always return the path to the mbed library
    return {'path':MBED_LIBRARIES, 'name':'mbed'}


def test_export(toolchain, target, expected_error=None):
    if toolchain is None and target is None:
        base_dir = join(EXPORT_TMP, "zip")
    else:
        base_dir = join(EXPORT_TMP, toolchain, target)
    temp_dir = join(base_dir, "temp")
    mkdir(temp_dir)

    zip_path, report = export(USER_PRJ, USR_PRJ_NAME, toolchain, target, base_dir, temp_dir, False, None, fake_build_url_resolver)

    if report['success']:
        move(zip_path, join(EXPORT_DIR, "export_%s_%s.zip" % (toolchain, target)))
        print "[OK]"
    else:
        if expected_error is None:
            print '[ERRROR] %s' % report['errormsg']
        else:
            if (zip_path is None) and (expected_error in report['errormsg']):
                print '[OK]'
            else:
                print '[ERROR]'
                print '    zip:', zip_path
                print '    msg:', report['errormsg']


if __name__ == '__main__':
    setup_test_user_prj()

    for toolchain, target in [
            ('zip', 'LPC1768'),
            
            ('emblocks', 'LPC1768'),
            ('emblocks', 'LPC1549'),
            ('emblocks', 'LPC1114'),
            ('emblocks', 'LPC11U35_401'),
            ('emblocks', 'LPC11U35_501'),
            ('emblocks', 'LPCCAPPUCCINO'),
            ('emblocks', 'LPC2368'),
            ('emblocks', 'STM32F407'),
            ('emblocks', 'DISCO_F100RB'),
            ('emblocks', 'DISCO_F051R8'),
            ('emblocks', 'DISCO_F407VG'),
            ('emblocks', 'DISCO_F303VC'),
            ('emblocks', 'NRF51822'),
            ('emblocks', 'NUCLEO_F401RE'),
            ('emblocks', 'NUCLEO_F411RE'),
            ('emblocks', 'MTS_MDOT_F405RG'),
            ('emblocks', 'MTS_MDOT_F411RE'),

            ('coide', 'KL05Z'),
            ('coide', 'KL25Z'),
            ('coide', 'LPC1768'),
            ('coide', 'ARCH_PRO'),
            ('coide', 'DISCO_F407VG'),
            ('coide', 'NUCLEO_F401RE'),
            ('coide', 'NUCLEO_F411RE'),
            ('coide', 'DISCO_F429ZI'),
            ('coide', 'NUCLEO_F334R8'),
            ('coide', 'MTS_MDOT_F405RG'),
            ('coide', 'MTS_MDOT_F411RE'),

            ('uvision', 'LPC1768'),
            ('uvision', 'LPC11U24'),
            ('uvision', 'KL25Z'),
            ('uvision', 'LPC1347'),
            ('uvision', 'LPC1114'),
            ('uvision', 'LPC4088'),
            ('uvision', 'LPC4088_DM'),
            ('uvision', 'LPC4337'),
            ('uvision', 'HRM1017'),

            ('uvision', 'NUCLEO_F030R8'),
            ('uvision', 'NUCLEO_F070RB'),
            ('uvision', 'NUCLEO_F072RB'),
            ('uvision', 'NUCLEO_F091RC'),
            ('uvision', 'NUCLEO_F103RB'),
            ('uvision', 'NUCLEO_F302R8'),
            ('uvision', 'NUCLEO_F303RE'),
            ('uvision', 'NUCLEO_F334R8'),