#!/bin/bash

# variable params

BUILDPATH=./build
BINPATH=/APLICACIONES/arduino-1.0.5/hardware/tools/avr/bin
OPTIMIOPTS="-O0"

# Do not edit the following vars

COMPEXTRAOPTS="$2" # all compiler options must be passed in a single argument
SRCFILE=$(basename "$1")
FILENAME="${SRCFILE%%.*}"
if [[ ! $FILENAME ]]; then
	echo "You must provide a file+path for the source, with extension"
	exit
fi
CPU=atmega328p
CLOCK=16000000L
ARDUINOCOMPILEOPTS="-DUSB_VID=null -DUSB_PID=null -DARDUINO=105"
COMPILEOPTS="-Wall -fno-exceptions -ffunction-sections -fdata-sections"

# compile source

$BINPATH/avr-gcc -g $COMPILEOPTS $OPTIMIOPTS $COMPEXTRAOPTS -mmcu=$CPU -DF_CPU=$CLOCK -MMD $ARDUINOCOMPILEOPTS -c $1 -o $BUILDPATH/$FILENAME.o

# preprocessed code

$BINPATH/avr-gcc -g $COMPILEOPTS $OPTIMIOPTS -mmcu=$CPU -DF_CPU=$CLOCK -MMD $ARDUINOCOMPILEOPTS -E $1 -o $BUILDPATH/$FILENAME.i

# generate assembly

$BINPATH/avr-objdump -xdrC $BUILDPATH/$FILENAME.o > $BUILDPATH/$FILENAME.asm
