#!/bin/bash
# ci build script
# runs tests and then builds the OpenWRT package
# requires the following env vars:
# - $BUILD_DIR
# - $DOWNLOADS_DIR
# - $CORES (defaults to 1)
set -e

BUILD_DIR=${BUILD_DIR-./build}
DOWNLOADS_DIR=${DOWNLOADS_DIR-./downloads}
START_TIME=${START_TIME-$(date +"%Y-%m-%d-%H%M%S")}
VERSIONED_DIR="$DOWNLOADS_DIR/$START_TIME"
COMPILE_TARGET=${COMPILE_TARGET-mips_24kc}
LATEST_LINK="$DOWNLOADS_DIR/latest"
CORES=${CORES:-1}
CURRENT_DIR=$(pwd)
OPENWRT_BRANCH="openwrt-21.02"

mkdir -p "$BUILD_DIR"
mkdir -p "$VERSIONED_DIR"
cd "$BUILD_DIR"

if ! [ -d "openwrt" ]; then
	git clone https://git.openwrt.org/openwrt/openwrt.git
fi
cd openwrt
git reset --hard HEAD
git fetch origin
git checkout $OPENWRT_BRANCH
git reset --hard origin/$OPENWRT_BRANCH

# configure feeds
echo "src-git openwisp_config https://github.com/openwisp/openwisp-config.git^1.0.0" >feeds.conf
echo "src-link openwisp_monitoring $CURRENT_DIR" >>feeds.conf
cat feeds.conf.default >>feeds.conf
# remove unneeded feeds
sed -i '/telephony/d' feeds.conf
sed -i '/routing/d' feeds.conf
./scripts/feeds update -a
./scripts/feeds install -a
# add required packages
{
	echo "CONFIG_PACKAGE_netjson-monitoring=y"
	echo "CONFIG_PACKAGE_openwisp-monitoring=y"
} >>.config
make defconfig

if [ ! "$CI_CACHE" ]; then
	make -j"$CORES" tools/install
	make -j"$CORES" toolchain/install
fi

make -j"$CORES" package/openwisp-monitoring/compile || exit 1

mv "$BUILD_DIR/openwrt/bin/packages/$COMPILE_TARGET/openwisp_monitoring" "$VERSIONED_DIR"

rm "$LATEST_LINK" || true
ln -s "$VERSIONED_DIR" "$LATEST_LINK"
