#!/bin/bash

if [ $(whoami) != 'root' ]; then
    echo "Must be root to run. Try using 'sudo'."
    exit 1;
fi

# Add the EPEL repository (for access to some packages required by DriveStrike)
yum install -y epel-release

# Add the DriveStrike repository. To do this portably, we first try to use dnf (is available on
# Fedora), then fall back to yum-config-manager (for RHEL and CentOS). If both of those methods
# fail, we create the repository file manually.
if which dnf; then
   dnf config-manager --add-repo https://app.drivestrike.com/static/yum/drivestrike.repo
else
    # Attempt to use yum-config-manager
    echo "dnf not found; attempting to use yum-config-manager instead..."
    yum install -y yum-utils
    yum-config-manager --add-repo https://app.drivestrike.com/static/yum/drivestrike.repo
    if [ $? -ne 0 ]; then
        # As a last resort, create the file manually
        echo "yum-config-manager failed; adding repository manually..."
        mkdir -p /etc/yum.repos.d
        cat <<EOF >> /etc/yum.repos.d/drivestrike.repo
[drivestrike]
name=DriveStrike Repository
baseurl=https://app.drivestrike.com/static/yum
enabled=1
gpgcheck=1
gpgkey=https://app.drivestrike.com/static/drivestrike.pubkey.txt
EOF
    fi
fi

# Install DriveStrike
yum install -y drivestrike

# Register DriveStrike and start the service
if [[ -f /usr/sbin/drivestrikerun.sh ]]; then
    /usr/sbin/drivestrikerun.sh
else
    /usr/bin/drivestrikerun.sh
fi
