#!/bin/bash
set -e

if [[ $EUID -ne 0 ]]; then
   echo "Этот скрипт должен быть выполнен от суперпользователя"
   exit 1
fi

# Detecting the OS
source /etc/os-release || true
if [[ -z "$ID" || -z "$VERSION_ID" ]]; then
    echo "Не удалось определить операционную систему"
    exit 1
fi

echo "Обнаруженная ОС: $ID $VERSION_ID"

REPOS_SOURCE=https://repo.nanodev.ru/r24.1

install_package() {
    PACKAGE_MANAGER=$1
    PACKAGE_NAME=$2
    $PACKAGE_MANAGER install $PACKAGE_NAME -y
}

make_repo_file() {
    DISTNAME=$1
    cat <<EOF > /etc/yum.repos.d/nanocad241.repo
[nanocad241]
name=nanoCAD 24.1 release builds
baseurl=$REPOS_SOURCE/$DISTNAME/
gpgcheck=1
gpgkey=$REPOS_SOURCE/$DISTNAME/repodata/repomd.xml.key
enabled=1
EOF
}

case "$ID $VERSION_ID" in
    "redos 7.3"*|"redos 8.0"*|"altlinux 10"*|"alteros 7"|"astra 1.7_x86-64"|"astra 1.8_x86-64"|"ubuntu 22.04"|"ubuntu 24.04"|"rosa 2021.1") SUPPORTED=true ;;
    *) SUPPORTED=false ;;
esac

if [[ $SUPPORTED != true ]]; then
    echo "Ваша операционная система не поддерживается"
    exit 1
fi

echo "Добавление репозитория nanoCAD 24.1..."

if [[ "$ID" == "altlinux" ]]; then
    echo "rpm ${REPOS_SOURCE}/Alt_p10/ x86_64 nanodev" > /etc/apt/sources.list.d/nanocad241.list
elif [[ "$ID $VERSION_ID" == "astra 1.8_x86-64" ]]; then
    wget --no-check-certificate -qO /tmp/nanosoft.key ${REPOS_SOURCE}/AstraLinuxSE_1.8/Release.key
    gpg --dearmor /tmp/nanosoft.key
    mv /tmp/nanosoft.key.gpg /etc/apt/trusted.gpg.d/ &>/dev/null || :
    wget -qO- ${REPOS_SOURCE}/AstraLinuxSE_1.8/nanosoft.asc > /etc/apt/trusted.gpg.d/nanosoft.asc
    echo "deb ${REPOS_SOURCE}/AstraLinuxSE_1.8  ./" > /etc/apt/sources.list.d/nanocad241.list
elif [[ "$ID $VERSION_ID" == "astra 1.7_x86-64" ]]; then
    wget ${REPOS_SOURCE}/AstraLinuxSE_1.7/Release.key --no-check-certificate -qO- | apt-key add
    echo "deb ${REPOS_SOURCE}/AstraLinuxSE_1.7/ ./" > /etc/apt/sources.list.d/nanocad241.list
elif [[ "$ID $VERSION_ID" == "redos 7.3"* ]]; then
    make_repo_file "redOS_7.3"
elif [[ "$ID $VERSION_ID" == "redos 8.0"* ]]; then
    make_repo_file "redOS_8.0"
elif [[ "$ID" == "rosa" ]]; then
    make_repo_file "ROSA_2021.1"
elif [[ "$ID" == "ubuntu" ]]; then
    wget --no-check-certificate -qO /tmp/nanosoft.key ${REPOS_SOURCE}/Ubuntu_$VERSION_ID/Release.key
    gpg --dearmor /tmp/nanosoft.key
    mv /tmp/nanosoft.key.gpg /etc/apt/trusted.gpg.d/ &>/dev/null || :
    wget -qO- ${REPOS_SOURCE}/Ubuntu_$VERSION_ID/nanosoft.asc > /etc/apt/trusted.gpg.d/nanosoft.asc
    echo "deb ${REPOS_SOURCE}/Ubuntu_$VERSION_ID /" > /etc/apt/sources.list.d/nanocad241.list
elif [[ "$ID" == "alteros" ]]; then
    make_repo_file "alterOS_7.5"
fi


if [[ "$ID" =~ ^(redos|rosa|sberlinux)$ ]]; then
    PACKAGE_MANAGER="dnf"
elif [[ "$ID" =~ ^(ubuntu|astra|altlinux|uncom)$ ]]; then
    apt-get update
    PACKAGE_MANAGER="apt-get"
elif [[ "$ID" =~ ^(alteros)$ ]]; then
    PACKAGE_MANAGER="yum"
fi

if [[ "$PACKAGE_MANAGER" == "apt-get" ]]; then
    AVAILABLE_PACKAGES=$(apt-cache search '^ncad(-digsig|-licsrv|24-1)$' | awk '{print $1}' | tr '\n' ' ') 
elif [[ "$PACKAGE_MANAGER" == "dnf" ]]; then
    AVAILABLE_PACKAGES=$(dnf list | grep -E "ncad(-digsig|-licsrv|24-1)\.x86_64" | sed 's/\.x86_64//' | awk '{print $1}' | tr '\n' ' ')
elif [[ "$PACKAGE_MANAGER" == "yum" ]]; then
    AVAILABLE_PACKAGES=$(yum list | grep -E "ncad(-digsig|-licsrv|24-1)\.x86_64" | sed 's/\.x86_64//' | awk '{print $1}' | tr '\n' ' ')
fi

read -rp "Желаете установить nanoCAD? [д/Н]: " choice </dev/tty

case "$choice" in
    [yYдД])
        install_package "$PACKAGE_MANAGER" "ncad-pgsql"
        install_package "$PACKAGE_MANAGER" "$AVAILABLE_PACKAGES"
        ;;
    *)
        echo "Чтобы установить nanoCAD, выполните следующие команды:"
        echo "    sudo $PACKAGE_MANAGER install ncad-pgsql"
        echo "    sudo $PACKAGE_MANAGER install $AVAILABLE_PACKAGES"
esac
