PACKAGE_VERSION=2.10.0
PACKAGE_NAME="lttng-modules"
AUTOINSTALL="yes"

KCONFIG=false
if [ -f /boot/config-$kernelver ]; then
    . /boot/config-$kernelver
    KCONFIG=true
fi

VERSION=$(echo $kernelver | sed -ne 's/\([0-9]\+\)\.\([0-9]\+\)\.\([0-9]\+\)\-\(.\+\)/\1/p')
PATCHLEVEL=$(echo $kernelver | sed -ne 's/\([0-9]\+\)\.\([0-9]\+\)\.\([0-9]\+\)\-\(.\+\)/\2/p')
SUBLEVEL=$(echo $kernelver | sed -ne 's/\([0-9]\+\)\.\([0-9]\+\)\.\([0-9]\+\)\-\(.\+\)/\3/p')

i=0

MAKE[$i]="make -C $kernel_source_dir M=$dkms_tree/$PACKAGE_NAME/$PACKAGE_VERSION/build modules CONFIG_LTTNG=m CONFIG_LTTNG_CLOCK_PLUGIN_TEST=m"

BUILT_MODULE_NAME[$i]="lttng-lib-ring-buffer"
BUILT_MODULE_LOCATION[$i]="lib/"
DEST_MODULE_LOCATION[$i]="/extra/lib"
i=$((i+1))

BUILT_MODULE_NAME[$i]="lttng-ring-buffer-client-discard"
DEST_MODULE_LOCATION[$i]="/extra"
i=$((i+1))

BUILT_MODULE_NAME[$i]="lttng-ring-buffer-client-mmap-discard"
DEST_MODULE_LOCATION[$i]="/extra"
i=$((i+1))

BUILT_MODULE_NAME[$i]="lttng-ring-buffer-client-mmap-overwrite"
DEST_MODULE_LOCATION[$i]="/extra"
i=$((i+1))

BUILT_MODULE_NAME[$i]="lttng-ring-buffer-client-overwrite"
DEST_MODULE_LOCATION[$i]="/extra"
i=$((i+1))

BUILT_MODULE_NAME[$i]="lttng-ring-buffer-metadata-client"
DEST_MODULE_LOCATION[$i]="/extra"
i=$((i+1))

BUILT_MODULE_NAME[$i]="lttng-ring-buffer-metadata-mmap-client"
DEST_MODULE_LOCATION[$i]="/extra"
i=$((i+1))

BUILT_MODULE_NAME[$i]="lttng-clock"
DEST_MODULE_LOCATION[$i]="/extra"
i=$((i+1))

BUILT_MODULE_NAME[$i]="lttng-statedump"
DEST_MODULE_LOCATION[$i]="/extra"
i=$((i+1))

BUILT_MODULE_NAME[$i]="lttng-tracer"
DEST_MODULE_LOCATION[$i]="/extra"
i=$((i+1))

if [ "$KCONFIG" = "true" ] && [ -n "$CONFIG_SND_SOC" ]; then
    BUILT_MODULE_NAME[$i]="lttng-probe-asoc"
    BUILT_MODULE_LOCATION[$i]="probes/"
    DEST_MODULE_LOCATION[$i]="/extra/probes"
    i=$((i+1))
fi

if [ "$KCONFIG" = "true" ] && [ -n "$CONFIG_BLOCK" ] && [ -n "$CONFIG_EVENT_TRACING" ]; then
    BUILT_MODULE_NAME[$i]="lttng-probe-block"
    BUILT_MODULE_LOCATION[$i]="probes/"
    DEST_MODULE_LOCATION[$i]="/extra/probes"
    i=$((i+1))
fi

BUILT_MODULE_NAME[$i]="lttng-probe-compaction"
BUILT_MODULE_LOCATION[$i]="probes/"
DEST_MODULE_LOCATION[$i]="/extra/probes"
i=$((i+1))

if [ "$KCONFIG" = "true" ] && [ -n "$CONFIG_GPIOLIB" ]; then
    BUILT_MODULE_NAME[$i]="lttng-probe-gpio"
    BUILT_MODULE_LOCATION[$i]="probes/"
    DEST_MODULE_LOCATION[$i]="/extra/probes"
    i=$((i+1))
fi

BUILT_MODULE_NAME[$i]="lttng-probe-irq"
BUILT_MODULE_LOCATION[$i]="probes/"
DEST_MODULE_LOCATION[$i]="/extra/probes"
i=$((i+1))

