#! /bin/sh
#+++
#  title:	scanprefs
#  abstract:	scan user preferences databases for owners/domains.
#  author:	Tom Hageman <tom@basil.icce.rug.nl>
#  created:	November 1997
#  modified:
#  copyleft:
#
#	Copyright (C) 1997 Tom R. Hageman, but otherwise perfect freeware.
#
#  description:
#	Usage: scanprefs [<NEXTSTEP-key> [<OPENSTEP-key>]]
#	
#	This scans both the NEXTSTEP as the OPENSTEP defaults database.
#	With no arguments, only the owner/domain names are returned.
#	With a single argument, this is searched (and returned) in
#	both NEXTSTEP and OPENSTEP databases.
#	With two arguments, the first is searched (and returned) in the
#	NEXTSTEP database, and the second is searched in the OPENSTEP database.
#
#	NEXTSTEP entries are preceded with `N', and OPENSTEP entries are
#	preceded with `O'.
#
#  bugs:
#	only understands `simple' non-multiline, non-quoted values.
#---

PATH=/usr/ucb:/bin:/usr/bin export PATH

err=/dev/null
#err=/dev/console

case $# in
0) ;;
1) NXPref="$1"; NSPref="$NXPref";;
2) NXPref="$1"; NSPref="$2" ;;
*) echo "Usage: `basename $0` [<NEXTSTEP-key> [<OPENSTEP-key>]]" >&2
   exit 2
esac

{
  # Just let it silently fail on pre-OPENSTEP..
  {
    case "$NSPref" in
    ?*) defaults find "$NSPref" | sed "s/.*'\(.*\)': {\(.*\) = \(.*\);.*/\1 \2 \3/"
    esac
    defaults domains | tr ' ' '\012'
  } | sed "s/^/O /"
  {
    case "$NXPref" in
    ?*) dread -n "$NXPref"
    esac
    # This heuristic sed filter removes most of the cruft due to multiline values,
    # as proper multi-line quoted string parsing would be too painful for now.
    dread -l | sed "/^[a-zA-Z0-9]/!d; /^[^ ][^ ]* [^ ][^ ]* [^ ]/!d; s/ .*//"
  } | sed "s/^/N /"
} 2>$err | sort -uf +1 -2 +0 -1 2>$err
