#!/bin/sh
set -e
#set -x

RECEIVER="domain receiver.24mon.io"

dist=""
dist_version=""
dist_version_family=""
dist_pretty=""

check_distro() {
    if [  -f /etc/os-release  ]; then
        dist=$(awk -F= '$1 == "ID" {gsub("\"", ""); print$2}' /etc/os-release)
        dist_version=$(awk -F= '$1 == "VERSION_ID" {gsub("\"", ""); print$2}' /etc/os-release)
        dist_pretty=$(awk -F= '$1 == "PRETTY_NAME" {gsub("\"", ""); print$2}' /etc/os-release)
    elif [ -f /etc/redhat-release ]; then
        dist=$(awk '{print tolower($1)}' /etc/redhat-release)
        dist_version=$(awk '{print tolower($3)}' /etc/redhat-release)
        dist_pretty=$(awk '{print$1" "$3}' /etc/redhat-release)
    elif [ $(uname -a | grep FreeBSD -c) -gt 0 ]; then
        echo "Freebsd?"
        dist=freebsd
        dist_version=$(uname -a | awk '{print $3}' |awk -F \. '{print $1}')
        dist_pretty=$(uname -a | awk '{print $1 " " $3}')
    else
        cat <<-EOF

  Automated installation is not supported on distributions
  without /etc/os-release or /etc/redhat-release.

EOF
        exit 1
    fi

    dist=${dist?unable to determine the distribution}
    dist_version=${dist_version?unable to determine distribution version}
    dist_version_family=$(echo ${dist_version} | awk -F. '{print$1}')
    dist_version_family=${dist_version_family?failed to determine the distribution major number}
}

command_exists() {
  command -v "$@" > /dev/null 2>&1
}


not_supported() {
  cat <<-EOF

 ${dist_pretty} is not supported at this time.

 Scripted installation only supported on:
    CentOS 6 and later
    Debian 7 and later
    Ubuntu 14.04 and later

EOF
  exit 1
}

write_centos_repo() {
  $sh_c '
cat > /etc/yum.repos.d/ITSumma.repo <<"EOM"
[itsumma]
name=ITSumma
baseurl=https://repo2.itsumma.com/centos/$releasever/$basearch/
enabled=1
gpgkey=https://repo2.itsumma.com/itsumma.asc
gpgcheck=1
EOM

rpm --import https://repo2.itsumma.com/itsumma.asc
'
}

write_centos_repo2() {
  $sh_c '
cat > /etc/yum.repos.d/ITSumma2.repo <<"EOM"
[itsumma2]
name=ITSumma
baseurl=https://repo2.itsumma.com/centos/$releasever/$basearch/
enabled=1
gpgkey=https://repo2.itsumma.com/itsumma.asc
gpgcheck=1
EOM

rpm --import https://repo2.itsumma.com/itsumma.asc
'
}

write_common_amzn_repo() {
  $sh_c '
cat > /etc/yum.repos.d/ITSumma.repo <<"EOM"
[itsumma]
name=ITSumma
baseurl=https://repo2.itsumma.com/centos/8/$basearch/
enabled=1
gpgkey=https://repo2.itsumma.com/itsumma.asc
gpgcheck=1
EOM

rpm --import https://repo2.itsumma.com/itsumma.asc
'
}

write_common_rhel_repo() {
  $sh_c '
cat > /etc/yum.repos.d/ITSumma.repo <<"EOM"
[itsumma]
name=ITSumma
baseurl=https://repo2.itsumma.com/centos/6/x86_64/
enabled=1
gpgkey=https://repo2.itsumma.com/itsumma.asc
gpgcheck=1
EOM

rpm --import https://repo2.itsumma.com/itsumma.asc
'
}