if [ "$KCONFIG" = "true" ] && [ -n "$CONFIG_JBD" ]; then
    BUILT_MODULE_NAME[$i]="lttng-probe-jbd"
    BUILT_MODULE_LOCATION[$i]="probes/"
    DEST_MODULE_LOCATION[$i]="/extra/probes"
    i=$((i+1))
fi

if [ "$KCONFIG" = "true" ] && [ -n "$CONFIG_JBD2" ]; then
    BUILT_MODULE_NAME[$i]="lttng-probe-jbd2"
    BUILT_MODULE_LOCATION[$i]="probes/"
    DEST_MODULE_LOCATION[$i]="/extra/probes"
    i=$((i+1))
fi

BUILT_MODULE_NAME[$i]="lttng-probe-kmem"
BUILT_MODULE_LOCATION[$i]="probes/"
DEST_MODULE_LOCATION[$i]="/extra/probes"
i=$((i+1))

if [ "$KCONFIG" = "true" ] && [ -n "$CONFIG_KVM" ]; then
    BUILT_MODULE_NAME[$i]="lttng-probe-kvm"
    BUILT_MODULE_LOCATION[$i]="probes/"
    DEST_MODULE_LOCATION[$i]="/extra/probes"
    i=$((i+1))
fi

BUILT_MODULE_NAME[$i]="lttng-probe-module"
BUILT_MODULE_LOCATION[$i]="probes/"
DEST_MODULE_LOCATION[$i]="/extra/probes"
i=$((i+1))

if [ "$KCONFIG" = "true" ] && [ -n "$CONFIG_NET" ]; then
    BUILT_MODULE_NAME[$i]="lttng-probe-napi"
    BUILT_MODULE_LOCATION[$i]="probes/"
    DEST_MODULE_LOCATION[$i]="/extra/probes"
    i=$((i+1))

    BUILT_MODULE_NAME[$i]="lttng-probe-net"
    BUILT_MODULE_LOCATION[$i]="probes/"
    DEST_MODULE_LOCATION[$i]="/extra/probes"
    i=$((i+1))

    BUILT_MODULE_NAME[$i]="lttng-probe-skb"
    BUILT_MODULE_LOCATION[$i]="probes/"
    DEST_MODULE_LOCATION[$i]="/extra/probes"
    i=$((i+1))

    BUILT_MODULE_NAME[$i]="lttng-probe-sock"
    BUILT_MODULE_LOCATION[$i]="probes/"
    DEST_MODULE_LOCATION[$i]="/extra/probes"
    i=$((i+1))

    BUILT_MODULE_NAME[$i]="lttng-probe-udp"
    BUILT_MODULE_LOCATION[$i]="probes/"
    DEST_MODULE_LOCATION[$i]="/extra/probes"
    i=$((i+1))
fi

BUILT_MODULE_NAME[$i]="lttng-probe-power"
BUILT_MODULE_LOCATION[$i]="probes/"
DEST_MODULE_LOCATION[$i]="/extra/probes"
i=$((i+1))

BUILT_MODULE_NAME[$i]="lttng-probe-printk"
BUILT_MODULE_LOCATION[$i]="probes/"
DEST_MODULE_LOCATION[$i]="/extra/probes"
i=$((i+1))

BUILT_MODULE_NAME[$i]="lttng-probe-random"
BUILT_MODULE_LOCATION[$i]="probes/"
DEST_MODULE_LOCATION[$i]="/extra/probes"
i=$((i+1))

BUILT_MODULE_NAME[$i]="lttng-probe-rcu"
BUILT_MODULE_LOCATION[$i]="probes/"
DEST_MODULE_LOCATION[$i]="/extra/probes"
i=$((i+1))

if [ "$KCONFIG" = "true" ] && [ -n "$CONFIG_REGULATOR" ]; then
    BUILT_MODULE_NAME[$i]="lttng-probe-regulator"
    BUILT_MODULE_LOCATION[$i]="probes/"
    DEST_MODULE_LOCATION[$i]="/extra/probes"
    i=$((i+1))
fi

if [ "$KCONFIG" = "true" ] && [ -n "$CONFIG_PM_RUNTIME" ]; then
    BUILT_MODULE_NAME[$i]="lttng-probe-rpm"
    BUILT_MODULE_LOCATION[$i]="probes/"
    DEST_MODULE_LOCATION[$i]="/extra/probes"
    i=$((i+1))
fi

BUILT_MODULE_NAME[$i]="lttng-probe-sched"
BUILT_MODULE_LOCATION[$i]="probes/"
DEST_MODULE_LOCATION[$i]="/extra/probes"
i=$((i+1))

