#!/bin/bash

if test $# -ne 1; then
  echo "Usage: make-bl-and-app out"
  exit 1
fi
 
HEX2IOIO=tools/hex2ioio/hex2ioio
MERGE=tools/merge-hex

function make-one-ioio {
  BL=firmware/bootloader/dist/$1/production/bootloader.production.hex
  FW=firmware/app_layer_v1/dist/$2/production/app_layer_v1.production.hex
  OUTPREFIX=$3
  if test ! -f $BL; then
    echo Missing file: $BL
    exit 1
  fi
  if test ! -f $FW; then
    echo Missing file: $FW
    exit 1
  fi
  HEX=$OUTPREFIX.hex
  $MERGE $BL $FW > $HEX
  $HEX2IOIO $HEX $OUTPREFIX.ioio
}

TEMPDIR=$(mktemp -d -t ioio)

make-one-ioio SPRK0012 IOIO0021 $TEMPDIR/SPRK0012
make-one-ioio SPRK0013 IOIO0022 $TEMPDIR/SPRK0013
make-one-ioio SPRK0014 IOIO0022 $TEMPDIR/SPRK0014
make-one-ioio SPRK0015 IOIO0022 $TEMPDIR/SPRK0015
make-one-ioio SPRK0016 IOIO0023 $TEMPDIR/SPRK0016
zip -j $1.zip $TEMPDIR/*.hex
zip -j $1.ioioimg $TEMPDIR/*.ioio
 
