#   Copyright (c) 2009 Axel Wachtler
#   All rights reserved.
#
#   Redistribution and use in source and binary forms, with or without
#   modification, are permitted provided that the following conditions
#   are met:
#
#   * Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#   * Neither the name of the authors nor the names of its contributors
#     may be used to endorse or promote products derived from this software
#     without specific prior written permission.
#
#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
#   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#   POSSIBILITY OF SUCH DAMAGE.

# $Id$

#=== modules ==================================================================
import pprint, glob, os
Import("contr board")

#=== setup build environment ==================================================
avrlocal = contr.Clone()
avrlocal["CPU"]= "atmega128rfa1"
avrlocal["BOARD_TYPE"] = "radiofaro"
avrlocal["dir_arduino"] = "${dir_build}/arduino_1x"

avrlocal["dir_core"] = "${dir_arduino}/hardware/uracoli/cores"
avrlocal["dir_variants"] = "${dir_arduino}/hardware/uracoli/variants"

# === core and variants =======================================================
avrlocal.Install("${dir_arduino}/hardware/uracoli",
                 "hardware_1x/uracoli/boards.txt")

avrlocal.Install("${dir_core}/uracoli",
                 Glob("hardware_1x/uracoli/cores/uracoli/*.[hc]*"))

avrlocal.Install("${dir_core}/uracoli",
                 ["#install/inc/atmega_rfa1.h",
                  "#install/inc/at86rf230a.h",
                  "#install/inc/at86rf230b.h",
                  "#install/inc/at86rf212.h",
                  "#install/inc/board.h",
                  "#install/inc/const.h",
                  "#install/inc/radio.h",
                  "#install/inc/transceiver.h",
                  "#Src/Lib/Rf230/radio_rfa.c",
                  "#Src/Lib/Rf230/radio_rf230.c",
                  "#Src/Lib/Rf230/trx_datarate.c",
                  "#Src/Lib/Rf230/trx_datarate_str.c",
                  "#Src/Lib/Rf230/trx_rf230_bitrd.c",
                  "#Src/Lib/Rf230/trx_rf230_bitwr.c",
                  "#Src/Lib/Rf230/trx_rf230.c",
                  "#Src/Lib/Rf230/trx_rf230_crc.c",
                  "#Src/Lib/Rf230/trx_rf230_frame.c",
                  "#Src/Lib/Rf230/trx_rf230_irq.c",
                  "#Src/Lib/Rf230/trx_rf230_misc.c",
                  "#Src/Lib/Rf230/trx_rf230_param.c",
                  "#Src/Lib/Rf230/trx_rf230_sram.c",
                  "#Src/Lib/Rf230/trx_rfa.c",
                  "#Src/Lib/Rf230/usr_radio_irq.c"])

avrlocal.Install("${dir_core}/uracoli/boards",
                 ["#install/inc/boards/board_derfa.h",
                 "#install/inc/boards/board_wdba1281.h",
                 "#install/inc/boards/base_zdma1281.h"])

avrlocal.BoardCfgWriter("${dir_core}/uracoli/board_cfg.h",
                        "#${BOARD_CFG_FILE}",
                        SELECTED_BOARDS = ["radiofaro", "wdba1281"],
                        PRESELECTED_BOARD = "radiofaro")

for variant_hdr in avrlocal.Glob("hardware_1x/uracoli/variants/*/pins_arduino.h"):
    variant_name = os.path.basename(os.path.dirname(str(variant_hdr)))
    avrlocal.Install("${dir_variants}/%s" % variant_name, variant_hdr)



# === bootloaders ==============================================================
avrlocal.InstallAs("${dir_arduino}/hardware/uracoli/bootloaders",
                   "hardware_1x/uracoli/bootloaders")

# === examples =================================================================
avrlocal.InstallAs("${dir_arduino}/examples", "examples")


