#!/bin/sh
# Enable picodata repo for all supported os
# Author: Dmitry Kibirev <kdy@picodata.io>

NIGHTLY=0

# for releases
HOST="https://download.picodata.io"
# for nightly builds
HOST_NIGHTLY="https://download.binary.picodata.io"

# package for rpm-based distrs
RPM_RELEASE="picodata-release-1.1.3.0-1"

# package for Altlinux distrs
ALT_RELEASE="picodata-release-1.0.3.0-1"

# official gpg public key for repos
GPGKEY="${HOST}/tarantool-picodata/picodata.gpg.key"
GPGKEY_PREV="${HOST}/tarantool-picodata/picodata.gpg.1.key"

INSTALLER="apt-get"

# Error and help messages
err_msg() {
  [ $# -ne 0 ] && (echo "Error: $@"; echo)
  echo "Run: $0 [-h] [-n]"
  echo "    -h  - help, this screen"
  echo "    -n  - use nightly repo"
}

# for install over yum
repo_yum() {
  [ $# -ne 1 ] && exit 1
  RPM=$1
  $INSTALLER install -y curl
#  rpm -vvvv --import $GPGKEY_PREV
  rpm --import $GPGKEY
  $INSTALLER install -y $RPM
  if [ $NIGHTLY -eq 1 ]; then
    sed -i "s|${HOST}|${HOST_NIGHTLY}|g" /etc/yum.repos.d/picodata.repo
  fi
}

# for install over apt-get
repo_deb() {
  [ $# -ne 1 ] && exit 1
  REPO=$1
  apt-get update
  apt-get install -y gpg curl apt-transport-https software-properties-common
  curl -s $GPGKEY | gpg --no-default-keyring --keyring gnupg-ring:/etc/apt/trusted.gpg.d/picodata.gpg --import
#  curl -s $GPGKEY_PREV | gpg --no-default-keyring --keyring gnupg-ring:/etc/apt/trusted.gpg.d/picodata.gpg --import
  chmod 644 /etc/apt/trusted.gpg.d/picodata.gpg

  if [ "$OS" = "astra" ]; then
    echo "$REPO" > /etc/apt/sources.list.d/picodata.list
  else
    add-apt-repository -y "$REPO"
  fi

  apt-get update
}

# for install in altlinux
repo_alt() {
  [ $# -ne 1 ] && exit 1
  RPM=$1
  apt-get --fix-broken install -y
  apt-get update
  apt-get install -y gpg curl apt-https
  curl -SLO $RPM
  apt-get install -y ./$(basename $RPM)
  if [ $NIGHTLY -eq 1 ]; then
    sed -i "s|${HOST}|${HOST_NIGHTLY}|g" /etc/apt/sources.list.d/picodata.list
  fi
  curl -s $GPGKEY | gpg --no-default-keyring --keyring gnupg-ring:/usr/lib/alt-gpgkeys/pubring.gpg --import
#  curl -s $GPGKEY_PREV | gpg --no-default-keyring --keyring gnupg-ring:/usr/lib/alt-gpgkeys/pubring.gpg --import
  apt-get update
}

# need params for script:
#  -n   install nightly repo
#  -h   help screen
while getopts 'hn' param ; do
  case $param in
    n)
      # Enable nightly repo
      NIGHTLY=1
      ;;
    h)
      err_msg
      exit 0
  esac
done

# need root permissions for use
if [ $(id -u) -ne 0 ] ;then
  err_msg "root permissions required!"
  exit 1
fi

# /etc/os-release is standard file for define OS and version
if [ -f /etc/os-release ]; then
  . /etc/os-release

    # define OS family
  OS="$ID"
  # all rhel-base OS
  # if echo "$ID_LIKE"| grep "rhel" &>/dev/null; then
  if echo "$ID_LIKE"| grep "rhel"; then
    OS="rhel"
    INSTALLER="yum"
  fi

  # for redos separately repo
  if [ "$ID" = "redos" -o "$ID" = "rosa" -o "$ID" = "fedora" ]; then
    OS="$ID"
    INSTALLER="dnf"
  fi

  # define OS version
  OS_VER="$VERSION_ID"
  # if debian based distrs then use codename ex: focal, jammy, bullseye
  [ "$VERSION_CODENAME" != "" ] && OS_VER="$VERSION_CODENAME"
fi

echo "OS_FAMILY=$OS VER=$OS_VER NIGHTLY=$NIGHTLY"
echo ---

case $OS in
  rhel)
    VER="$(echo $OS_VER | cut -b 1)"
    if ! echo $VER | grep [78] &>/dev/null; then
      err_msg "Version of this OS is not supported"
      exit 1
    fi
    PKG="${HOST}/tarantool-picodata/el/${VER}/x86_64/${RPM_RELEASE}.el${VER}.x86_64.rpm"
    repo_yum "$PKG"
    ;;
  redos)
    VER="$(echo $OS_VER | cut -b 1)"
    if ! echo $VER | grep [7] &>/dev/null; then
      err_msg "Version of this OS is not supported"
      exit 1
    fi
    PKG="${HOST}/tarantool-picodata/redos/${VER}/x86_64/${RPM_RELEASE}.el${VER}.x86_64.rpm"
    repo_yum "$PKG"
    ;;
  rosa)
    PKG="${HOST}/tarantool-picodata/rosa/chrome/x86_64/${RPM_RELEASE}-rosa2021.1.x86_64.rpm"
    repo_yum "$PKG"
    ;;
  ubuntu|debian)
    if [ $NIGHTLY -eq 1 ]; then HOST=${HOST_NIGHTLY}; fi
    if ! echo ${OS_VER} | grep -E "focal|jammy|bullseye" >/dev/null 2>/dev/null; then
      err_msg "Version of this OS is not supported"
      exit 1
    fi
    REPO="deb [arch=amd64] ${HOST}/tarantool-picodata/${OS}/ ${OS_VER} main"
    repo_deb "$REPO"
    ;;
  astra)
    if [ $NIGHTLY -eq 1 ]; then HOST=${HOST_NIGHTLY}; fi
    REPO="deb [arch=amd64] ${HOST}/tarantool-picodata/astra/ orel main"
    repo_deb "$REPO"
    ;;
  altlinux)
    if ! echo ${OS_VER} | grep -E "p9|p10|8.?|10.?" &>/dev/null; then
      err_msg "Version of this OS is not supported"
      exit 1
    fi
    if echo ${OS_VER} | grep -E "8.?" &>/dev/null; then
      OS_VER="p9"
    elif echo ${OS_VER} | grep -E "10.?" &>/dev/null; then
      OS_VER="p10"
    fi
    PKG="${HOST}/tarantool-picodata/altlinux/${OS_VER}/${ALT_RELEASE}.${OS_VER}.x86_64.rpm"
    repo_alt "$PKG"
    ;;
  fedora)
    PKG="${HOST}/tarantool-picodata/fedora/${OS_VER}/x86_64/${RPM_RELEASE}.fc${OS_VER}.x86_64.rpm"
    repo_yum "$PKG"
    ;;
  *)
    err_msg "This OS is not supported!"
    exit 1
esac

echo
echo "For install picodata run command (under root or use sudo):"
echo "    $INSTALLER install -y picodata"
echo
