#!/bin/bash

#distro=$(lsb_release -ds)
distro=$(cat /usr/share/doc/megacmd/distro)
#ver=$(lsb_release -rs)
ver=$(cat /usr/share/doc/megacmd/version)

if [[ $distro = "Raspbian GNU/Linux 12"* ]]; then
    str="Raspbian_12"
elif [[ $distro = "Raspbian GNU/Linux 11"* ]]; then
    str="Raspbian_11"
elif [[ $distro = "Raspbian GNU/Linux 10"* ]]; then
    str="Raspbian_10.0"
elif [[ $distro = "Raspbian GNU/Linux 9"* ]]; then
    str="Raspbian_9.0"
elif [[ $distro = "Raspbian GNU/Linux 8"* ]]; then
    str="Raspbian_8.0"
# Ubuntu
elif [[ $ver = "24.04" ]]; then
    str="xUbuntu_24.04"
elif [[ $ver = "23.10" ]]; then
    str="xUbuntu_23.10"
elif [[ $ver = "23.04" ]]; then
    str="xUbuntu_23.04"
elif [[ $ver = "22.10" ]]; then
    str="xUbuntu_22.10"
elif [[ $ver = "22.04" ]]; then
    str="xUbuntu_22.04"
elif [[ $ver = "21.10" ]]; then
    str="xUbuntu_21.10"
elif [[ $ver = "21.04" ]]; then
    str="xUbuntu_21.04"
elif [[ $ver = "20.10" ]]; then
    str="xUbuntu_20.10"
elif [[ $ver = "20.04" ]]; then
    str="xUbuntu_20.04"
elif [[ $ver = "19.10" ]]; then
    str="xUbuntu_19.10"
elif [[ $ver = "19.04" ]]; then
    str="xUbuntu_19.04"
elif [[ $ver = "18.10" ]]; then
    str="xUbuntu_18.10"
elif [[ $ver = "18.04" ]]; then
    str="xUbuntu_18.04"
elif [[ $ver = "17.10" ]]; then
    str="xUbuntu_17.10"
elif [[ $ver = "17.04" ]]; then
    str="xUbuntu_17.04"
elif [[ $ver = "16.10" ]]; then
    str="xUbuntu_16.10"
elif [[ $ver = "16.04" ]]; then
    str="xUbuntu_16.04"
elif [[ $ver = "15.10" ]]; then
    str="xUbuntu_15.10"
elif [[ $ver = "15.04" ]]; then
    str="xUbuntu_15.04"
elif [[ $ver = "14.10" ]]; then
    str="xUbuntu_14.10"
elif [[ $ver = "14.04" ]]; then
    str="xUbuntu_14.04"
elif [[ $ver = "13.10" ]]; then
    str="xUbuntu_13.10"
elif [[ $ver = "12.10" ]]; then
    str="xUbuntu_12.10"
elif [[ $ver = "12.04" ]]; then
    str="xUbuntu_12.04"
# Debian
elif [[ $ver == 6.* ]]; then
    str="Debian_6.0"
elif [[ $ver == 7.* ]]; then
    str="Debian_7.0"
elif [[ $ver == 8.* ]]; then
    str="Debian_8.0"
elif [[ $ver == 9.* ]]; then
    str="Debian_9.0"
elif [[ $ver = 10* ]]; then
    str="Debian_10.0"
elif [[ $ver = 11* ]]; then
    str="Debian_11"
elif [[ $ver = 12* ]]; then
    str="Debian_12"
elif [[ $ver = "testing" ]]; then
    str="Debian_testing"
# Elementary OS Luna uses 12.04
elif [[ $distro = "elementary OS Luna" ]]; then
    str="xUbuntu_12.04"
# Elementary OS Freya Ubuntu 14.04 LTS
elif [[ $distro = "elementary OS Freya" ]]; then
    str="xUbuntu_14.04"
