#!/usr/bin/env bash

URL=riot
RELEASE=buster

# sudo sh -c "echo 'deb https://${URL}.im/packages/debian/ ${RELEASE} main' > /etc/apt/sources.list-available/matrix-riot-im.list"

# sudo ln -s /etc/apt/sources.list-available/matrix-riot-im.list /etc/apt/sources.list.d/matrix-riot-im.list
cat <<EOF | sudo tee /etc/apt/sources.list.d/matrix-riot-im.list
Types: deb
URIs: https://packages.element.io/debian/
Suites: buster
Components: main 
Signed-By: /usr/share/keyrings/riot-im-archive-keyring.gpg
EOF

# Add the public key:
KEY_DIR=/usr/share/keyrings
curl -L https://packages.element.io/debian/riot-im-archive-keyring.gpg | sudo tee ${KEY_DIR}/riot-im-archive-keyring.gpg
curl -L https://packages.element.io/debian/element-io-archive-keyring.gpg | sudo tee ${KEY_DIR}/element-io-archive-keyring.gpg

# Bypass apt-proxy for riotIM packages
if [ -d /etc/apt/apt.conf.d/02proxy ]; then
  if [ ! -z $(grep "${URL}") ]; then
    echo "Acquire::http::Proxy { \"${URL}\" DIRECT; };" >>/etc/apt/apt.conf.d/02proxy
  fi
else
  echo "Acquire::http::Proxy { \"${URL}\" DIRECT; };" >>/etc/apt/apt.conf.d/02proxy
fi

sudo apt update && apt install -y riot-web
