#!/bin/sh

apt_trusted_d_keyring="/etc/apt/trusted.gpg.d/datasaker-archive-keyring.gpg"
apt_usr_share_keyring="/usr/share/keyrings/datasaker-archive-keyring.gpg"

# init keyring
if ! [ -f ${apt_usr_share_keyring} ]; then
  echo "create archive-keyring.gpg"
  sudo touch ${apt_usr_share_keyring}
  sudo chmod a+r ${apt_usr_share_keyring}
fi

# download keyring then add key to keyring
curl -fsSL -o /tmp/datasaker.gpg.key https://dsk-agent-s3.s3.ap-northeast-2.amazonaws.com/dsk-agent-s3/public/public.gpg.key
cat /tmp/datasaker.gpg.key | sudo gpg --import --batch  --no-default-keyring --keyring "${apt_usr_share_keyring}"

# copy keyring to trusted keyring
if ! [ -f ${apt_trusted_d_keyring} ]; then
  sudo cp -a ${apt_usr_share_keyring} ${apt_trusted_d_keyring}
fi

# add apt source list
if ! [ -f /etc/apt/sources.list.d/datasaker.list ]; then
  echo "deb [signed-by=${apt_usr_share_keyring}] https://nexus.exem-oss.org/repository/debian-repos/ ubuntu main" | sudo tee /etc/apt/sources.list.d/datasaker.list > /dev/null
fi

sudo apt update
sudo apt install $1
