#/bin/bash
set -e

export SPECIFIED_CLOUD_PROVIDER=volcedge

ELKEID_VERSION='1.8.2.26'

if command -v curl > /dev/null;then
    DOWNLOADER="curl -fsL -m 30 -o ${FILE_NAME}"
    GETTER="curl -sSfL -m 30"
elif command -v wget > /dev/null;then
    DOWNLOADER="wget -T 30 -t 1 -O ${FILE_NAME}"
    GETTER="wget -T 30 -t 1 -qO-"
else
    echo "[ERROR] no supported downloader, please install curl or wget"
    exit 1
fi

if [ -f /etc/debian_version ];then
    OS_FAMILY=debian
elif [ -f /etc/redhat-release ] || [ -f /etc/SuSE-release ] || cat /etc/os-release | grep -q 'Amazon Linux' || cat /etc/os-release | grep -qi 'opensuse';then
    OS_FAMILY=redhat
else
    echo "[ERROR] only support os family: rhel(rhel/centos/fedora/opensuse etc.) debian(debian/ubuntu etc.)"
    exit 1
fi

case $OS_FAMILY in
"debian")
  FILE_NAME="elkeid-agent-linux-debian-$(uname -m)-${ELKEID_VERSION}.deb"
  PM="dpkg"
  PM_ARGS="-i"
  set +e
  dpkg -l | grep -q proxima
  case $? in
  0)
    dpkg -P proxima || true
    echo "[INFO] remove installed proxima"
    ;;
  *) echo "[INFO] not deceted proxima" ;;
  esac
  dpkg -l | grep -q elkeid-agent
  case $? in
  0)
    INSTALLED=1
    INSTALLED_VERSION=$(dpkg-query --showformat='${Version}' --show elkeid-agent | awk -F '[-]' '{print$1}' | cut -b 3-)
    echo "[INFO] elkeid-agent is installed, version: " "${INSTALLED_VERSION}"
    ;;
  *) INSTALLED=0 ;;
  esac
  ;;
"redhat")
  FILE_NAME="elkeid-agent-linux-rhel-$(uname -m)-${ELKEID_VERSION}.rpm"
  PM="rpm"
  PM_ARGS="-ivh"
  set +e
  rpm -qa | grep -q proxima
  case $? in
  0)
    rpm -e proxima || true
    echo "[INFO] remove installed proxima"
    ;;
  *) echo "[INFO] not deceted proxima" ;;
  esac
  rpm -qa | grep -q elkeid-agent
  case $? in
  0)
    INSTALLED=1
    INSTALLED_VERSION=$(rpm -qa --qf "%{NAME} %{VERSION}\n" | grep elkeid-agent | awk '{print $2}')
    echo "[INFO] elkeid-agent is installed, version: " "${INSTALLED_VERSION}"
    ;;
  *) INSTALLED=0 ;;
  esac
  ;;
esac

set -e

if [ $INSTALLED -eq 1 ]; then
  if [ $ELKEID_VERSION == "$INSTALLED_VERSION" ]; then
    service elkeid-agent start
    echo "[INFO] elkeid-agent is running"
    exit 0
  else
    case $OS_FAMILY in
    redhat) PM_ARGS="-Uvh" ;;
    esac
    echo "[INFO] elkeid-agent version not matched, update from" "$INSTALLED_VERSION" "to" "$ELKEID_VERSION"
  fi
fi

if curl --version >/dev/null 2>&1; then
  DOWNLOAD="curl -f -s --connect-time 30 -O"
elif wget --version >/dev/null 2>&1; then
  DOWNLOAD="wget -T 30 -t 1 -O ${FILE_NAME}"
else
  echo "[ERROR] unsupported download tools, please install curl or wget"
  exit 1
fi
for i in "http://100.100.100.101:80/agent/"
do
  echo "[INFO] downloading installer from node $i..."
  echo ${DOWNLOAD} "${i}component/elkeid-agent/${FILE_NAME}"
  if ${DOWNLOAD} "${i}component/elkeid-agent/${FILE_NAME}"; then
    break
  fi
done
if [ ! -f "${FILE_NAME}" ]; then
  echo "[ERROR] download installer failed"
  exit 1
fi
echo "[INFO] installing..."
echo ${PM} "${PM_ARGS}" "${FILE_NAME}"
${PM} "${PM_ARGS}" "${FILE_NAME}"
case $? in
0) echo "[INFO] installed successfully" && exit 1 ;;
*) echo "[ERROR] installed failed" && exit 1 ;;
esac

rm -f ${FILE_NAME} || true
