#!/usr/bin/env bash
set -euo pipefail

# Windows is not supported by HawkStream
if [[ ${OS:-} = Windows_NT ]]; then
  echo 'error: Please install HawkStream using Windows Subsystem for Linux'
  exit 1
fi

# Reset
Color_Off=''

# Regular Colors
Red=''
Green=''
Dim='' # White

# Bold
Bold_White=''
Bold_Green=''

if [[ -t 1 ]]; then
  # Reset
  Color_Off='\033[0m' # Text Reset

  # Regular Colors
  Red='\033[0;31m'   # Red
  Green='\033[0;32m' # Green
  Dim='\033[0;2m'    # White

  # Bold
  Bold_Green='\033[1;32m' # Bold Green
  Bold_White='\033[1m'    # Bold White
fi

error() {
  echo -e "${Red}error${Color_Off}:" "$@" >&2
  exit 1
}

info() {
  echo -e "${Dim}$* ${Color_Off}"
}

info_bold() {
  echo -e "${Bold_White}$* ${Color_Off}"
}

success() {
  echo -e "${Green}$* ${Color_Off}"
}

success_bold() {
  echo -e "${Bold_Green}$* ${Color_Off}"
}

# check if the user is root
if [ "$EUID" -ne 0 ]; then
  error "Please run this script as root user"
fi

# check if user passed the API key
if [[ $# -lt 1 ]]; then
  info_bold "Please provide the API key as an argument"
  error "Usage: ./install.sh <API_KEY>"
fi

if [[ $# -gt 1 ]]; then
  error 'Too many arguments, only 1 is allowed, and it needs to be the API KEY.'
fi

info "Installing HawkStream Agent on this system"

# create directory for the Agent at /opt/hawkstream
mkdir -p /opt/hawkstream

# determine the architecture of the system
ARCH=$(uname -m)

info "Detected architecture: $ARCH"

# check if curl is installed
if ! [ -x "$(command -v curl)" ]; then
  error "Curl is not installed on this system, Please install curl and run this script again"
fi

# switch case to download the correct binary
case $ARCH in
x86_64)
  # check if AVX2 is supported
  if [[ $(grep "avx2" "/proc/cpuinfo") = '' ]]; then
    info "AVX2 is **NOT** supported on this system - downloading the non-AVX2 binary"
    # download the non-AVX2 binary
    curl --fail --location --progress-bar --output /opt/hawkstream/hawkstream https://get.hawkstream.app/stable/linux/amd64/hawkstream-agent-linux-amd64-noavx2
  else
    info "AVX2 is supported on this system - downloading the AVX2 binary"
    # download the AVX2 binary
    curl --fail --location --progress-bar --output /opt/hawkstream/hawkstream https://get.hawkstream.app/stable/linux/amd64/hawkstream-agent-linux-amd64-avx2
  fi
  ;;
aarch64)
  info "Downloading the ARM64 binary"
  # download the ARM64 binary
  curl --fail --location --progress-bar --output /opt/hawkstream/hawkstream https://get.hawkstream.app/stable/linux/arm64/hawkstream-agent-linux-arm64
  ;;
*)
  # unsupported architecture
  error "HawkStream does **NOT** support the architecture $ARCH"
  ;;
esac

# make the binary executable
chmod +x /opt/hawkstream/hawkstream

# unlink the binary if it already exists
if [ -L /usr/local/bin/hawkstream ]; then
  unlink /usr/local/bin/hawkstream
fi

# create a symlink to the binary
ln -s /opt/hawkstream/hawkstream /usr/local/bin/hawkstream

# check if systemd is installed
if ! [ -x "$(command -v systemctl)" ]; then
  info_bold "Systemd is not installed on this system"
  info_bold "Please start the agent manually using /opt/hawkstream/hawkstream"
  info_bold "Experimental: No systemd support"
  info_bold "Please refer to the documentation for more information"
  exit
fi

info "Configuring systemd service for HawkStream"

# download the systemd service file
curl --fail --location --progress-bar --output /etc/systemd/system/HawkStream.service https://get.hawkstream.app/stable/linux/HawkStream.service

# download the journalctl configuration file
curl --fail --location --progress-bar --output /etc/systemd/journald@HawkStream.conf https://get.hawkstream.app/stable/linux/journalctl.conf

# create the directory for the configuration file
mkdir -p /etc/hawkstream

# download the configuration file
curl --fail --location --progress-bar --output /etc/hawkstream/agent.conf https://get.hawkstream.app/stable/linux/agent.conf

# replace the API_KEY in the configuration file
sed -i "s/REPLACE_WITH_API_KEY/$1/g" /etc/hawkstream/agent.conf

# reload the systemd daemon
systemctl daemon-reload

info_bold "Starting HawkStream service"

# enable the service
systemctl enable HawkStream

# start the service
systemctl start HawkStream

# successfull installation message
success "HawkStream has been installed successfully"
success "Please refer to the documentation for more information"
