#!/bin/bash
# DKMS build script for camx camera driver.
# DKMS sets cwd to $dkms_tree/$module/$version/build/ before invoking MAKE.
# M=$(pwd) and CAMERA_KERNEL_ROOT=$(pwd) ensure .ko files and Kbuild
# include paths resolve correctly from the build directory.
set -euo pipefail

KERNEL_VER="${1}"
BUILD_DIR="$(pwd)"
# $5 is dkms's ${kernel_source_dir}: the kernel tree to build against. For a
# normal on-target install it is /lib/modules/${KERNEL_VER}/build; when the
# caller passes --kernelsourcedir it is a staged headers tree for a kernel that
# is not installed. The fallback keeps this script runnable by hand.
KERNEL_BUILD="${5:-/lib/modules/${KERNEL_VER}/build}"
PARALLEL_JOBS="${PARALLEL_JOBS-$(nproc)}"

if [ ! -d "${KERNEL_BUILD}" ]; then
    echo "ERROR: kernel build directory not found: ${KERNEL_BUILD}" >&2
    echo "       Install the matching kernel headers, or pass the tree to" >&2
    echo "       build against via dkms --kernelsourcedir." >&2
    exit 1
fi

{
    echo "#define CAMERA_COMPILE_TIME \"$(date)\""
    echo "#define CAMERA_COMPILE_HOST \"$(uname -n)\""
    CC_VER=$(LC_ALL=C gcc --version 2>/dev/null | head -n1)
    echo "#define CAMERA_CC_VERSION \"${CC_VER}\""
} > "${BUILD_DIR}/cam_generated_h"

SUPPORTED_ARCH=$(grep "^SUPPORTED_ARCH" "${BUILD_DIR}/Makefile" \
    | sed 's/SUPPORTED_ARCH[[:space:]]*=[[:space:]]*//')

if [ -z "${SUPPORTED_ARCH}" ]; then
    echo "ERROR: SUPPORTED_ARCH not found in ${BUILD_DIR}/Makefile" >&2
    exit 1
fi

BUILD_FAILED=0
for CAM_ARCH in ${SUPPORTED_ARCH}; do
    if make "-j${PARALLEL_JOBS}" -C "${KERNEL_BUILD}" \
            M="${BUILD_DIR}" \
            CAMERA_KERNEL_ROOT="${BUILD_DIR}" \
            CAMERA_ARCH="${CAM_ARCH}" \
            INSTALL_MOD_DIR=camera \
            modules; then
        echo "camera_${CAM_ARCH}.ko built OK"
    else
        echo "ERROR: build failed for CAMERA_ARCH=${CAM_ARCH}" >&2
        BUILD_FAILED=1
    fi
done

[ "${BUILD_FAILED}" -eq 0 ] || exit 1
