#!/bin/sh
# Detect target kernel declarations, including distribution API backports.
set -eu
root=$1
headers() {
    for name in "$@"; do
        if [ -f "$root/include/linux/$name" ]; then
            cat "$root/include/linux/$name"
        fi
    done | tr '\n\t' '  '
}
flag() {
    name=$1
    pattern=$2
    shift 2
    if headers "$@" | grep -Eq "$pattern"; then value=1; else value=0; fi
    printf ' -D%s=%s' "$name" "$value"
}
flag DAHDI_SEMAPHORE_TWO_ARGS '#define[ ]+DEFINE_SEMAPHORE[ ]*\([^)]*,' semaphore.h
flag DAHDI_BUS_MATCH_CONST '\(\*match\)[ ]*\([^;]*const[ ]+struct[ ]+device_driver' device/bus.h device.h
flag DAHDI_BUS_UEVENT_CONST '\(\*uevent\)[ ]*\([ ]*const[ ]+struct[ ]+device' device/bus.h device.h
flag DAHDI_CLASS_CREATE_ONE_ARG 'class_create[ ]*\([ ]*const[ ]+char[ ]*\*' device/class.h device.h
printf '\n'
