#!/bin/bash

TITLE="Space Client installer"
HEIGHT=18
WIDTH=82
HELLO="Welcome to Space Client installer"
ERROR="ERROR: Space Client installer must be run as root!"
SUCCESS="Installation of Space Client was successful!"
ERROR_INSTALL="ERROR: An error occurred during the installation of the program!"
REPO_KEY_URL="https://update.spacevm.ru/space-repo-key.gpg"

if [[ $1 == "dev" ]]; then
    REPO_URL="http://space-harbor.space.team/space-client-dev/linux"
    APT_REPO_URL="http://space-build.space.team/space-client-dev"
    APTRPM_REPO_URL="http://space-build.space.team apt-rpm/space-client-dev"
elif [[ $1 == "electron" ]]; then
    REPO_URL="http://space-harbor.space.team/space-client-electron/linux"
    APT_REPO_URL="http://space-build.space.team/space-client-electron"
    APTRPM_REPO_URL="http://space-build.space.team apt-rpm/space-client-electron"
else
    REPO_URL="https://update.spacevm.ru/space-client/linux"
    APT_REPO_URL="$REPO_URL/apt"
    APTRPM_REPO_URL="https://update.spacevm.ru space-client/linux/apt-rpm"
fi

which dialog > /dev/null && WINDOW=dialog; which whiptail > /dev/null && WINDOW=whiptail

if [ -n "$WINDOW" ]; then
    if [ "$EUID" -ne 0 ]; then
        $WINDOW --title  "$TITLE" --msgbox "$ERROR" $HEIGHT $WIDTH  2>/dev/null
        clear 2> /dev/null || :
        exit 1
    fi

    $WINDOW --title  "$TITLE" --msgbox "$HELLO" $HEIGHT $WIDTH  2>/dev/null

    clear 2> /dev/null || :

    # if dialog exists
    OS=$($WINDOW --title  "$TITLE" --menu  "Select your OS:" $HEIGHT $WIDTH 10 \
    "1" "Debian 9" \
    "2" "Debian 10" \
    "3" "Debian 11" \
    "4" "Ubuntu 16.04" \
    "5" "Ubuntu 18.04" \
    "6" "Ubuntu 20.04" \
    "7" "Ubuntu 22.04" \
    "8" "CentOS 7" \
    "9" "CentOS 8" \
    "10" "Astra Linux Orel 2.12" \
    "11" "Astra Linux SE 1.7" \
    "12" "ALT Linux 10" \
    "13" "RED OS 7.2" \
    "14" "RED OS 7.3" \
    "15" "AlterOS 7" 3>&1 1>&2 2>&3)


    clear 2> /dev/null || :

    if [ "$?" -ne 0 ]; then
        exit 1
    fi
else
    echo "Welcome to Space Client installer! (sudo required)"
    echo "Select your OS:"

    echo "
        1.  Debian 9
        2.  Debian 10
        3.  Debian 11
        4.  Ubuntu 16.04
        5.  Ubuntu 18.04
        6.  Ubuntu 20.04
        7.  Ubuntu 22.04
        8.  CentOS 7
        9.  CentOS 8
        10. Astra Linux Orel 2.12
        11. Astra Linux SE 1.7
        12. ALT Linux 10
        13. RED OS 7.2
        14. RED OS 7.3
        15. AlterOS 7
    "
    echo "My OS is:"
    read OS
fi

