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

# Copyright (c) 2010-2015, 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

LIBEXECDIR=${LIBEXECDIR-/usr/pkg/libexec/psu}
PKG_INFO_CMD="${PKG_INFO_CMD-/usr/sbin/pkg_info -K /usr/pkg/pkgdb}"

usage (){
    cat 1>&2 <<EOF
pkg_bin_summary - wrapper for 'pkg_info -X' that outputs
  build information and installation information
  variables as well some special variables for installed
  or binary packages,
  e.g. ASSIGNMENTS (settings to build multivariant packages) and
  REQUIREDBY. By default it is equivalent to pkg_info -Xa.
USAGE: pkg_bin_summary -h
       pkg_bin_summary [OPTIONS] [-- [PKG_INFO_ARGS]] [packages...]
OPTIONS:
  -h                display this help
  -a <fields>       add to the output the specified fields,
                    fields are separated by space or comma
  -f <fields>       output only the specified fields,
                    fields are separated by space or comma
  -r <fields>       remove from output the specified fields,
                    fields are output only specified fields
  -e                does nothing if no packages were specified
  -k <algorithms>   generate checksums for binary packages
                    and make them a part of summary (FILE_CKSUM)
PKG_INFO_ARGS:
  Options passed to pkg_info(1) that default to -a.
  In addition to these options -X is always applied to pkg_info
  as well as -B if needed. "--" is required
  if PKG_INFO_ARGS is not empty.
Samples of use:
  pkg_bin_summary -f PKGNAME,PKGPATH,ASSIGNMENTS,DEPENDS,automatic
  pkg_bin_summary -a automatic,ASSIGNMENTS
  pkg_bin_summary -r 'DESCRIPTION REQUIRES PROVIDES' -- -u
  pkg_bin_summary -a automatic -r REQUIRES 'lib*'
EOF
}

while getopts hea:r:f:k: f; do
    case "$f" in 
	'?')   exit 1;;
	h) usage; exit 0;;
	a) add_fields="$add_fields $OPTARG";;
	r) rem_fields="$OPTARG";;
	f) only_fields="$OPTARG";;
	e) explicit_pkgs=1;;
	k) cksums="$OPTARG"
	   add_fields="$add_fields FILE_NAME";;
    esac
done
shift `expr $OPTIND - 1`

if test $# -eq 0; then
    pkg_info_opts=-XBa
else
    pkg_info_opts=-XB
fi

if echo "$only_fields$add_fields" | grep PLIST > /dev/null; then
    pkg_info_opts="$pkg_info_opts -L"
fi

if test -n "$explicit_pkgs"; then
    if test $# -gt 0; then
	$PKG_INFO_CMD $pkg_info_opts "$@"
    else
	printf ''
    fi
else
    $PKG_INFO_CMD $pkg_info_opts "$@"
fi |
$LIBEXECDIR/XB2bin_summary -a "$add_fields" -r "$rem_fields" -f "$only_fields" |
if test -z "$cksums"; then
    cat
else
    ## Add FILE_CKSUM field to summary
    tmpdir=`mktemp -d ${TMPDIR-/tmp}/bin_summary.XXXXXX`
    test -n "$tmpdir" || exit 1

    # generate checksums
    for i in $cksums; do
	digest $i "$@" >> $tmpdir/cksums
    done

    # 
    awk -v cksums="$cksums" '
    BEGIN { cksums_cnt = split(cksums, cksums_arr) }
    FILENAME != "-" && $3 == "=" {
	fn = substr($2, 2, length($2)-2)
	sub(/^.*\//, "", fn)
	cksum [fn, tolower($1)] = $4
    }
    FILENAME == "-" {
	if (/^FILE_NAME=/)
	    fn = substr($0, 11)

	if (NF > 0) {
	    print
	    next
	}

	for (i=1; i <= cksums_cnt; ++i){
	    cksum_type = cksums_arr [i]
	    print "FILE_CKSUM=" cksum_type " " cksum [fn, cksum_type]
	}
	fn = ""
	print ""
    }' $tmpdir/cksums -
fi
