#!/bin/sh

# https://flipper.wiki/tutorials/beginner_guide_fw/guide/
# https://github.com/flipperdevices/flipperzero-firmware/blob/dev/documentation/fbt.md#high-level-what-you-most-likely-need
# https://developer.flipper.net/flipperzero/doxygen/index.html

VERSION=${VERSION:-'1.4.3'}

set -e

if [ ! -d flipperzero-firmware ]; then
  git clone --recursive https://github.com/flipperdevices/flipperzero-firmware.git
fi
if [ ! -d flipperzero-good-faps ]; then
  git clone --recursive https://github.com/flipperdevices/flipperzero-good-faps.git
fi

cd flipperzero-firmware
git checkout "$VERSION"

# Building the firmware
./fbt updater_package
# In qFlipper, install from file 'dist/f7-D/flipper-z-f7-update-local.tgz'
# Over SD Card, copy 'dist/f7-D/f7-update-local/' to 'SD Card/Update/'

# Building applications
find ../flipperzero-good-faps -mindepth 1 -maxdepth 1 -type d ! -name '\.*' \
  -exec cp -r '{}' applications_user \;
./fbt fap_dist
# Copy 'dist/f7-D/apps' and 'dist/f7-D/apps_data' to 'SD Card/'