#!/usr/bin/env bash
set -euo pipefail

OPENWRT_DIR=${OPENWRT_DIR:-"$(cd "$(dirname "$0")/../openwrt" && pwd)"}
JOBS=${JOBS:-"$(nproc)"}

cd "$OPENWRT_DIR"
./scripts/feeds update -a
./scripts/feeds install -a
cat > .config <<'EOF'
CONFIG_KERNEL_BUILD_USER="nyarime"
CONFIG_KERNEL_BUILD_DOMAIN="naixi.net"
CONFIG_TARGET_ath79=y
CONFIG_TARGET_ath79_generic=y
CONFIG_TARGET_ath79_generic_DEVICE_xiaomi_mi-router-4-pro-r1350=y
CONFIG_DEVEL=y
CONFIG_BUILD_LOG=y
CONFIG_PACKAGE_luci-ssl=y
CONFIG_PACKAGE_kmod-ath10k-ct=y
CONFIG_PACKAGE_ath10k-firmware-qca9888-ct=y
CONFIG_PACKAGE_wpad-basic-mbedtls=y
EOF
make defconfig
make -j"$JOBS" download
make -j"$JOBS" V=sc

find bin/targets/ath79/generic -maxdepth 1 -type f \
  -name '*xiaomi_mi-router-4-pro-r1350*' -print
