#! /bin/sh
# $OpenBSD: show-reverse-deps,v 1.5 2018/12/19 15:29:47 espie Exp $
#
# Copyright (c) 2018 Marc Espie <espie@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

# compute reverse dependencies
# XXX this is not perfect yet! it *does* require collating lib-depends over
# multi-packages setup, which this is NOT doing yet, but it is close to
# actually doing it.
# we just require a few more views for that.

if [ $# -eq 0 ]
then
    echo 2>&1 "Usage: show-reverse-deps path [sqlite_db]"
    exit 1
fi
if [ $# -ge 2 ]
then
    file=$2
else
    file=/usr/local/share/sqlports
fi
cat <<EOSQL |sqlite3 $file
with recursive d (fullpkgpath, dependspath) as
	(select root.fullpkgpath, root.dependspath 
		from _canonical_depends root
			join _paths 
			    on root.dependspath=_paths.canonical
			join _paths p2 
			    on p2.fullpkgpath="$1" and p2.id=_paths.pkgpath
	    union
	    select child.fullpkgpath, child.dependspath
	    from d parent, _canonical_depends child  
	    	where parent.fullpkgpath=child.dependspath)
select distinct(_paths.fullpkgpath) from d 
    join _paths 
	on _paths.id=d.fullpkgpath
order by _paths.fullpkgpath;
EOSQL
