#!/bin/bash
# KernelCare (https://kernelcare.com/) installation script v1.23
# Copyright (c) 2021 CloudLinux Inc. All Rights Reserved

set -e -o pipefail
cd /root/

#Custom package name
PACKAGE_NAME=${KCARE_PACKAGE_NAME:-"kernelcare"}

# Default repo could be redefined during installation
repo=${KCARE_REPO:-"https://repo.cloudlinux.com"}
if [ "$(uname -m)" = "aarch64" ]; then
    arm64_dist=y
fi

min_debian_ver=8
max_debian_ver=12

set_debian_version() {
    if [ -n "${arm64_dist}" ]; then
        DEBIAN_SUBREPO=kernelcare-ubuntu
        eval $(source /etc/os-release; echo DEBIAN_VERSION="$VERSION_ID"; echo DEBIAN_CODENAME="$VERSION_CODENAME")
        return
    fi

    DEBIAN_CODENAME=kcare
    DEBIAN_SUBREPO=kernelcare-debian
    declare -A debian_names=(
            ["squeeze/sid"]=6
            ["wheezy/sid"]=7
            ["jessie/sid"]=8
            ["stretch/sid"]=9
            ["buster/sid"]=10
            ["bullseye/sid"]=11
            ["bookworm/sid"]=12
    )

    if [[ ! -f /etc/debian_version ]]; then
            DEBIAN_VERSION=$max_debian_ver
            return
    fi

    DEBIAN_VERSION=$(cat /etc/debian_version)
    DEBIAN_VERSION="${DEBIAN_VERSION:-default}"
    DEBIAN_VERSION=${debian_names[$DEBIAN_VERSION]};

    if [[ -z "$DEBIAN_VERSION" ]]; then
            DEBIAN_VERSION=$max_debian_ver
    elif (( $DEBIAN_VERSION > $max_debian_ver )); then
            DEBIAN_VERSION=$max_debian_ver
    elif (( $DEBIAN_VERSION < $min_debian_ver )); then
            DEBIAN_VERSION=$min_debian_ver
    fi
}

DRY_RUN=
while getopts n option
do
case "${option}"
in
n) DRY_RUN=1;;
esac
done

if [ -e /etc/os-release ]; then
    rel=$(grep -w 'VERSION_ID' /etc/os-release | cut -d'.' -f1 | grep -oE "[0-9]*" || :)
    codename=$(grep -w 'VERSION_CODENAME' /etc/os-release | cut -d'.' -f1 || :)
    distr=$(grep -w 'ID' /etc/os-release | cut -d'=' -f2|tr -d '"' | xargs || :)
    distr_like=$(grep -w 'ID_LIKE' /etc/os-release | cut -d'=' -f2|tr -d '"' || :)

    case $distr in
        "debian" | "ubuntu" | "raspbian")
            DISTRO_FAMILY='deb'
        ;;
        "amzn" | "almalinux" | "rocky" | "centos" | "rhel" | "ol" | "cloudlinux" | "virtuozzo")
            DISTRO_FAMILY='rpm'
        ;;
    esac

    case $distr in
            "debian" | "raspbian")
                    DEBIAN_CODENAME=kcare
                    DEBIAN_SUBREPO=kernelcare-debian
                    if [ "$rel" -ge $max_debian_ver ]; then
                        DEBIAN_VERSION=$max_debian_ver
                    else
                        DEBIAN_VERSION="$rel"
                    fi
            ;;
            "ubuntu")
                    set_debian_version
            ;;
            "amzn")
                    # TODO: Remove this hack after amzn1 support dropped
                    [[ "${distr}" -eq "amzn" ]] && [[ "${rel}" -eq "2018" ]] && yum install -y python-argparse
            ;;
            "almalinux" | "rocky" | "centos" | "rhel" | "ol" | "cloudlinux" | "virtuozzo")
            ;;
            * )
        echo "WARNING: Sorry, this distro isn't supported yet. If you'd like it to be, let us know at feedback@cloudlinux.com."
        case $distr_like in
            *"ubuntu"*)
                DEBIAN_VERSION="$rel"
                DEBIAN_CODENAME="$codename"
                DEBIAN_SUBREPO="kernelcare-ubuntu"
                DISTRO_FAMILY='deb'
            ;;
            * )
                rel='7'
                DISTRO_FAMILY='rpm'
            ;;
        esac
            ;;
    esac
elif [ -e /etc/system-release ]; then
    rel=$(cut -d'.' -f1 /etc/system-release | grep -oE "[0-9]")
    DISTRO_FAMILY='rpm'
else
    rel=$(cut -d'.' -f1 /etc/redhat-release | grep -oE "[0-9]")
    DISTRO_FAMILY='rpm'
fi

download() {
    url="$1"
    fname="$2"
    if hash curl 2>/dev/null; then
        curl --fail -o "$fname" "$url"
    elif hash wget 2>/dev/null; then
        wget -O "$fname" "$url"
    else
        echo "Unable to install kernelcare: install curl or wget to continue."
        exit 1
    fi
}

# force default umask
umask 0022

case "${DISTRO_FAMILY}" in
    "deb")
        mkdir -p /usr/share/keyrings
        download "${repo}/kernelcare/kernelcare.gpg" /usr/share/keyrings/kcare.gpg
        chmod 644 /usr/share/keyrings/kcare.gpg
        if [[ $DEBIAN_SUBREPO == "kernelcare-debian" && $DEBIAN_VERSION -le 8 ]]; then
            apt-key add /usr/share/keyrings/kcare.gpg
            line_start="deb"
        else
            line_start="deb [signed-by=/usr/share/keyrings/kcare.gpg]"
        fi
        if [[ $DEBIAN_SUBREPO == "kernelcare-debian" ]]; then
            DEBIAN_SUBREPO=kernelcare/kernelcare-debian
        fi

        echo "$line_start $repo/$DEBIAN_SUBREPO/$DEBIAN_VERSION $DEBIAN_CODENAME main" > /etc/apt/sources.list.d/kcare.list
        if [ -n "${DRY_RUN}" ]; then
            exit 0
        fi
        apt-get update && apt-get install --no-install-recommends --yes $PACKAGE_NAME
    ;;
    "rpm")
        rpm --import ${repo}/kernelcare/RPM-GPG-KEY-KernelCare
        cat >/etc/yum.repos.d/kernelcare.repo <<EOL
[kernelcare]
name=kernelcare
baseurl=$repo/kernelcare/\$releasever/\$basearch
enabled=1
gpgcheck=1
gpgkey=$repo/kernelcare/RPM-GPG-KEY-KernelCare

EOL
        if [ -n "${DRY_RUN}" ]; then
            exit 0
        fi
        if [ "$rel" == "5" ]; then
            params=--nogpgcheck
        fi
        yum --disablerepo='*' --enablerepo=kernelcare clean all
        yum -y install ${PACKAGE_NAME}${KCARE_PACKAGE_VERSION} $params
    ;;
    *)
        echo "Unable to install kernelcare: this linux distribution is not rpm/deb capable."
        exit 1
    ;;
esac