if [ "$KCONFIG" = "true" ] && [ -n "$CONFIG_SCSI" ]; then
    BUILT_MODULE_NAME[$i]="lttng-probe-scsi"
    BUILT_MODULE_LOCATION[$i]="probes/"
    DEST_MODULE_LOCATION[$i]="/extra/probes"
    i=$((i+1))
fi

BUILT_MODULE_NAME[$i]="lttng-probe-signal"
BUILT_MODULE_LOCATION[$i]="probes/"
DEST_MODULE_LOCATION[$i]="/extra/probes"
i=$((i+1))

BUILT_MODULE_NAME[$i]="lttng-probe-statedump"
BUILT_MODULE_LOCATION[$i]="probes/"
DEST_MODULE_LOCATION[$i]="/extra/probes"
i=$((i+1))

if [ "$KCONFIG" = "true" ] && [ -n "$CONFIG_SUNRPC" ]; then
    BUILT_MODULE_NAME[$i]="lttng-probe-sunrpc"
    BUILT_MODULE_LOCATION[$i]="probes/"
    DEST_MODULE_LOCATION[$i]="/extra/probes"
    i=$((i+1))
fi

BUILT_MODULE_NAME[$i]="lttng-probe-timer"
BUILT_MODULE_LOCATION[$i]="probes/"
DEST_MODULE_LOCATION[$i]="/extra/probes"
i=$((i+1))

BUILT_MODULE_NAME[$i]="lttng-probe-vmscan"
BUILT_MODULE_LOCATION[$i]="probes/"
DEST_MODULE_LOCATION[$i]="/extra/probes"
i=$((i+1))

BUILT_MODULE_NAME[$i]="lttng-probe-workqueue"
BUILT_MODULE_LOCATION[$i]="probes/"
DEST_MODULE_LOCATION[$i]="/extra/probes"
i=$((i+1))

if [ "$KCONFIG" = "true" ] && [ -n "$CONFIG_KALLSYMS_ALL" ]; then
    BUILT_MODULE_NAME[$i]="lttng-probe-writeback"
    BUILT_MODULE_LOCATION[$i]="probes/"
    DEST_MODULE_LOCATION[$i]="/extra/probes"
    i=$((i+1))
fi

if [ "$KCONFIG" = "true" ] && [ -n "$CONFIG_KPROBES" ]; then
   BUILT_MODULE_NAME[$i]="lttng-kprobes"
   BUILT_MODULE_LOCATION[$i]="probes/"
   DEST_MODULE_LOCATION[$i]="/extra/probes"
   i=$((i+1))
fi

if [ "$KCONFIG" = "true" ] && [ -n "$CONFIG_KRETPROBES" ]; then
   BUILT_MODULE_NAME[$i]="lttng-kretprobes"
   BUILT_MODULE_LOCATION[$i]="probes/"
   DEST_MODULE_LOCATION[$i]="/extra/probes"
   i=$((i+1))
fi

if [ "$KCONFIG" = "true" ] && [ -n "$CONFIG_DYNAMIC_FTRACE" ] && \
   ( [ "$VERSION" -eq 4 -a "$PATCHLEVEL" -lt 12 ] || [ "$VERSION" -lt 4 ] ); then
   BUILT_MODULE_NAME[$i]="lttng-ftrace"
   BUILT_MODULE_LOCATION[$i]="probes/"
   DEST_MODULE_LOCATION[$i]="/extra/probes"
   i=$((i+1))
fi

if [ "$KCONFIG" = "true" ] && [ -n "$CONFIG_VIDEO_V4L2" ] && \
   ( [ "$VERSION" -ge 3 -a "$PATCHLEVEL" -ge 14 ] || [ "$VERSION" -ge 4 ] ); then
   BUILT_MODULE_NAME[$i]="lttng-probe-v4l2"
   BUILT_MODULE_LOCATION[$i]="probes/"
   DEST_MODULE_LOCATION[$i]="/extra/probes"
   i=$((i+1))
fi

BUILT_MODULE_NAME[$i]="lttng-test"
BUILT_MODULE_LOCATION[$i]="tests/"
DEST_MODULE_LOCATION[$i]="/extra/tests"
i=$((i+1))

BUILT_MODULE_NAME[$i]="lttng-clock-plugin-test"
BUILT_MODULE_LOCATION[$i]="tests/"
DEST_MODULE_LOCATION[$i]="/extra/tests"
i=$((i+1))
