

function hasRepository()
{
  [ "$TV_PKGTYPE" == 'DEB' ]
}

function RemoveKeyFromMainKeyring
{
  cmdExists apt-key || return

  local keypath="$1"

  for keyid in $(gpg --list-packets "$keypath" | awk '$1=="keyid:"{print $2}'); do
    apt-key del "$keyid" &> /dev/null
  done
}

function ConfigureUpdateDEB()
{
  hasRepository || return
  cmdExists gpg || return

  local -r key1="$TV_SCRIPT_DIR/TeamViewer2017.asc"
  local -r key2="$TV_SCRIPT_DIR/TeamViewer2020.asc"
  local -r key3="$TV_SCRIPT_DIR/TeamViewer2023.asc"
  local -r keyring="/usr/share/keyrings/teamviewer-keyring.gpg"
  local -r logfile="$TV_BASE_DIR/logfiles/signaturekey.log"

  RemoveKeyFromMainKeyring "$key1"
  RemoveKeyFromMainKeyring "$key2"

  gpg --no-default-keyring --keyring gnupg-ring:"$keyring" --import "$key1" &>  "$logfile"
  gpg --no-default-keyring --keyring gnupg-ring:"$keyring" --import "$key2" &>> "$logfile"
  gpg --no-default-keyring --keyring gnupg-ring:"$keyring" --import "$key3" &>> "$logfile"

  chmod 0644 "$keyring"

  rm -f "$keyring~"
}

function PrintHelpRepo()
{
  hasRepository || return

  ABecho 'teamviewer repo'                            'show current repository configuration'
  ABecho 'teamviewer repo list'                       'list available packages'
  ABecho 'teamviewer repo default'                    'restore default configuration'
  ABecho 'teamviewer repo disable'                    'disable repository updates'
  ABecho 'teamviewer repo stable'                     'make all regular TeamViewer packages available (default)'
  ABecho 'teamviewer repo preview'                    'additionally, make feature preview packages available'
  echo
}

function Run_Repository()
{
  local action="$1"
  local param="$2" # unused

  local stableStr=
  local default='/opt/teamviewer/tv_bin/script/teamviewer.list'
  local repoFile='/etc/apt/sources.list.d/teamviewer.list'

  hasRepository || return 0

  echo

  if [ -n "$action" ]; then
    case "$action" in
      ( default                         )  RepoDefault        ;;
      ( disable                         )  RepoDisable        ;;
      ( stable | preview                )  RepoEnable         ;;
      ( list                            )  RepoList;   return ;;
      ( level                           )  RepoLevel;  exit 1 ;; # for internal use
      ( * ) die "Unknown option '$action'"
    esac
  fi
  RepoConfig
}

function RepoCheckSuperUser()
{
  isSuperUser || die 'You need root permissions for this operation'
}

function RepoDefault()
{
  RepoCheckSuperUser
  echo "Restoring defaults from $default ..."

  cp $default $repoFile
}

function RepoDisable()
{
  RepoCheckSuperUser
  echo "Disabling repository ..."

  RepoRemove
}

function RepoEnable()
{
  RepoCheckSuperUser
  echo "Enabling $action repository $stableStr..."

  RepoRemove
  RepoAdd
}

function RepoList()
{
  echo "Available TeamViewer packages"
  echo

  cmdExists apt-cache || die 'apt-cache not found'

  local data="$(apt-cache madison teamviewer teamviewer-host 2>/dev/null | sed -e 's/teamviewer |/teamviewer      |/g')"
  echo "$data" | grep -q teamviewer || { Yecho 'No packages found. Make sure the repository is enabled (teamviewer repo default) and up to date (apt update)'; return; }
  echo "$data"
}

function RepoRemove()
{
  grep -v -e '^\s*#*\s*deb ' $default > $repoFile
}

function RepoAdd()
{
  echo "# Modified via 'teamviewer repo' command"  >> $repoFile

  RepoAddLine stable main      >> $repoFile
  RepoAddLine preview main     >> $repoFile
  RepoAddLine development main >> $repoFile
}

function RepoAddLine()
{
  local dist=$1
  local component=$2
  local repoUri="deb [signed-by=/usr/share/keyrings/teamviewer-keyring.gpg] https://linux.teamviewer.com/deb $dist $component"
  local prefix=
  EnableEntry || prefix='### '

  echo "${prefix}$repoUri"
}

function EnableEntry()
{
  [ $action = stable      ] && return $([ $dist  = stable ])      # add stable only
  [ $action = preview     ] && return $([ $dist != development ]) # add all components, except development
  [ $action = development ] && return 0  # add all components
  return 1
}

function RepoConfig()
{
  BDecho "Active configuration lines in '$repoFile':"

  RepoConfigLines

  if (( $? != 0 )); then
    echo '< repository disabled / no active configuration lines >'
  fi

  echo
}

function RepoLevel()
{
  RepoConfigContains 'stable main'      || exit 10 # none
  RepoConfigContains 'preview main'     || exit 11 # stable
  RepoConfigContains 'development main' || exit 12 # preview
  exit 13 # development
}

function RepoConfigContains()
{
  RepoConfigLines | grep -q "$1"
}

function RepoConfigLines()
{
  grep -e '^\s*deb ' "$repoFile"
}
