#!/bin/sh
#
# where_is_debian_package_from
#
# This script shows you where your installed Debian-Packages came from
#
# Usage:
#          where_is_deb_from
#
# Autor Thorsten Gunkel <tgunkel@gmx.de>
#
# --- 13.08.2004 Thorsten Gunkel <tgunkel@gmx.de> ---
# * Initial Release
# --- No older changelog available ---
#

# get installed packages
dpkg --get-selections | 
grep [^A-Za-z0-9]install$ |
awk '{ print $1 }' |
while read package_line
do
 # extract possible sources
 echo -n "$package_line |"
 source_found=0
 apt-cache policy "$package_line" |
 while IFS= read source_line
 do
  # find the marked source
  case $source_found in
  0) echo "$source_line" | grep ^" \*\*\*" >/dev/null && source_found=1
  ;;
  1) echo "$source_line" | grep \ \ *[-\ ][0-9][0-9]*\ >/dev/null || source_found=2
     if [ $source_found -eq 1 ]; then
      source_line="`echo $source_line | sed s/^\ *[0-9]*\ //`"
	  echo -n " $source_line |"
	 fi
  ;;
  *)
  ;;
  esac
 done
 echo 
done |
sort +1 |
while read finish_line
do
 # sort output by sources
 package="`echo $finish_line | sed s/\ .*$//`"
 debsource=`echo $finish_line | sed s/^[^\ ]*\ //`
 if [ "X$debsource" != "X$debsource_old" ]; then
  echo "*** $debsource"
 fi
 debsource_old=$debsource
 echo "$package"
done