write_apt_repo() {
    $sh_c '
export DEBIAN_FRONTEND=noninteractive
apt-get -qqy update
apt-get -y install apt-transport-https curl gnupg
curl -sSL https://repo2.itsumma.com/itsumma.asc | apt-key add -
'
## DEBIAN
   if [ "$dist" = "debian" ] && [ "$dist_version_family" -eq 7 ]; then
    $sh_c '
cat > /etc/apt/sources.list.d/itsumma.list <<EOF
deb https://repo2.itsumma.com/debian wheezy main
EOF
'
   fi

   if [ "$dist" = "debian" ] && [ "$dist_version_family" -eq 8 ]; then
      $sh_c '
cat > /etc/apt/sources.list.d/itsumma.list <<EOF
deb https://repo2.itsumma.com/debian jessie main
EOF
'
   fi

   if [ "$dist" = "debian" ] && [ "$dist_version_family" -eq 9 ]; then
    $sh_c '
cat > /etc/apt/sources.list.d/itsumma.list <<EOF
deb https://repo2.itsumma.com/debian stretch main
EOF
'
   fi

   if [ "$dist" = "debian" ] && [ "$dist_version_family" -eq 10 ]; then
    $sh_c '
cat > /etc/apt/sources.list.d/itsumma.list <<EOF
deb https://repo2.itsumma.com/debian buster main
EOF
'
   fi

   if [ "$dist" = "debian" ] && [ "$dist_version_family" -eq 11 ]; then
    $sh_c '
cat > /etc/apt/sources.list.d/itsumma.list <<EOF
deb https://repo2.itsumma.com/debian bullseye main
EOF
'
   fi

   if [ "$dist" = "debian" ] && [ "$dist_version_family" -eq 12 ]; then
    $sh_c '
curl -sSL https://repo2.itsumma.com/itsumma.gpg -o  /usr/share/keyrings/itsumma.gpg
cat > /etc/apt/sources.list.d/itsumma.list <<EOF
deb [signed-by=/usr/share/keyrings/itsumma.gpg] https://repo2.itsumma.com/debian bookworm main
EOF
'
   fi

## UBUNTU
   if [ "$dist" = "ubuntu" ] && [ "$dist_version_family" -eq 14 ]; then
    $sh_c '
cat > /etc/apt/sources.list.d/itsumma.list <<EOF
deb https://repo2.itsumma.com/ubuntu trusty main
EOF
'
   fi

   if [ "$dist" = "ubuntu" ] && [ "$dist_version_family" -eq 16 ]; then
    $sh_c '
cat > /etc/apt/sources.list.d/itsumma.list <<EOF
deb https://repo2.itsumma.com/ubuntu xenial main
EOF
'
   fi

   if [ "$dist" = "ubuntu" ] && [ "$dist_version_family" -eq 17 ]; then
    $sh_c '
cat > /etc/apt/sources.list.d/itsumma.list <<EOF
deb https://repo2.itsumma.com/ubuntu xenial main
EOF
'
    fi

   if [ "$dist" = "ubuntu" ] && [ "$dist_version_family" -eq 18 ]; then
    $sh_c '
cat > /etc/apt/sources.list.d/itsumma.list <<EOF
deb https://repo2.itsumma.com/ubuntu bionic main
EOF
'
   fi

   if [ "$dist" = "ubuntu" ] && [ "$dist_version_family" -eq 19 ]; then
    $sh_c '
cat > /etc/apt/sources.list.d/itsumma.list <<EOF
deb https://repo2.itsumma.com/ubuntu bionic main
EOF
'
   fi

   if [ "$dist" = "ubuntu" ] && [ "$dist_version_family" -eq 20 ]; then
    $sh_c '
cat > /etc/apt/sources.list.d/itsumma.list <<EOF
deb https://repo2.itsumma.com/ubuntu focal main
EOF
'
   fi

   if [ "$dist" = "ubuntu" ] && [ "$dist_version_family" -eq 22 ]; then
    $sh_c '
curl -sSL https://repo2.itsumma.com/itsumma.gpg -o  /usr/share/keyrings/itsumma.gpg
cat > /etc/apt/sources.list.d/itsumma.list <<EOF
deb [signed-by=/usr/share/keyrings/itsumma.gpg] https://repo2.itsumma.com/ubuntu jammy main
EOF
'
   fi

   if [ "$dist" = "ubuntu" ] && [ "$dist_version_family" -eq 23 ]; then
    $sh_c '
curl -sSL https://repo2.itsumma.com/itsumma.gpg -o  /usr/share/keyrings/itsumma.gpg
cat > /etc/apt/sources.list.d/itsumma.list <<EOF
deb [signed-by=/usr/share/keyrings/itsumma.gpg] http://repo2.itsumma.com/ubuntu lunar main
EOF
'
   fi

   if [ "$dist" = "ubuntu" ] && [ "$dist_version_family" -eq 24 ]; then
    $sh_c '
curl -sSL https://repo2.itsumma.com/itsumma.gpg -o  /usr/share/keyrings/itsumma.gpg
cat > /etc/apt/sources.list.d/itsumma.list <<EOF
deb [signed-by=/usr/share/keyrings/itsumma.gpg] https://repo2.itsumma.com/ubuntu noble main
EOF
'
   fi
}

install_yum() {
  $sh_c 'yum install -y essm'
}

install_dnf() {
  $sh_c 'dnf install -y essm'
}

install_apt() {
  write_apt_repo
  $sh_c '
apt-get -qqy update
apt-get -y install essm
'
}

install_freebsd() {
  $sh_c '
  pkg add -f https://repo2.itsumma.com/freebsd/essm/essm-0.4.8_1.txz
'
}

essm_start() {
    echo "Starting essm..."
    $sh_c 'service essm start'
}

essm_install() {
  user="$(id -un 2>/dev/null || true)"

  sh_c='sh -c'
  if [ "$user" != 'root' ]; then
    if command_exists sudo; then
      sh_c='sudo -E sh -c'
    elif command_exists su; then
      sh_c='su -c'
    else
      cat >&2 <<-'EOF'
      Error: this installer needs the ability to run commands as root.
      We are unable to find either "sudo" or "su" available to make this happen.
EOF
      exit 1
    fi
  fi

  check_distro

  echo "Detected ${dist_pretty}..."
  case "$dist" in

    centos|redos|almalinux|rocky)
      if [ "$dist_version_family" -ge 8 ]; then
        write_centos_repo2
        install_dnf
        essm_start
        exit 0
      elif [ "$dist_version_family" -ge 6 ]; then
        write_centos_repo
        install_yum
        essm_start
        exit 0
      fi
      not_supported
    ;;

    fedora)
      if [ "$dist_version_family" -ge 34 ]; then
        write_common_amzn_repo
        install_yum
        essm_start
        exit 0
      fi
      not_supported
    ;;

    amzn|amzn2)
      # Amazon Linux
      write_common_amzn_repo
      install_yum
      essm_start
      exit 0
    ;;

    rhel|ol)
      write_centos_repo2
#      write_common_rhel_repo
      install_yum
      essm_start
      exit 0
    ;;

    debian)
      if [ "$dist_version_family" -ge 7 ]; then
        install_apt
        essm_start
        exit 0
      fi
      echo "why? " $dist_version_family
      not_supported
    ;;

    ubuntu)
      if [ "$dist_version_family" -ge 14 ]; then
        install_apt
        essm_start
        exit 0
      fi
      not_supported
    ;;

    freebsd)
      if [ "$dist_version_family" -ge 10 ]; then
        install_freebsd
        essm_start
        exit 0
      fi
      not_supported
    ;;

    *)
      not_supported
    ;;
  esac

}

essm_install

