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

# Copyright (c) 2007-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

. pipestatus

############################################################
# user settable variables
export PKGSRCDIR=${PKGSRCDIR:=/usr/pkgsrc} # export - for pkg_src_fetch_var
export BMAKE=${BMAKE:=/usr/pkg/bin/bmake}
SH=${SH-/bin/sh}

############################################################
LIBEXECDIR=${LIBEXECDIR-/usr/pkg/libexec/psu}
. ${LIBEXECDIR}/sig_handler.sh
on_exit () {
	# Stupid test for stupid Solaris
	if test -n "$tmpdir"; then rm -rf $tmpdir; fi
}

tmpdir=`mktemp -d ${TMPDIR-/tmp}/pkg_micro_src_summary.XXXXXX`
test -n $tmpdir || exit 1

errsfn=$tmpdir/errors.txt

############################################################
usage (){
    cat 1>&2 <<EOF
pkg_micro_src_summary - builds micro summary information
about source packages

usage: pkg_micro_src_summary [OPTIONS] [pkgpath1 pkgpath2 ...]
OPTIONS:
  -h              display this help message
  -f <fields>     fields to be included to summary,
                  PKGNAME and PKGPATH is the default
  -s              paexec slave mode
EOF
}

fields='PKGNAME PKGPATH'

set_fields (){
    fields=$(echo "$@" | tr , ' ')
}

while getopts hsf: f; do
    case "$f" in
	h)    usage; exit 0;;
	f)    set_fields "$OPTARG";;
	s)    slave=1;;
	?)    printf "Run pkg_micro_src_summary -h for details\n"; exit 2;;
    esac
done
shift `expr $OPTIND - 1`

############################################################
cd $PKGSRCDIR

# running helper
helper_out=$tmpdir/helper_out.txt

list_pkgs (){
    if test $# -ne 0; then
	# processing arguments
	for pkgpath in "$@"; do
	    echo "$pkgpath"
	done
    else
	awk -v C=10 \
	    'BEGIN {modu=C-1}
	     { printf ",%s", $0; if ((NR % C) == modu) printf "\n"; }
	     END {if (NR && (NR % C) != modu) printf "\n"}'
    fi
}

if test "$slave"; then
    pkg_src_fetch_var -sf "$fields"
    exit 0
fi

pkg_src_fetch (){
    if test "$PSS_SLAVES"; then
	qfields="$(echo $fields | tr ' ' ,)"
	environ="PSS_SLAVES= BMAKE=$BMAKE PKGSRCDIR=$PKGSRCDIR"
	environ="$environ PSS_PRE_PATH=$PSS_PRE_PATH PSS_POST_PATH=$PSS_POST_PATH"

	paexec -z $PSS_PAEXEC_EXTRA_OPTS \
	    -n "$PSS_SLAVES" \
	    -t "$PSS_TRANSPORT" \
	    -c "env $environ $SH $0 -s -f '$qfields'"
    else
	pkg_src_fetch_var -f "$fields"
    fi
}

runpipe list_pkgs "$@" '|' pkg_src_fetch > "$helper_out"

# `+' to micro_summary (fast!)
awk -F'\t' -v fields="$fields" '
BEGIN {
   fcnt = split(fields, fname, " ")
}

/^[+]/ {
   for (i=1; i <= fcnt; ++i){
      printf "%s=%s\n", fname [i], $(i+1)
   }

   printf "\n"
}' "$helper_out"

# `-' to micro_summary (slo-o-o-ow!)
make_out=$tmpdir/make_out.txt
awk -F'\t' '
/^[-]/ {
   print $NF
}' "$helper_out" |
pkg_src_summary -f "$fields"
