#!/bin/bash
#
# @file cosa
# @version 1.0
#
# @section License
# Copyright (C) 2014-2015, Mikael Patel
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# @section Description
# The Cosa Arduino-Makefile build command driver.
#
# This file is part of the Arduino Che Cosa project.

if [ $# == 0 ];
then
   echo "Usage: cosa [BOARD] [COMMAND]"
   echo "Build a Cosa sketch for the given board. Some useful commands are:"
   echo "  'cosa help' for more command details (make targets)"
   echo "  'cosa boards' for list of supported boards"
   echo "  'cosa BOARD config' show configuration for given board"
   echo "  'cosa BOARD upload' compile and upload"
   echo "  'cosa BOARD ispload ISP_PORT=/dev/ttyACM0' compile and upload with ISP on device ttyACM0"
   echo "  'cosa BOARD monitor' connect serial monitor"
   echo "  'cosa BOARD monitor MONITOR_PORT=/dev/ttyUSB0' connect serial monitor on device ttyUSB0"
   echo "  'cosa BOARD clean' remove all generated files"
   echo "  'cosa BOARD avanti' compile, upload and connect serial monitor"
   echo "Where BOARD is one of the supported boards (see cosa boards)"
   exit 1
fi

[ -z "$COSA_DIR" ] && COSA_DIR=$HOME/Sketchbook/hardware/Cosa ; export COSA_DIR

if [ "$1" == "help" ];
then
   make -f $COSA_DIR/build/Cosa.mk help "ARDUINO_QUIET=1"
   exit 0
fi

if [ "$1" == "boards" ];
then
   make -f $COSA_DIR/build/Cosa.mk boards "ARDUINO_QUIET=1"
   exit 0
fi

if [ "$2" == "config" ];
then
   make -f $COSA_DIR/build/Cosa.mk "BOARD_TAG=$1" config
   exit 0
fi

BOARD=$1
shift
make -j -f $COSA_DIR/build/Cosa.mk "BOARD_TAG=$BOARD" "ARDUINO_QUIET=1" $*