# Linux Mint 13 Maya LTS (Ubuntu 12.04)
elif [[ $distro = "Linux Mint 13 Maya" ]]; then
    str="xUbuntu_12.04"
# Linux Mint 17.1 Rebecca LTS (Ubuntu 14.04)
elif [[ $distro = "Linux Mint 17.1 Rebecca" ]]; then
    str="xUbuntu_14.04"
# Linux Mint 17.2 Rafaela LTS (Ubuntu 14.04)
elif [[ $distro = "Linux Mint 17.1 Rafaela" ]]; then
    str="xUbuntu_14.04"
# fallback to LTS version, unsupported
else
    str="xUbuntu_15.04"
fi

if [ -d /etc/apt/sources.list.d ]; then
cat >/etc/apt/sources.list.d/megasync.list <<EOF
deb [signed-by=/usr/share/keyrings/meganz-archive-keyring.gpg] https://mega.nz/linux/repo/$str/ ./
EOF
fi

## Remove keys from main keyring
apt-key del 03C3AD3A7F068E5D &>/dev/null || :
apt-key del 1A664B787094A482 &>/dev/null || :

echo "
-----BEGIN PGP PUBLIC KEY BLOCK-----

mQINBGHe0SkBEADd5u7XBExxSg6stILhfNTNfhtTQ3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ngqt42UzHSd/
xqrOFTPMTAl1BDgFiMwwIH+JeYbpJ1ohKBaDMMG7IU4sp6YlIRj6iFeZCkwWjU7W
zIqtvwARAQABiQI8BBgBCgAmFiEEsByBGIBIDIVMc+x+GmZLeHCUpIIFAmHe0SkC
GwwFCRLMAwAACgkQGmZLeHCUpIIdohAA3c2/oLlrPTKEPCSlHvQYDpvTBQjdQ9GY
20pPHDom/T26qO5v36+vFfI47Z3uz8RX2vn83CEE467IjvGE3AyMp4cBODWgJJgG
Wx8yH8ueR1Qk9AAZ/VZ8zD0rQ34Sk0uVl7voosJ5cH2hwdy6xXjR2dfFb1+wLjpi
+Bdy3RU69Y2D7H8Okut8PpRgbd+u9JnK0+U0rzMJUICRIFC1NI8zaAw+ZpSTlTpY
622vp8ynkTk6TZ2D9e8yM70L/lwza5rloHi7NdCxEjly/O0JAON6if1kPbnteOUc
8pll57bPWxhUOnpcawDZa7i7E6WaN84gabnGE6l3DIGTp8Iatq+oT4mKDWLKotjA
ZsdccUmxLqfMKHl8gjkxjyGlD85QdCKms5zZIzUXnO/0HKs7+vSmRaK5xaD62M2L
h6q3it344NjV37v9Ofs2KroNovwfRBcjImblNv0DLERFeEIfzNJ4P9NsAW7Pvnem
mTa7cc5kmtaxBYi5ZPR9l3A5kWv2BlhFV8jZF328eh+KgLKdRJPRIK6z7NU7yHAB
cqHV7UnrSsJ2fzCOSOWULzW1ZhAGCP1I/kldxm1t5uzr0msZ9VFGlHYSkIAwBcys
/xZLk+MVzXxJfRv+9viXL/SoNitOsh8ZUs3SjvJTVhxFDpAmGvNb3+jv3pNVU77S
sAdVa6xer/c=
=F8S0
-----END PGP PUBLIC KEY BLOCK-----
" | gpg --dearmor > /usr/share/keyrings/meganz-archive-keyring.gpg

sysctl -p /etc/sysctl.d/99-megacmd-inotify-limit.conf || true

if [[ $str == "Debian_10.0" ]]; then
sed -i "s#Buster#Debian_10#g" /var/lib/apt/lists/mega.nz_linux_MEGAsync_Debian%5f10.0_._InRelease 2>/dev/null || true
fi

killall -s SIGUSR2 mega-cmd-server 2> /dev/null || true

