PACKAGE_VERSION=@@pkgver@@
PACKAGE_NAME="lttng-modules"
AUTOINSTALL="yes"

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

VERSION="${kernelver%%.*}"
PATCHLEVEL="${kernelver#*.}"
PATCHLEVEL="${PATCHLEVEL%%.*}"
SUBLEVEL="${kernelver##*.}"
SUBLEVEL="${SUBLEVEL%%_*}"

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]="src/lib/"
DEST_MODULE_LOCATION[$i]="/extra/lib"
i=$((i+1))

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

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

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

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

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

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

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

BUILT_MODULE_NAME[$i]="lttng-ring-buffer-event-notifier-client"
BUILT_MODULE_LOCATION[$i]="src/"
DEST_MODULE_LOCATION[$i]="/extra"
i=$((i+1))

BUILT_MODULE_NAME[$i]="lttng-counter-client-percpu-32-modular"
BUILT_MODULE_LOCATION[$i]="src/"
DEST_MODULE_LOCATION[$i]="/extra"
i=$((i+1))

if [ "$KCONFIG" == true ] && [ "$CONFIG_64BIT" ]; then
	BUILT_MODULE_NAME[$i]="lttng-counter-client-percpu-64-modular"
	BUILT_MODULE_LOCATION[$i]="src/"
	DEST_MODULE_LOCATION[$i]="/extra"
	i=$((i+1))
fi

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

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

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

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

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

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

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

if [ "$VERSION" -lt 6 ] || [ -f "$kernel_source_dir/mm/slab.h" ]; then
	BUILT_MODULE_NAME[$i]="lttng-probe-kmem"
	BUILT_MODULE_LOCATION[$i]="src/probes/"
	DEST_MODULE_LOCATION[$i]="/extra/probes"
	i=$((i+1))
fi

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

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

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

# Linux 3.10+
if [ "$KCONFIG" == true ] && [ "$CONFIG_NET_9P" ]; then
	BUILT_MODULE_NAME[$i]="lttng-probe-9p"
	BUILT_MODULE_LOCATION[$i]="src/probes/"
	DEST_MODULE_LOCATION[$i]="/extra/probes"
	i=$((i+1))
fi

if [ -f "$kernel_source_dir/include/trace/events/i2c.h" ]; then
	BUILT_MODULE_NAME[$i]="lttng-probe-i2c"
	BUILT_MODULE_LOCATION[$i]="src/probes/"
	DEST_MODULE_LOCATION[$i]="/extra/probes"
	i=$((i+1))
fi

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

if [ "$KCONFIG" = "true" ] && [ -n "$CONFIG_KVM" ] && [ "$CONFIG_X86" ]; then
    if [ ! -f "$kernel_source_dir/virt/kvm/iodev.h" ] &&
         [ ! -f "$kernel_source_dir/include/kvm/iodev.h" ]; then
        :
    elif [ "$VERSION" -lt 6 ] ||
         [ -f "$kernel_source_dir/arch/x86/kvm/kvm_emulate.h" ]; then
        BUILT_MODULE_NAME[$i]="lttng-probe-kvm-x86"
        BUILT_MODULE_LOCATION[$i]="src/probes/"
        DEST_MODULE_LOCATION[$i]="/extra/probes"
        i=$((i+1))
        BUILT_MODULE_NAME[$i]="lttng-probe-kvm-x86-mmu"
        BUILT_MODULE_LOCATION[$i]="src/probes/"
        DEST_MODULE_LOCATION[$i]="/extra/probes"
        i=$((i+1))
    fi
fi

if [ "$KCONFIG" = "true" ] && [ "$CONFIG_X86" ]; then
    if [ -f "$kernel_source_dir/arch/x86/include/asm/trace/irq_vectors.h" ]; then
        BUILT_MODULE_NAME[$i]="lttng-probe-x86-irq-vectors"
        BUILT_MODULE_LOCATION[$i]="src/probes/"
        DEST_MODULE_LOCATION[$i]="/extra/probes"
        i=$((i+1))
    fi
    if [ -f "$kernel_source_dir/arch/x86/include/asm/trace/exceptions.h" ]; then
        BUILT_MODULE_NAME[$i]="lttng-probe-x86-exceptions"
        BUILT_MODULE_LOCATION[$i]="src/probes/"
        DEST_MODULE_LOCATION[$i]="/extra/probes"
        i=$((i+1))
    fi
fi

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

BUILT_MODULE_NAME[$i]="lttng-probe-signal"
BUILT_MODULE_LOCATION[$i]="src/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]="src/probes/"
    DEST_MODULE_LOCATION[$i]="/extra/probes"
    i=$((i+1))

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

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

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

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

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

if [ "$KCONFIG" = "true" ] && [ -n "$CONFIG_GPIOLIB" ]; then
    BUILT_MODULE_NAME[$i]="lttng-probe-gpio"
    BUILT_MODULE_LOCATION[$i]="src/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]="src/probes/"
    DEST_MODULE_LOCATION[$i]="/extra/probes"
    i=$((i+1))
fi

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

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

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

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

if [ "$KCONFIG" = true ] && [ "$CONFIG_COMPACTION" ]; then
    has_probe=
    if [ "$VERSION" -lt 5 ]; then
        has_probe=y
    elif [ "$VERSION" = 5 ] && [ "$PATCHLEVEL" -lt 18 ]; then
        has_probe=y
    elif [ -f "$kernel_source_dir/mm/internal.h" ]; then
        has_probe=y
    fi
    if [ "$has_probe" ]; then
        BUILT_MODULE_NAME[$i]="lttng-probe-compaction"
        BUILT_MODULE_LOCATION[$i]="src/probes/"
        DEST_MODULE_LOCATION[$i]="/extra/probes"
        i=$((i+1))
    fi
fi

if [ -f "$kernel_source_dir/include/trace/events/random.h" ]; then
   BUILT_MODULE_NAME[$i]="lttng-probe-random"
   BUILT_MODULE_LOCATION[$i]="src/probes/"
   DEST_MODULE_LOCATION[$i]="/extra/probes"
   i=$((i+1))
fi

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

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

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

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

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

BUILT_MODULE_NAME[$i]="lttng-probe-workqueue"
BUILT_MODULE_LOCATION[$i]="src/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]="src/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]="src/probes/"
   DEST_MODULE_LOCATION[$i]="/extra/probes"
   i=$((i+1))
fi

if [ "$KCONFIG" = "true" ] && [ -n "$CONFIG_UPROBES" ]; then
   BUILT_MODULE_NAME[$i]="lttng-uprobes"
   BUILT_MODULE_LOCATION[$i]="src/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]="src/probes/"
   DEST_MODULE_LOCATION[$i]="/extra/probes"
   i=$((i+1))
fi

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

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

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