#!/bin/bash
### Copyright 1999-2020. Plesk International GmbH. All rights reserved.

set -e

ai_url="http://autoinstall.plesk.com"
packages=(
  plesk-py27 plesk-py27-dnspython plesk-py27-ecdsa plesk-py27-paramiko plesk-py27-psycopg2
  plesk-py27-pyasn1 plesk-py27-pysmb plesk-py27-pyyaml
)

export DEBIAN_FRONTEND=noninteractive

psa_version=()
IFS=" " read -r -a psa_version <<< "$(cat /usr/local/psa/version)"

if [ "$PLESK_DEBUG" != "1" ] ; then
  yum_quiet="-q"
  apt_quiet="-qq"
fi

remove_debian_migrator_repository()
{
  rm -f /etc/apt/sources.list.d/plesk-migrator.list
}

remove_centos_migrator_repository()
{
  rm -f /etc/yum.repos.d/plesk-migrator.repo
}

remove_debian()
{
	remove_debian_migrator_repository
	[ -n "$(command -v apt-get)" ] && apt-get remove -y $apt_quiet "${packages[@]}"
}

remove_centos()
{
  remove_centos_migrator_repository
  [ -n "$(command -v yum)" ] && yum remove -y $yum_quiet "${packages[@]}"
}

if [ "$1" = "remove" ] ; then
  case "${psa_version[1]}${psa_version[2]}" in
      Debian*|Ubuntu*) remove_debian ;;
      CentOS*|RedHatel*) remove_centos ;;
      *) not_supported "${psa_version[1]} ${psa_version[2]}" ;;
  esac
  exit 0
fi

not_supported()
{
  echo "Sorry, your operating system $1 is not supported"
  exit 1
}

check_installed_debian()
{
  local has_missing_packages=0
  for package in "${packages[@]}"; do
    if ! dpkg -s "$package" > /dev/null
    then
      echo "Package $package was not installed" >&2
      has_missing_packages=1
    fi
  done
  exit $has_missing_packages
}

check_installed_centos()
{
  local has_missing_packages=0
  for package in "${packages[@]}"; do
    if ! rpm -q "$package" > /dev/null
    then
      echo "Package $package was not installed" >&2
      has_missing_packages=1
    fi
  done
  exit $has_missing_packages
}

bootstrap_debian()
{
  local plesk_can_manage_repos="$3"
  [[ $plesk_can_manage_repos ]] && remove_debian_migrator_repository

  local os_name="$1"
  local os_version="$2"
  local arch_spec=""
  case "$(uname -m)" in
    x86_64)  arch_spec="[arch=amd64]" ;;
    aarch64) arch_spec="[arch=arm64]" ;;
  esac
  local url_path="$4"
  [[ -z "$5" ]] || echo 'APT::Get::AllowUnauthenticated "true";' > "/etc/apt/apt.conf.d/plesk-migrator"

  echo "Bootstrapping dependencies for $os_name $os_version..."
  if [[ $plesk_can_manage_repos ]]; then
    plesk sbin repository --create --persistent	--source "deb $arch_spec $ai_url/$url_path $os_version all"
    plesk sbin package --install "${packages[@]}"
    plesk sbin repository --delete --persistent	--source "deb $arch_spec $ai_url/$url_path $os_version all"
  else
    echo "deb $arch_spec $ai_url/$url_path $os_version all" > /etc/apt/sources.list.d/plesk-migrator.list
    apt-get update $apt_quiet -o Dir::Etc::sourcelist="sources.list.d/plesk-migrator.list" \
      -o Dir::Etc::sourceparts="-" \
      -o APT::Get::List-Cleanup="0"
    apt-get install -y $apt_quiet -o APT::Install-Suggests=false -o APT::Install-Recommends=false "${packages[@]}"
  fi

  check_installed_debian
}

