#!/bin/bash

PRJ_FILE="${1}"

if [ -z "$PRJ_FILE" ]; then
    echo "No exported project file given, exiting..."
    exit 1
fi

if [[ "$PRJ_FILE" != *atzip ]]; then
    echo -n "Exported project extension has to be .atzip instead of "
    echo    "${PRJ_FILE##*.}, exiting..."
    exit 1
fi

if [ ! -f "$PRJ_FILE" ]; then
    echo "No exported project file at ${PRJ_FILE}, exiting..."
    exit 1
fi

echo -n "Will try to compare the exported ATMEL START project with "
echo -n "the local project (ignoring main.c, gcc Makefile, and any "
echo    "changes to the linker script)..."
echo

TMPDIR=$(mktemp -d)
echo "Extracting project archive into the temporary directory ${TMPDIR}..."
unzip -d "$TMPDIR" "$PRJ_FILE" 1>&/dev/null

# main file and gcc build directory are moved, remove them from the
# exported project (ignoring them)
rm "$TMPDIR/main.c"
rm "$TMPDIR/gcc" -rf

echo "Diffing..."

# exclude linker script
diff -urdw atmel_start_prj "$TMPDIR" -x samd21g18a_flash.ld

echo
echo "Deleting temporary directory ${TMPDIR}..."
rm "$TMPDIR:?/"* -rf
rmdir "$TMPDIR"
