#!/bin/bash
if [[ $(/usr/bin/id -u) -ne 0 ]]
then
  echo 'Please run this script as the "root" user (uid 0).'
  exit 1
fi
if [[ ! -x /usr/bin/apt-show-versions ]]
then
  echo 'To use this script, please run this command first:'
  echo '  sudo apt install apt-show-versions'
  exit 2
fi
/usr/bin/apt-get update --quiet=2 --download-only
FOREIGN=$(tempfile --prefix=fopa_)
/usr/bin/apt-show-versions | grep -Ev ' (uptodate|not installed|upgradeable to .*)$' > $FOREIGN
if [ -e $FOREIGN ]
then
  echo '[ No available version in archive ]'
  awk -F' ' '/: No available version in archive$/ {print $1" (version "$2")"}' $FOREIGN
  echo
  echo '[ Newer than version in archive ]'
  awk -F' ' '/ newer than version in archive$/ {print $1" (version "$2")"}' $FOREIGN
else
  echo 'Note: No packages / package versions of unknown origin were found' >&2
fi
rm $FOREIGN