# === documentation and licenses ===============================================
avrlocal.Install("${dir_arduino}/doc",
                 ["arduino.txt",
                  "license_gpl_2v0.txt",
                  "license_gpl_3v0.txt",
                  "license_lgpl_2v1.txt"])
avrlocal.InstallAs("${dir_arduino}/doc/license_uracoli.txt", "#LICENSE")

if avrlocal["HAVE_ASCIIDOC"]:
    avrlocal.Command("${dir_arduino}/doc/arduino.pdf", "arduino.txt",
                ["a2x -v -L -f pdf -D ${dir_arduino}/doc $SOURCE"])


# === arscons ==================================================================
#avrlocal.InstallAs("${dir_core}/uracoli", "arscons")


# RadioFaro files
#hw += avrlocal.Install("$dir_arduino/cores/radiofaro", common_corefiles)
#hw += avrlocal.Install("$dir_arduino/cores/radiofaro", rfa_corefiles)
#hw += avrlocal.Install("$dir_arduino/cores/radiofaro", "zigbit/pins_rfarduino.c")
#hw += avrlocal.Install("$dir_arduino/cores/radiofaro/boards", "${dir_install}/inc/boards/board_derfa.h")
#hw += avrlocal.BoardCfgWriter("$dir_arduino/cores/radiofaro/board_cfg.h", "#${BOARD_CFG_FILE}",
#                                SELECTED_BOARDS = ["radiofaro"],
#                                PRESELECTED_BOARD = "radiofaro")

# ZigDuino Files
#hw += avrlocal.Install("$dir_arduino/cores/zigduino", common_corefiles)
#hw += avrlocal.Install("$dir_arduino/cores/zigduino", rfa_corefiles)
#hw += avrlocal.Install("$dir_arduino/cores/radiofaro", "zigbit/pins_zigduino.c")
#hw += avrlocal.Install("$dir_arduino/cores/zigduino/boards", "${dir_install}/inc/boards/board_derfa.h")
#hw += avrlocal.BoardCfgWriter("$dir_arduino/cores/zigduino/board_cfg.h", "#${BOARD_CFG_FILE}",
#                                SELECTED_BOARDS = ["zigduino"],
#                                PRESELECTED_BOARD = "zigduino")


#bloader = avrlocal.Install("$dir_arduino/bootloaders/radiofaro", bootloader_files)
#hw += avrlocal.Command("$dir_arduino/bootloaders/radiofaro/ATmegaBOOT_radiofaro.hex",
#        bloader,
#        ["make -C ${dir_arduino}/bootloaders/radiofaro"])


#
# Build documentation.
#
#avrlocal['dox_install_dir'] = avrlocal.Dir("$dir_arduino/doc")
#avrlocal['dox_title'] = "Arduino Support Package"
#avrlocal['dox_source_browser'] = "YES"
#avrlocal["dox_enabled_sections"] = "ARDUINODOC"
#avrlocal['dox_generate_tagfile'] = "alldoc.tag"
#avrlocal["dox_input_files"] = "${dir_src_doc}/Contrib/arduino.dox "\
#                              "${dir_top}/Contrib/Arduino/zigbit/HardwareRadio.h "#
#
#avrlocal["dox_example_path"] = ["Contrib/Arduino/examples/Radio/HelloRadio",#
#                                "Contrib/Arduino/examples/Radio/RadioUart",
#                                "Contrib/Arduino/examples/Radio/IoRadio" ]
#
#doxyfile = avrlocal.DoxyfileTransformer("Doxyfile","${dir_src_doc}/Doxyfile.in")
#
#htmlfile = avrlocal.Command("${dox_install_dir}/index.html",
#                         [doxyfile, "${dir_src_doc}/uracoli.css",
#                         "${dir_src_doc}/header.html", "${dir_src_doc}/footer.html",],
#                         ["doxygen $SOURCE"])
#
#avrlocal.Depends(htmlfile, avrlocal.Split("${dox_input_files} ${dir_top}/LICENSE"))
