#   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/hardware/uracoli"

common_corefiles = [
     # arduino core files
     "zigbit/binary.h",
     "zigbit/HardwareRadio.h",
     "zigbit/HardwareRadio.cpp",
     "zigbit/HardwareSerial.cpp",
     "zigbit/HardwareSerial.h",
     "zigbit/main.cpp",
     "zigbit/Makefile",
     "zigbit/Print.cpp",
     "zigbit/Print.h",
     "zigbit/PureVirtual.cpp",
     "zigbit/Stream.h",
     "zigbit/Tone.cpp",
     "zigbit/WConstants.h",
     "zigbit/WInterrupts.c",
     "zigbit/wiring_analog.c",
     "zigbit/wiring.c",
     "zigbit/wiring_digital.c",
     "zigbit/wiring.h",
     "zigbit/wiring_private.h",
     "zigbit/wiring_pulse.c",
     "zigbit/wiring_shift.c",
     "zigbit/WMath.cpp",
     "zigbit/WProgram.h",
     "zigbit/WString.h",
     "zigbit/WString.cpp",
     "zigbit/pins_uracoli.h",
     # uracoli headers and libs
     "${dir_install}/inc/radio.h",
     "${dir_install}/inc/const.h",
     "${dir_install}/inc/board.h",
     "${dir_install}/inc/transceiver.h",
     "zigbit/sketch.tpl",
    ]

rfa_corefiles = [
     "#Src/Lib/Rf230/radio_rfa.c",
     "#Src/Lib/Rf230/trx_datarate.c",
     "#Src/Lib/Rf230/trx_datarate_str.c",
     "#Src/Lib/Rf230/trx_rfa.c",
     "#Src/Lib/Rf230/usr_radio_irq.c",
     "${dir_install}/inc/atmega_rfa1.h",
    ]


bootloader_files = [
    "bootloaders/radiofaro/ATmegaBOOT.c",
    "bootloaders/radiofaro/Makefile"
]


hw  = avrlocal.Install("$dir_arduino/", "boards.txt")

# 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"))
