#!/usr/pkg/bin/runawk

# 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.

############################################################

#env "LC_ALL=C"

#use "abort.awk"
#use "power_getopt.awk"

############################################################
#.begin-str help
# pkg_uniq_summary - filter out repeated summaries.
#   Summaries are compared by (PKGPATH, PKGBASE, ASSIGNMENTS) triple.
# usage: pkg_uniq_summary -h
#       pkg_uniq_summary [OPTIONS] [files...]
# OPTIONS:
#   -h              display this help
#   -n              only PKGBASE is analysed
#   =F <fields>     fields to compare (b-PKGBASE, n-PKGNAME, p-PKGPATH,
#                   a-ASSIGNMENTS)
#.end-str
############################################################

BEGIN {
	if (getarg("h")){
		print_help()
		exitnow(0)
	}

	skip       = 0
	line_count = 0

	field_pkgname = field_pkgbase = field_pkgpath = field_assign = 0

	opt_F = getarg("F")
	if (opt_F){
		if (opt_F ~ /a/) field_assign  = 1
		if (opt_F ~ /p/) field_pkgpath = 1
		if (opt_F ~ /n/) field_pkgname = 1
		if (opt_F ~ /b/) field_pkgbase = 1

		if (match(opt_F, /[^apnb]/))
			abort("Invalid field specifier `" substr(opt_F, RSTART) "'")
	}else if (getarg("n")){
		field_pkgbase = 1
		field_pkgname = field_pkgpath = field_assign = 0
	}else{
		field_pkgname = field_pkgpath = field_assign = 1
	}

	FS = "="
}

NF == 0 {
	if (!pkgs [pkgname, pkgbase, pkgpath, assigns]++){
		for (i=0; i < line_count; ++i){
			print lines [i]
		}
		print ""
	}
	pkgname = pkgbase = pkgpath = assigns = ""
	line_count = 0
	next
}

{
	lines [line_count++] = $0
}

(field_pkgname || field_pkgbase) && $1 == "PKGNAME" {
	_pkgname = substr($0, 9)
	if (field_pkgname){
		pkgname = _pkgname
	}
	if (field_pkgbase){
		pkgbase = _pkgname
		sub(/-[^-]+$/, "", pkgbase)
	}
	next
}

field_pkgpath && $1 == "PKGPATH" {
	pkgpath = substr($0, 9)
	if (sub(/:.*$/, "", pkgpath) && field_assign){
		assigns = substr($0, 9)
		sub(/^[^:]*:/, "", assigns)
	}
	next
}

field_assign && $1 == "ASSIGNMENTS" {
	assigns = substr($0, 13)
	next
}