bootstrap_redhat()
{
  local plesk_can_manage_repos="$3"
  [[ $plesk_can_manage_repos ]] && remove_centos_migrator_repository

  local os_name="$1"
  local os_version="$2"
  local os_arch
  os_arch="$(uname -m)"
  [[ "$(uname -m)" =~ .*64$ ]] || os_arch='i386'
  local url_path="$4"

  echo "Bootstrapping dependencies for centos $os_version $os_arch..."
  if [[ $plesk_can_manage_repos ]]; then
      plesk sbin repository --create --persistent \
        --baseurl "$ai_url/$url_path/dist-rpm-$os_name-$os_version-$os_arch" \
        --gpg-key "$ai_url/plesk.gpg"
      plesk sbin package --install "${packages[@]}"
  else
     cat > /etc/yum.repos.d/plesk-migrator.repo <<EOF
[plesk-migrator]
name=Plesk packages for migrator
baseurl=$ai_url/$url_path/dist-rpm-$os_name-$os_version-$os_arch
enabled=1
gpgcheck=1
gpgkey=$ai_url/plesk.gpg
EOF
    yum install -y $yum_quiet "${packages[@]}"
  fi

  check_installed_centos
}

plesk_version=()
IFS="." read -r -a plesk_version <<< "${psa_version[0]}"
[[ ${plesk_version[0]} -ge 17 ]] && plesk_can_manage_repos=true || plesk_can_manage_repos=false
case "${psa_version[1]}${psa_version[2]}" in
  Debian6*) bootstrap_debian "debian" "squeeze" $plesk_can_manage_repos "debian/PMM_0.1.10" "EOL" ;;
  Debian7*) bootstrap_debian "debian" "wheezy" $plesk_can_manage_repos "debian/PMM_0.1.10" "EOL" ;;
  Debian8*) bootstrap_debian "debian" "jessie" $plesk_can_manage_repos "PMM_0.1.11" ;;
  Debian9*) bootstrap_debian "debian" "stretch" $plesk_can_manage_repos "PMM_0.1.11" ;;
  Debian10*) bootstrap_debian "debian" "buster" $plesk_can_manage_repos "PMM_0.1.11" ;;
  Debian11*) bootstrap_debian "debian" "bullseye" $plesk_can_manage_repos "PMM_0.1.11" ;;
  Debian12*) bootstrap_debian "debian" "bookworm" $plesk_can_manage_repos "PMM_0.1.11" ;;
  Ubuntu12.04) bootstrap_debian "ubuntu" "precise" $plesk_can_manage_repos "ubuntu/PMM_0.1.10" "EOL" ;;
  Ubuntu14.04) bootstrap_debian "ubuntu" "trusty" $plesk_can_manage_repos "ubuntu/PMM_0.1.10" "EOL" ;;
  Ubuntu16.04) bootstrap_debian "ubuntu" "xenial" $plesk_can_manage_repos "PMM_0.1.11" ;;
  Ubuntu18.04) bootstrap_debian "ubuntu" "bionic" $plesk_can_manage_repos "PMM_0.1.11" ;;
  Ubuntu20.04) bootstrap_debian "ubuntu" "focal" $plesk_can_manage_repos "PMM_0.1.11" ;;
  Ubuntu22.04) bootstrap_debian "ubuntu" "jammy" $plesk_can_manage_repos "PMM_0.1.11" ;;
  Ubuntu24.04) bootstrap_debian "ubuntu" "noble" $plesk_can_manage_repos "PMM_0.1.11" ;;
  CentOS6*|RedHatel6*) bootstrap_redhat "CentOS" "6" $plesk_can_manage_repos "PMM_0.1.11" ;;
  CentOS7*|RedHatel7*) bootstrap_redhat "CentOS" "7" $plesk_can_manage_repos "PMM_0.1.11" ;;
  CentOS8*|RedHatel8*) bootstrap_redhat "RedHat" "el8" $plesk_can_manage_repos "PMM_0.1.11" ;;
  RedHatel9*)          bootstrap_redhat "RedHat" "el9" $plesk_can_manage_repos "PMM_0.1.11" ;;
  *) not_supported "${psa_version[1]} ${psa_version[2]}" ;;
esac

echo "Done"
