#!/bin/bash

set -e

SRC_DIR="/usr/src/iqrf-gateway-overlays"
DST_DIR="/boot/firmware/overlays"
FILE=iqrf-gpio-wd


if [ ! -d "$SRC_DIR" ]; then
    echo "[WARN] iqrf-gateway-overlays: source directory not found, exiting early." >&2
    exit 0
fi

if ! command -v dtc >/dev/null 2>&1; then
    echo "[WARN] iqrf-gateway-overlays: device-tree-compiler not found, skipping compilation." >&2
    exit 0
fi

if [ ! -f "$SRC_DIR/$FILE.dts" ]; then
    echo "[WARN] iqrf-gateway-overlays: .dts file not found, skipping compilation." >&2
    exit 0
fi

mkdir -p "$DST_DIR"
dtc -@ -I dts -O dtb -o "${DST_DIR}/${FILE}.dtbo" "${SRC_DIR}/${FILE}.dts"