case $OS in
    1)
        apt-get update && apt-get install apt-transport-https wget lsb-release gnupg -y
        DISTR=$(lsb_release -cs)
        wget -qO - $REPO_KEY_URL | apt-key add -
        # add stretch-backports repo (for freerdp 2.0)
        echo "deb http://deb.debian.org/debian stretch-backports main" | tee /etc/apt/sources.list.d/stretch-backports.list
        echo "deb [arch=amd64] $APT_REPO_URL $DISTR main" | tee /etc/apt/sources.list.d/space-client.list
        apt-get update && apt-get install space-client -y
        result="$?"
        rm -f /etc/apt/sources.list.d/stretch-backports.list && apt-get update
        ;;
    3|4|5)
        apt-key del 4BD0CAA7
        apt-get update && apt-get install apt-transport-https wget lsb-release gnupg ca-certificates -y
        DISTR=$(lsb_release -cs)
        mkdir -p /usr/share/keyrings
        wget -O /usr/share/keyrings/space-repo-key.gpg $REPO_KEY_URL
        echo "deb [arch=amd64 signed-by=/usr/share/keyrings/space-repo-key.gpg] $APT_REPO_URL $DISTR main" | tee /etc/apt/sources.list.d/space-client.list
        apt-get update && apt-get install space-client -y
        result="$?"
        ;;
    2|6|7|11)
        # with loudplay-client
        apt-key del 4BD0CAA7
        apt-get update && apt-get install apt-transport-https wget lsb-release gnupg ca-certificates -y
        DISTR=$(lsb_release -cs)
        mkdir -p /usr/share/keyrings
        wget -O /usr/share/keyrings/space-repo-key.gpg $REPO_KEY_URL
        echo "deb [arch=amd64 signed-by=/usr/share/keyrings/space-repo-key.gpg] $APT_REPO_URL $DISTR main" | tee /etc/apt/sources.list.d/space-client.list
        apt-get update && apt-get install space-client -y
        apt-get install loudplay-client -y
        result="$?"
        ;;
    8|9)
        tee /etc/yum.repos.d/space-client.repo <<EOF
[space-client]
name=Space Client repository
baseurl=$REPO_URL/yum/el\$releasever/\$basearch
gpgcheck=1
gpgkey=$REPO_URL/yum/RPM-GPG-KEY-space-client
enabled=1
EOF

        yum install epel-release -y
        yum install -y https://download1.rpmfusion.org/free/el/updates/8/x86_64/r/rpmfusion-free-release-8-0.1.noarch.rpm
        yum install space-client -y
        result="$?"
        ;;
    10)
        apt-get update && apt-get install apt-transport-https wget gnupg lsb-release ca-certificates -y
        wget -qO - $REPO_KEY_URL | apt-key add -
        echo "deb [arch=amd64] $APT_REPO_URL bionic main" | tee /etc/apt/sources.list.d/space-client.list
        apt-get update && apt-get install space-client -y
        result="$?"
        ;;
    12)
        apt-get update && apt-get install apt-https -y
        echo "rpm $APTRPM_REPO_URL/x86_64 space-client" | tee /etc/apt/sources.list.d/space-client.list
        apt-get update
        apt-get install space-client -y
        result="$?"
        ;;
    13)
        tee /etc/yum.repos.d/space-client.repo <<EOF
[space-client]
name=Space Client repository
baseurl=$REPO_URL/yum/el7/\$basearch
gpgcheck=1
gpgkey=$REPO_URL/yum/RPM-GPG-KEY-space-client
enabled=1
EOF

        yum install space-client freerdp-libs -y
        result="$?"
        ;;

    14)
        tee /etc/yum.repos.d/space-client.repo <<EOF
[space-client]
name=Space Client repository
baseurl=$REPO_URL/yum/redos7.3/\$basearch
gpgcheck=1
gpgkey=$REPO_URL/yum/RPM-GPG-KEY-space-client
enabled=1
EOF

        dnf install space-client -y
        result="$?"
        ;;
    
    15)
        tee /etc/yum.repos.d/AlterOS-Experimental.repo <<EOF
[experimental]
name=AlterOS-\$releasever - experimental
baseurl=http://repo.alter-os.ru/alteros/\$releasever/experimental/\$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-AlterOS-7
enabled=1
EOF
        tee /etc/yum.repos.d/space-client.repo <<EOF
[space-client]
name=Space Client repository
baseurl=$REPO_URL/yum/alteros7/\$basearch
gpgcheck=1
gpgkey=$REPO_URL/yum/RPM-GPG-KEY-space-client
enabled=1
EOF

        # remove old freerdp
        if [[ $(rpm -qa | grep freerdp-libs) == "freerdp-libs-1.0.2-15.el7.alteros.x86_64" ]]; then
            yum erase -y freerdp-libs
        fi

        # install space-client
        yum install -y space-client
        result="$?"
        ;;

    *)
        echo "Error: Empty OS" ;;
esac

if [ "$result" -eq 0 ]; then
        echo -e "\n$TITLE\n\n$SUCCESS"
    else
        echo -e "\n$TITLE\n\n$ERROR_INSTALL"
fi