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

# Copyright (c) 2012, Aleksey Cheusov <vle@gmx.net>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
#       copyright notice, this list of conditions and the following
#       disclaimer in the documentation and/or other materials provided
#       with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

set -e

export LC_ALL=C

: ${PKG_DBDIR:=/usr/pkg/pkgdb}

usage () {
    cat 1>&2 <<EOF
pkg_digger_installed is a pkg summary searcher
(backend for pkg_digger).
usage: pkg_digger_installed -h
       pkg_digger_installed -f
       pkg_digger_installed -s
       pkg_digger_installed [-n limit] fsq1 [fsq2 ...]
       pkg_digger_installed -1|-3|-9|-i [-q] pkgid1 [pkgid2...]
where pkgid is <PKGPATH>,<PKGNAME>
and fsq is <field>:<strategy>:<query>
OPTIONS:
   -h         display this screen
   -s         display available search strategies
   -f         display available fields for search in
   -1         display 1-line information about packages
   -3         display short information about packages
   -9|-i      display full information about packages
   -q         quiet mode, do not print "No matches found" to stderr
   -n <limit>    maximum number of results
EOF
}

check_summary (){
    if test -z "$PKG_DIGGER_SUMMARY"; then
	echo 'PKG_DIGGER_SUMMARY environment variable must be set' 1>&2
	exit 1
    fi
}

update_summary (){
    check_summary
    if test -s "$PKG_DIGGER_SUMMARY" -a \
	"$PKG_DIGGER_SUMMARY" -nt "$PKG_DBDIR/pkgdb.byfile.db"
    then
	return 0
    fi
    pkg_bin_summary -a automatic,PLIST > "$PKG_DIGGER_SUMMARY"
}

if test $# -eq 0; then
    usage
    exit 2
fi

while getopts hsf139iqn: f; do
    case "$f" in
	h)              usage; exit 0;;
	s)              pkg_digger_summary -s; exit 0;;
	f|9|i|3|1|q)  opts="$opts -$f";;
	n)              opts="$opts -$f $OPTARG";;
	?)              echo "Run pkg_digger_installed -h for details" 1>&2; exit 2;;
    esac
done
shift `expr $OPTIND - 1`

update_summary

exec pkg_digger_summary $opts "$@"
