#!/bin/sh
# Expected to be called as: depmodwrapper -a KERNEL_VERSION
if [ "$1" != "-a" -o "$2" != "-b" ]; then
    echo "Usage: depmodwrapper -a -b rootfs KERNEL_VERSION" >&2
    exit 1
fi
if [ ! -r FIXMESTAGINGDIRHOST/pkgdata/kernel-depmod/kernel-abiversion ]; then
    echo "Unable to read: FIXMESTAGINGDIRHOST/pkgdata/kernel-depmod/kernel-abiversion" >&2
else
    kernelabi=$(cat FIXMESTAGINGDIRHOST/pkgdata/kernel-depmod/kernel-abiversion)
    if [ "$kernelabi" != "$4" ]; then
        echo "Error: Kernel version $4 does not match kernel-abiversion ($kernelabi)" >&2
        exit 1
    fi
fi

if [ ! -r FIXMESTAGINGDIRHOST/pkgdata/kernel-depmod/System.map-$4 ]; then
    echo "Unable to read: FIXMESTAGINGDIRHOST/pkgdata/kernel-depmod/System.map-$4" >&2
    exec env depmod "$1" "$2" "$3" "$4"
else
    exec env depmod "$1" "$2" "$3" -F "FIXMESTAGINGDIRHOST/pkgdata/kernel-depmod/System.map-$4" "$4"
fi
