#!/bin/bash
### usage: make-tra [opts..]
###
### options:
###   --help                  This help
###   --outputdir <path>      Where to write generated release archive, defaults to current directory
###   --version <version>     Version to write to release archive, reads from firmware image by default
###   --fwrepo <path>         Path to main firmware repository with built firmware images, defaults to tangara-fw

set -euo pipefail
source "$(dirname "$0")/../script/lib/functions.sh"

# read command line options
FIRMWARE_REPO=tangara-fw
OUTPUT_DIR="$PWD"
VERSION=
while [ "$#" -gt 0 ]; do
    case "$1" in
        --outputdir)
            [ "$#" -ge 2 ] || usage
            OUTPUT_DIR="$2"
            shift
            shift
            ;;
        --version)
            [ "$#" -ge 2 ] || usage
            VERSION="$2"
            shift
            shift
            ;;
        --fwrepo)
            [ "$#" -ge 2 ] || usage
            FIRMWARE_REPO="$2"
            shift
            shift
            ;;
        --help)
            usage
            ;;
        *)
            echo "unknown option: $1" >&2
            echo "" >&2
            usage
            ;;
    esac
done

# check for prerequisite commands
check-command dd
check-command tr
check-command zip

if ! [ -f "$FIRMWARE_REPO/build/tangara.bin" ]; then
    die "Firmware not found. Use --fwrepo to specify directory of tangara-fw repository containing built firmware"
fi

# read version out of firmware image
[ -n "$VERSION" ] || VERSION="$(dd if="$FIRMWARE_REPO/build/tangara.bin" bs=16 count=1 skip=3 status=none | tr -d '\000')" || die "reading version from firmware image"
[ -n "$VERSION" ] || die "no version in firmware image"
echo "$(tput setaf 4 bold)Found firmware version:$(tput sgr0) $(tput bold)${VERSION}$(tput sgr0)"
OUTPUT_FILENAME="tangarafw-v$VERSION.tra"

# create staging dir for zip
TMP_DIR="/tmp/make-tra-$$"
mkdir -p "$TMP_DIR" || die "creating staging dir: $TMP_DIR"

# write the needful
cp "$FIRMWARE_REPO/build/bootloader/bootloader.bin" "$FIRMWARE_REPO/build/partition_table/partition-table.bin" "$FIRMWARE_REPO/build/ota_data_initial.bin" "$FIRMWARE_REPO/build/tangara.bin" "$FIRMWARE_REPO/tools/collate/Generic.LC_COLLATE" "$FIRMWARE_REPO/build/lua.bin" "$FIRMWARE_REPO/build/repl.bin" "$TMP_DIR" || die "copying firmware images to staging dir"
cat > "$TMP_DIR/tangaraflash.json" <<JSON || die "writing json manifest to staging dir"
    {
        "version": 0,
        "data": {
            "firmware": {
                "version": "$VERSION",
                "images": [
                    { "addr": 4096, "name": "bootloader.bin" },
                    { "addr": 32768, "name": "partition-table.bin" },
                    { "addr": 53248, "name": "ota_data_initial.bin" },
                    { "addr": 65536, "name": "tangara.bin" },
                    { "addr": 8454144, "name": "Generic.LC_COLLATE" },
                    { "addr": 11599872, "name": "lua.bin" },
                    { "addr": 15794176, "name": "repl.bin" }
                ]
            }
        }
    }
JSON

# generate the TRA (Tangara Release Archive)
(cd "$TMP_DIR" && zip -9 $OUTPUT_FILENAME tangaraflash.json bootloader.bin partition-table.bin ota_data_initial.bin tangara.bin Generic.LC_COLLATE lua.bin repl.bin) || die "creating zip archive"

# move built TRA back to source dir
mv "$TMP_DIR/$OUTPUT_FILENAME" "$OUTPUT_DIR" || die "writing output archive"

win "created $OUTPUT_DIR/$OUTPUT_FILENAME"
