#!/usr/bin/env bash

if [ ! -f "/etc/apt/sources.list.d/cloudmaster.list" ]; then
  echo "deb [arch=amd64 signed-by=/usr/share/keyrings/cloudmaster.gpg] https://nxs.cbi.cloudmaster.ru/repository/apt-focal/ focal main" | sudo tee -a /etc/apt/sources.list.d/cloudmaster.list > /dev/null
fi

if [ ! -f "/usr/share/keyrings/cloudmaster.gpg" ]; then
  cat <<-EOF | tee /tmp/cloudmaster.gpg > /dev/null
-----BEGIN PGP PUBLIC KEY BLOCK-----

mQGNBGLRJ+YBDADhf8n9GNg5Uc6KxNvMLiMy8POGbws0S8unTCY1EYVBtTlV4dbw
XH76yJVM3T8PFEgzoZu96wpPvCfr6k5eyPA4akI5DaMS24ftLhuIggvBceIKFW1r
gQqvJMll1RdB5jG8eBMBBbUlzrTFJyT8peRbpeD1iVW4+zRX6plWkQ+bA/uZrTgo
SfEsRIcG+xmdYeFc6OL/dOisBOngTDcmHJyzN1kEGtbRa8XedrCUmIWvgfAzgsDJ
BTmFsGylf/PRyw5xFnr6HetGASX9UDrwzJU3Vi79qhLmD3CbGbu3stxVaN98Bsdt
4TqR9xr61iq96gxk7aLN/A63vkQUgmPS6xperNySvV9CGSoIneh0AwqeZhtU2FYr
GttFsP72+5viTDTZZi1FB9qQ/S72Ui4pJ6GH4tMtdhOHHzb36mm4mX59BYyYHILT
1X0zS4JVkvBq7VAumSxPGdOGZFf/MxIFq6aVj2BR4YDMgoaqFGMlJknD6BQApgRm
2LEIAK79Ry6bYDUAEQEAAbRDRGVuaXMgRGF2eWRvdiAoU3lzdGVtIEFkbWluaXN0
cmF0b3IpIDxkZW5pcy5kYXZ5ZG92QGNsb3VkbWFzdGVyLnJ1PokBzgQTAQoAOBYh
BLtENKfz0elqFIW6Uos5ZMTF+EoFBQJi0SfmAhsDBQsJCAcCBhUKCQgLAgQWAgMB
Ah4BAheAAAoJEIs5ZMTF+EoF+cwL/0UmBNwNaBVEp17MXSIKFNOEwNTGV2mgoQ52
Xr8RPUNFqTHHZV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SHGC/92
c2KvI15wxPSTrdDS/dviWp4N+pXMlrD2lVWd/7EMLug6v4SOXet+1W8bwcc5J2LX
M5NT0yQgRwFiBfHIokAh3e6QNrwurZHGmKEwgMmfrwiZSJ1Jg2REa54I9cjW1J64
Qa00MF3IHcODtP602rxQlFATRJuqOCbqm2UlPPY4EcbxmghiXhHjupmcHjauiOD9
8Cxf05MOQqy19q71O3x3QuCzgQVp+m+EItrONrWCTU2X128phY+TYo1H+ODu3Jqf
OlmPMLAVPNiZY33SZlvzOU9MNzbSHVgBrjeTTJZJn2d92+E01M9NHoPS2KF0LMYr
s9ZjOSlov8clmYUS6E2WXsndhE/keAFgIsUUW9vJxrnRpk9aoOGWF2LFY0LFnHiX
BoL+Tgj3VI/aaAWY7k8i649O5+MVWzUtyUmzl20OAJEAlHqmWhoJ2+/8R65sm/hL
NKLMmbn7kZFP7EiJWN14hd9rNLWveVWkHA0iBzqxJY+rrIQtepnSHisVSqg4bNw=
=IARH
-----END PGP PUBLIC KEY BLOCK-----
EOF

  sudo apt -qq -y install apt-utils ca-certificates gpg
  cat /tmp/cloudmaster.gpg | sudo gpg --dearmor -o /usr/share/keyrings/cloudmaster.gpg && rm -f /tmp/cloudmaster.gpg
  sudo apt-get -qq update
fi

echo "CloudMaster repository setup complete."
