#!/bin/bash
#
# @file build/tutto
# @version 1.0
#
# @section License
# Copyright (C) 2014-2016, 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
# Build all variants. Uses the CosaBuild example sketch as sandbox
# for this regression test script. Warnings and errors are written
# to standard output. If a board is given as parameter all example
# sketches are built.
#
# This file is part of the Arduino Che Cosa project.

if [ $# == 0 ];
then
   echo "Usage: tutto VERSION [BOARD]"
   echo "Build Cosa core for all boards or all example sketches for"
   echo "given BOARD and VERSION is Arduino version".
   exit 1
fi

ver=$1
shift
if [ $# == 0 ];
then
  # Build local CosaBuild for all board variants
  variants=`grep build.variant boards.txt | cut -d. -f1 | sort`
  for board in $variants
  do
    echo `date`: $board
    cosa $board "ARDUINO_VER=$ver" &> LOG
    cosa $board size "ARDUINO_VER=$ver" | grep bytes
    egrep "(warning|error)" LOG
  done
  rm LOG
else
  # Build all sketches for given board
  board=$1
  pushd .. &> /dev/null
  examples=`find -L -name *.ino | sort -f`
  for example in $examples
  do
    dir=`dirname $example`
    echo `date`: `echo $example | cut -c3-`
    pushd `dirname $example` &> /dev/null
      cosa $board "ARDUINO_VER=$ver" &> LOG
      cosa $board size "ARDUINO_VER=$ver" | grep bytes
      egrep "(warning|error)" LOG
      rm LOG
    popd &> /dev/null
  done
  popd &> /dev/null
fi


