#!/bin/bash

if [ -z "$1" ]; then
    exit 1
fi

platform=`cat /etc/windscribe/platform`
if [ -z $platform ]; then
    # should not be here
    platform="linux_deb_x64"
fi

if [ $platform == "linux_deb_x64" -o $platform == "linux_deb_arm64" ]; then
    pkexec sh -c "killall -q -w Windscribe && APT_LISTBUGS_FRONTEND=none apt install -y --reinstall \"$1\""
elif [ $platform == "linux_rpm_x64" -o $platform == "linux_rpm_arm64" ]; then
    if command -v rpm-ostree; then
        killall -q -w Windscribe && rpm-ostree upgrade --cache-only --reboot --uninstall windscribe --install "$1"
    else
        pkexec sh -c "killall -q -w Windscribe && dnf upgrade --cacheonly -y \"$1\""
    fi
elif [ $platform = "linux_rpm_opensuse_x64" ]; then
    if [ -f /usr/sbin/transactional-update ]; then
        pkexec sh -c "killall -q -w Windscribe && transactional-update -n pkg install --allow-unsigned-rpm \"$1\"; reboot"
    else
        pkexec sh -c "killall -q -w Windscribe && zypper --non-interactive install --force --allow-unsigned-rpm \"$1\""
    fi
elif [ $platform == "linux_zst_x64" ]; then
    pkexec sh -c "killall -q -w Windscribe && pacman -U --noconfirm \"$1\""
fi

nohup /opt/windscribe/Windscribe &>/dev/null & disown;
