#!/bin/sh
#-*-mode:  sh -*-

# Copyright (c) 2007-2008 Aleksey Cheusov <vle@gmx.net>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

set -e

. pipestatus

BMAKE=${BMAKE:-/usr/bin/make}
PKGSRCDIR=${PKGSRCDIR:-/usr/pkgsrc}

usage (){
    cat 1>&2 <<EOF
usage: pkg_common_files [OPTIONS] pkgpath1 pkgpath2
OPTIONS:
 -h|--help         display this help
EOF
}

while test $# -ne 0; do
    case "$1" in
	-h|--help)
	    usage
	    exit 0;;
	--)
	    shift
	    break;;
	-*)
	    echo "Unrecognized option " $1 ". Type --help to see usage" 1>&2
	    exit 1;;
	*)
	    break;;
    esac
    shift
done

if test $# -ne 2; then
    usage
    exit 1
fi

pkg1="$1"
pkg2="$2"

export LC_ALL=C

# temp dir
tmpdir=/tmp/pkg_common_files.$$
mkdir -m 700 "$tmpdir"
trap "rm -rf $tmpdir" 0 1 2 3 15

#########################################
plist_only (){
    sed -n 's,^PLIST=,,p' "$@"
}

pkg_online_find -9 -r PKGPATH:exact:"$pkg1" |
plist_only |
sort -u > $tmpdir/1

pkg_online_find -9 -r PKGPATH:exact:"$pkg2" |
plist_only |
sort -u > $tmpdir/2

sort $tmpdir/1 $tmpdir/2 | uniq -d
