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

NIGHTLY=0
SUBREPO="tarantool-picodata"

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

# 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] [-r MAJOR]"
  echo "    -h  - help, this screen"
  echo "    -n  - use nightly repo"
  echo "    -r  - major version of Picodata (ex: 26)"
}

# for install over yum
repo_yum() {
  REPO=$OS
  [ "$1" != "" ] && REPO=$1
  $INSTALLER install -y curl
#  rpm -vvvv --import $GPGKEY_PREV
  rpm --import $GPGKEY
  if [ $NIGHTLY -eq 1 ]; then HOST=${HOST_NIGHTLY}; fi
  echo "
[picodata]
name=Picodata Yum Repo
baseurl=$HOST/$SUBREPO/$REPO/\$releasever/\$basearch
enabled=1
gpgcheck=1
" > /etc/yum.repos.d/picodata.repo

}

# for install in fedora
repo_yum_binary() {
  [ $# -ne 1 ] && exit 1
  REPO=$1
  if [ $NIGHTLY -eq 1 ]; then
    TYPE=NIGHTLY
  else
    TYPE=RELEASE
  fi
  echo "
[picodata]
name=Picodata Yum Repo
baseurl=https://binary.picodata.io/repository/yum/${REPO}/\$releasever/\$basearch/$TYPE
enabled=1
gpgcheck=0
" > /etc/yum.repos.d/picodata.repo
}

# for install over apt-get
repo_deb() {
  [ $# -ne 1 ] && exit 1
  REPO=$1
  apt-get update
  apt-get install -y --no-install-recommends 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" -o "${OS_VER}" = "bookworm" -o "${OS_VER}" = "trixie" -o "${OS_VER}" = "onyx3" ]; 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() {
  apt-get --fix-broken install -y
  apt-get update
  apt-get install -y gpg curl apt-https
  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
  if [ $NIGHTLY -eq 1 ]; then HOST=${HOST_NIGHTLY}; fi
  echo "
rpm [picodata] $HOST/$SUBREPO/altlinux/ $OS_VER/x86_64 main
rpm [picodata] $HOST/$SUBREPO/altlinux/ $OS_VER/x86_64 debuginfo
rpm-src [picodata] $HOST/$SUBREPO/altlinux/ $OS_VER/x86_64 main
" > /etc/apt/sources.list.d/picodata.list
  echo '
simple-key "picodata" {
       Fingerprint "2EB992F946D6AD71A035BC5B6F7E9F5C3F63892C";
       Name "kdy@picodata.io";
}
' > /etc/apt/vendors.list.d/picodata.list


  apt-get update
}

# need params for script:
#  -n   install nightly repo
#  -h   help screen
while getopts 'hnr:' param ; do
  case $param in
    n)
      # Enable nightly repo
      NIGHTLY=1
      ;;
    h)
      err_msg
      exit 0
      ;;
    r)
      [ $OPTARG -ge 26 ] && SUBREPO="$OPTARG"
      ;;
  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" &>/dev/null; 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="$(echo ${VERSION_CODENAME} | cut -f 1 -d '_')"
fi

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

case $OS in
  rhel)
    VER="$(echo $OS_VER | cut -b 1)"
    if ! echo $VER | grep [89] &>/dev/null; then
      err_msg "Version of this OS is not supported"
      exit 1
    fi
    if [ $VER -eq 9 ] ; then
      repo_yum_binary el
    else
      repo_yum el
    fi
    ;;
  redos)
    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
    repo_yum
    ;;
  rosa)
    repo_yum
    ;;
  ubuntu|debian)
    if [ $NIGHTLY -eq 1 ]; then HOST=${HOST_NIGHTLY}; fi
    if ! echo ${OS_VER} | grep -E "focal|jammy|noble|bullseye|bookworm|trixie" >/dev/null 2>/dev/null; then
      err_msg "Version of this OS is not supported"
      exit 1
    fi
    REPO="deb [arch=amd64] ${HOST}/${SUBREPO}/${OS}/ ${OS_VER} main"
    repo_deb "$REPO"
    ;;
  linuxmint)
    if [ $NIGHTLY -eq 1 ]; then HOST=${HOST_NIGHTLY}; fi
    if ! echo ${OS_VER} | grep -E "virginia" >/dev/null 2>/dev/null; then
      err_msg "Version of this OS is not supported"
      exit 1
    fi
    REPO="deb [arch=amd64] ${HOST}/${SUBREPO}/ubuntu/ jammy main"
    repo_deb "$REPO"
    ;;
  astra)
    if [ $NIGHTLY -eq 1 ]; then HOST=${HOST_NIGHTLY}; fi
    if ! echo ${OS_VER} | grep -E "1\.7|1\.8|orel" >/dev/null 2>/dev/null; then
      err_msg "Version of this OS is not supported"
      exit 1
    fi
    REPO="deb [arch=amd64] ${HOST}/${SUBREPO}/astra/ ${OS_VER} main"
    repo_deb "$REPO"
    ;;
  osnova)
    if [ $NIGHTLY -eq 1 ]; then HOST=${HOST_NIGHTLY}; fi
    if ! echo ${OS_VER} | grep -E "onyx3" >/dev/null 2>/dev/null; then
      err_msg "Version of this OS is not supported"
      exit 1
    fi
    REPO="deb [arch=amd64] ${HOST}/${SUBREPO}/onyx/ ${OS_VER} main"
    repo_deb "$REPO"
    ;;
  altlinux)
    if ! echo ${OS_VER} | grep -E "p9|p10|8.?|10.?|p11|11.?" &>/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"
    elif echo ${OS_VER} | grep -E "11.?" &>/dev/null; then
      OS_VER="p11"
    fi
    repo_alt
    ;;
  fedora)
    repo_yum_binary $OS
    ;;
  *)
    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

