#!/bin/sh

platform="unknown"
issue=$(head -n 1 /etc/issue)

case "$issue" in
LMDE*)
        platform="lmde"
        ;;
*)
        ;;
esac

echo "platform detected - $platform"

if [ "$platform" = "unknown" ]; then
        echo "unknown platform, aborting script..."
        exit 1
fi

if [ ! -f /etc/apt/trusted.gpg.d/repo.smartplasma.ru.asc ]; then
        echo "RG repository key does not exist, installing..."
        sudo wget http://repo.smartplasma.ru/repo.smartplasma.ru.asc -O /etc/apt/trusted.gpg.d/repo.smartplasma.ru.asc
fi

need_add_repo=0

if [ ! -f /etc/apt/sources.list.d/additional-repositories.list ]; then
        echo "additional repositories list absent, populating with RG repo..."
        need_add_repo=1
else
        vendor_repo=$(grep repo.smartplasma.ru /etc/apt/sources.list.d/additional-repositories.list)
        if [ -z "$vendor_repo" ]; then
                echo "RG repo is not in the additional repositories list, adding..."
                need_add_repo=1
        fi
fi

if [ $need_add_repo -eq 1 ]; then
        echo "deb http://repo.smartplasma.ru/${platform}2/ rg main" | sudo tee -a /etc/apt/sources.list.d/additional-repositories.list
fi

sudo sed -i -e 's#https://#http://#' /etc/apt/sources.list.d/official-package-repositories.list

sudo apt update
sudo apt dist-upgrade -y

platform_meta_check=$(aptitude search -q -F '%p' "rg-cnc-meta-${platform}?installed")
if [ -z "$platform_meta_check" ]; then
        echo "RG meta-package is not installed, installing now..."
        sudo apt install -y rg-cnc-meta-${platform}
fi
