#!/bin/bash
#
# Simple script that desperately tries to load sysdig-probe looking
# for it in a bunch of ways. Convenient when running sysdig inside
# a container or in other weird environments.
#

if [ $(id -u) != 0 ]; then
	echo "Installer must be run as root (or with sudo)."
	exit 1
fi

if ! hash lsmod > /dev/null 2>&1; then
	echo "This program requires lsmod"
	exit 1
fi

if ! hash modprobe > /dev/null 2>&1; then
	echo "This program requires modprobe"
	exit 1
fi

if ! hash rmmod > /dev/null 2>&1; then
	echo "This program requires rmmod"
	exit 1
fi

if ! hash curl > /dev/null 2>&1; then
	echo "This program requires curl"
	exit 1
fi

if ! hash sysdig > /dev/null 2>&1; then
	echo "This program requires sysdig"
	exit 1
fi

ARCH=$(uname -m)
KERNEL_RELEASE=$(uname -r)
SYSDIG_VERSION=$(sysdig --version | cut -d' ' -f3)

if [ -z "$SYSDIG_REPOSITORY" ]; then
	SYSDIG_REPOSITORY="stable"
fi

echo "* Unloading sysdig-probe, if present"
rmmod sysdig-probe

if lsmod | grep sysdig_probe > /dev/null 2>&1; then
	echo "* sysdig-probe seems to still be loaded, hoping the best"
	exit 0
fi

echo "* Running dkms autoinstall"
dkms autoinstall --kernelver $KERNEL_RELEASE

echo "* Trying to load a system sysdig-probe, if present"

if modprobe sysdig_probe > /dev/null 2>&1; then
	echo "sysdig-probe found and loaded with modprobe"
	exit 0
fi

echo "* Trying to load a dkms sysdig-probe, if present"

if insmod /var/lib/dkms/sysdig/$SYSDIG_VERSION/$KERNEL_RELEASE/$ARCH/module/sysdig-probe.ko > /dev/null 2>&1; then
	echo "sysdig-probe found and loaded in dkms"
	exit 0
fi

echo "* Trying to find precompiled sysdig-probe for $KERNEL_RELEASE"

if [ -f /proc/config.gz ]; then
	echo "Found kernel config at /proc/config.gz"
	HASH=$(zcat /proc/config.gz | md5sum - | cut -d' ' -f1)
elif [ -f /boot/config-$KERNEL_RELEASE ]; then
	echo "Found kernel config at /boot/config-$KERNEL_RELEASE"
	HASH=$(md5sum /boot/config-$KERNEL_RELEASE | cut -d' ' -f1)	
elif [ ! -z "$SYSDIG_HOST_ROOT" ] && [ -f $SYSDIG_HOST_ROOT/boot/config-$KERNEL_RELEASE ]; then
	echo "Found kernel config at $SYSDIG_HOST_ROOT/boot/config-$KERNEL_RELEASE"
	HASH=$(md5sum $SYSDIG_HOST_ROOT/boot/config-$KERNEL_RELEASE | cut -d' ' -f1)	
fi

if [ -z "$HASH" ]; then
	echo "Cannot find kernel config"
	exit 1
fi

SYSDIG_PROBE_FILENAME="sysdig-probe-$SYSDIG_VERSION-$ARCH-$KERNEL_RELEASE-$HASH.ko"

if [ -f ~/.sysdig/$SYSDIG_PROBE_FILENAME ]; then
	echo "Found precompiled module at ~/.sysdig/$SYSDIG_PROBE_FILENAME, loading module"
	insmod ~/.sysdig/$SYSDIG_PROBE_FILENAME
	exit $?
fi

URL="https://s3.amazonaws.com/download.draios.com/$SYSDIG_REPOSITORY/sysdig-probe-binaries/$SYSDIG_PROBE_FILENAME"

echo "* Trying to download precompiled module from $URL"
if curl --create-dirs -f -s -o ~/.sysdig/$SYSDIG_PROBE_FILENAME $URL; then
	echo "Download succeeded, loading module"
	insmod ~/.sysdig/$SYSDIG_PROBE_FILENAME
	exit $?
else
	echo "Download failed, consider compiling your own sysdig-probe and loading it or getting in touch with the sysdig community"
	exit 1
fi
