#!/bin/bash
set -u
set +e
set -o pipefail

DOWNLOADHOST=proxy.cps.ctcdn.cn
USERKEY=f415f809afe26a4e
GROUPIDS=68f0611f33762e4b9951bd25
VERSION=0.27.2-5
RUNENV=prod
CPU_ARCH="x86_64"
PKG_VERSION="rpm"
DOWNLOADURLPRE="http://${DOWNLOADHOST}/agent/download?target=https://ct-cps.xzoss.xstore.ctyun.cn/cps-agent/agent"

if [[ "${RUNENV}" == "dev" || "${RUNENV}" == "test" ]]; then
  DOWNLOADURLPRE=${DOWNLOADURLPRE}/test
fi

if [[ "${RUNENV}" == "pre" ]]; then
  DOWNLOADURLPRE=${DOWNLOADURLPRE}/pre-prod
fi

get_arch=`arch`
if [[ $get_arch =~ "x86_64" ]];then
  CPU_ARCH="x86_64"
elif [[ $get_arch =~ "aarch64" ]];then
  CPU_ARCH="arm64"
fi

if [ "$(command -v dpkg 2>/dev/null)" ]; then
  PKG_VERSION="deb"
elif [ "$(command -v rpm 2>/dev/null)" ]; then
  PKG_VERSION="rpm"
fi

if [ ${VERSION} == "latest" ]; then
  if [ ${PKG_VERSION} == "rpm" ]; then
    if [ ${CPU_ARCH} == "x86_64" ]; then
      DownloadUrl="${DOWNLOADURLPRE}/latest/cps-agent.x86_64.rpm"
    elif [ ${CPU_ARCH} == "arm64" ]; then
      DownloadUrl="${DOWNLOADURLPRE}/latest/cps-agent.aarch64.rpm"
    fi
  elif [ ${PKG_VERSION} == "deb" ]; then
    if [ ${CPU_ARCH} == "x86_64" ]; then
      DownloadUrl="${DOWNLOADURLPRE}/latest/cps-agent_amd64.deb"
    elif [ ${CPU_ARCH} == "arm64" ]; then
      DownloadUrl="${DOWNLOADURLPRE}/latest/cps-agent_arm64.deb"
    fi
  fi
else
  if [ ${PKG_VERSION} == "rpm" ]; then
    if [ ${CPU_ARCH} == "x86_64" ]; then
      DownloadUrl="${DOWNLOADURLPRE}/v${VERSION}/cps-agent-${VERSION}.x86_64.rpm"
    elif [ ${CPU_ARCH} == "arm64" ]; then
      DownloadUrl="${DOWNLOADURLPRE}/v${VERSION}/cps-agent-${VERSION}.aarch64.rpm"
    fi
  elif [ ${PKG_VERSION} == "deb" ]; then
    if [ ${CPU_ARCH} == "x86_64" ]; then
      DownloadUrl="${DOWNLOADURLPRE}/v${VERSION}/cps-agent_${VERSION}_amd64.deb"
    elif [ ${CPU_ARCH} == "arm64" ]; then
      DownloadUrl="${DOWNLOADURLPRE}/v${VERSION}/cps-agent_${VERSION}_arm64.deb"
    fi
  fi
fi

install_log_file=/var/log/cps-agent-install.log
function log()
{
   echo "$(date) $@" | tee -a ${install_log_file}
}

#log "host_name: `hostname`, USERKEY=${USERKEY}, GROUPIDS=${GROUPIDS}, VERSION=${VERSION}, RUNENV=${RUNENV}, CPU_ARCH=${CPU_ARCH}, PKG_VERSION=${PKG_VERSION}, DownloadUrl=${DownloadUrl}"
log "host_name: `hostname`, USERKEY=${USERKEY}, GROUPIDS=${GROUPIDS}, VERSION=${VERSION}, RUNENV=${RUNENV}, CPU_ARCH=${CPU_ARCH}, PKG_VERSION=${PKG_VERSION}"

trap 'rm -f "$TMPFILE"' EXIT
#TMPFILE=$(mktemp) || exit 1
TMPFILE=/opt/tmp.test
log "download begin..."
curl -o ${TMPFILE} --connect-timeout 10 -m 300 ${DownloadUrl} 2>&1 | tee -a  ${install_log_file}

if [ $? -ne 0 ]; then
  log "download failed... "
  exit 1
else
  file ${TMPFILE} | tee -a  ${install_log_file}
  fileInfo=`file -b ${TMPFILE}`
  if [ ${PKG_VERSION} == "rpm" ]; then
    # file xx.rpm: RPM v3.0 bin i386/x86_64
    if [[ "${fileInfo}" =~ "RPM" ]]; then
      log "download success..."
    else
      head -n 1 ${TMPFILE} | tee -a  ${install_log_file}
      log "download failed..."
      exit 1
    fi
  elif [ ${PKG_VERSION} == "deb" ]; then
    # file yy.deb: Debian binary package
    if [[ "${fileInfo}" =~ "Debian" ]]; then
      log "download success..."
    else
      head -n 1 ${TMPFILE} | tee -a  ${install_log_file}
      log "download failed..."
      exit 1
    fi
  fi
fi

log "install begin..."
#if [ ! -f "$file_path" ]; then
#   # 如果文件不存在
#   mkdir -p /usr/local/ctcwpp/cps-agent/ && uuidgen >  /usr/local/ctcwpp/cps-agent/machine-id
#fi

if [ ${PKG_VERSION} == "rpm" ]; then
  CPS_AGENT_AGENT_RUNENV=${RUNENV} CPS_AGENT_AGENT_USERKEY=${USERKEY} CPS_AGENT_AGENT_GROUPIDS=${GROUPIDS} \
    rpm -ivh ${TMPFILE} 2>&1 | tee -a  ${install_log_file}
elif [ ${PKG_VERSION} == "deb" ]; then
  CPS_AGENT_AGENT_RUNENV=${RUNENV} CPS_AGENT_AGENT_USERKEY=${USERKEY} CPS_AGENT_AGENT_GROUPIDS=${GROUPIDS} \
    dpkg -i ${TMPFILE} 2>&1 | tee -a  ${install_log_file}
fi

if [ $? -ne 0 ]; then
  log "install failed..."
  exit 1
else
  log "install success..."
  exit 0
fi
