#!/usr/bin/env bash
#
# Copyright (C) 2020 The Falco Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#     http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
set -e

function install_rpm {
	if ! hash curl > /dev/null 2>&1; then
		echo "* Installing curl"
		yum -q -y install curl
	fi
	
	if ! rpm -qa | grep epel > /dev/null 2>&1; then
		echo "* Installing EPEL repository (for DKMS)"
		if [ "$VERSION" -eq 8 ]; then
			rpm --quiet -i https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
		elif [ "$VERSION" -eq 7 ]; then
			rpm --quiet -i https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
		else
			rpm --quiet -i https://mirrors.kernel.org/fedora-epel/6/i386/epel-release-6-8.noarch.rpm
		fi
	fi
	
	echo "* Installing Falco public GPG key"
	rpm --quiet --import https://falco.org/repo/falcosecurity-3672BA8F.asc
	echo "* Installing Falco repository"
	curl -s -o /etc/yum.repos.d/falcosecurity.repo https://falco.org/repo/falcosecurity-rpm.repo
	echo "* Installing kernel headers"
	KERNEL_VERSION=$(uname -r)
	if [[ $KERNEL_VERSION == *PAE* ]]; then
		yum -q -y install "kernel-PAE-devel-${KERNEL_VERSION%.PAE}" || kernel_warning
	elif [[ $KERNEL_VERSION == *stab* ]]; then
		# It's OpenVZ kernel and we should install another package
		yum -q -y install "vzkernel-devel-$KERNEL_VERSION" || kernel_warning
	elif [[ $KERNEL_VERSION == *uek* ]]; then
		yum -q -y install "kernel-uek-devel-$KERNEL_VERSION" || kernel_warning
	else
		yum -q -y install "kernel-devel-$KERNEL_VERSION" || kernel_warning
	fi
	echo "* Installing Falco"
	yum -q -y install falco
}

function install_deb {
	export DEBIAN_FRONTEND=noninteractive

	if ! hash curl > /dev/null 2>&1; then
		echo "* Installing curl"
		apt-get -qq -y install curl < /dev/null
	fi

	echo "* Installing Falco public GPG key"
	curl -s https://falco.org/repo/falcosecurity-3672BA8F.asc | apt-key add -
	echo "* Installing Falco repository"
	echo "deb https://download.falco.org/packages/deb stable main" | tee /etc/apt/sources.list.d/falcosecurity.list
	apt-get -qq update < /dev/null
	echo "* Installing kernel headers"
	apt-get -qq -y install "linux-headers-$(uname -r)" < /dev/null || kernel_warning
	echo "* Installing Falco"
	apt-get -qq -y install falco < /dev/null
}

function install_suse {
	if ! hash curl > /dev/null 2>&1; then
		echo "* Installing curl"
		zypper -q -n install curl
	fi
	
	rpm --import https://falco.org/repo/falcosecurity-3672BA8F.asc
	curl -s -o /etc/zypp/repos.d/falcosecurity.repo https://falco.org/repo/falcosecurity-rpm.repo
	
	KERNEL_VERSION=$(uname -r)
	if [[ $KERNEL_VERSION == *PAE* ]]; then
		zypper -q -n install "kernel-PAE-devel-${KERNEL_VERSION%.PAE}" || kernel_warning
	elif [[ $KERNEL_VERSION == *stab* ]]; then
		# It's OpenVZ kernel and we should install another package
		zypper -q -n install "vzkernel-devel-$KERNEL_VERSION" || kernel_warning
	elif [[ $KERNEL_VERSION == *uek* ]]; then
		zypper -q -n install "kernel-uek-devel-$KERNEL_VERSION" || kernel_warning
	else
		zypper -q -n install kernel-default-devel || kernel_warning
	fi
	echo "* Installing Falco"
	zypper -q -n install falco
}


function unsupported {
	echo 'Unsupported operating system. Please consider writing to the mailing list at'
	echo 'cncf-falco-dev@lists.cncf.io or trying the manual'
	echo 'installation.'
	exit 1
}

function kernel_warning {
	echo "Unable to find kernel development files for the current kernel version $(uname -r)"
	echo "This usually means that your system is not up-to-date or you installed a custom kernel version."
	echo "The installation will continue but you'll need to install these yourself in order to use falco."
	echo "Please write to the mailing list at cncf-falco-dev@lists.cncf.io"
	echo "if you need further assistance."
}

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

echo "* Detecting operating system"

ARCH=$(uname -m)
if [[ ! $ARCH = *86 ]] && [ ! "$ARCH" = "x86_64" ] && [ ! "$ARCH" = "s390x" ]; then
	unsupported
fi

if [ "$ARCH" = "s390x" ]; then
	echo "------------"
	echo "WARNING: A Docker container is the only officially supported platform on s390x"
	echo "------------"
fi

if [ -f /etc/debian_version ]; then
	if [ -f /etc/lsb-release ]; then
		. /etc/lsb-release
		DISTRO=$DISTRIB_ID
		VERSION=${DISTRIB_RELEASE%%.*}
	else
		DISTRO="Debian"
		VERSION=$(cat /etc/debian_version | cut -d'.' -f1)
	fi

	case "$DISTRO" in

		"Ubuntu")
			if [ "$VERSION" -ge 10 ]; then
				install_deb
			else
				unsupported
			fi
			;;

		"LinuxMint")
			if [ "$VERSION" -ge 9 ]; then
				install_deb
			else
				unsupported
			fi
			;;

		"Debian")
			if [ "$VERSION" -ge 6 ]; then
				install_deb
			elif [[ $VERSION == *sid* ]]; then
				install_deb
			else
				unsupported
			fi
			;;

		*)
			unsupported
			;;

	esac

elif [ -f /etc/system-release-cpe ]; then
	DISTRO=$(cat /etc/system-release-cpe | cut -d':' -f3)

	# New Amazon Linux 2 distro
	if [[ -f /etc/image-id ]]; then
		AMZ_AMI_VERSION=$(cat /etc/image-id | grep 'image_name' | cut -d"=" -f2 | tr -d "\"")
	fi

	if [[ "${DISTRO}" == "o" ]] && [[ ${AMZ_AMI_VERSION} = *"amzn2"* ]]; then
		DISTRO=$(cat /etc/system-release-cpe | cut -d':' -f4)
	fi

	VERSION=$(cat /etc/system-release-cpe | cut -d':' -f5 | cut -d'.' -f1 | sed 's/[^0-9]*//g')

	case "$DISTRO" in

		"oracle" | "centos" | "redhat")
			if [ "$VERSION" -ge 6 ]; then
				install_rpm
			else
				unsupported
			fi
			;;

		"amazon")
			install_rpm
			;;

		"fedoraproject")
			if [ "$VERSION" -ge 13 ]; then
				install_rpm
			else
				unsupported
			fi
			;;

		*)
			unsupported
			;;

	esac
elif [ -z $(cat /etc/os-release  | grep ^SUSE) ]; then
	install_suse
else
	unsupported
fi
