#!/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 "alt_getopt.awk"
#use "abort.awk"
#use "power_getopt.awk"
#use "heapsort.awk"

############################################################
#.begin-str help
# pkg_summary4view - converts pkg_summary(5) to human readable format
# usage: pkg_summary4view [OPTIONS] [files...]
# OPTIONS:
#   -h       display this help
#.end-str
############################################################

function spaces (n,            ret){
	ret = ""
	while (n-- > 0){
		ret = ret " "
	}
	return ret
}

function resort_D0 (arr, remap,                i){
	for (i=1; i <= NF; ++i){
		arr [i] = $i
	}
	heapsort(arr, remap, 1, NF)
	for (i=1; i <= NF; ++i){
		$i = arr [remap [i]]
	}
}

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

	special ["DESCRIPTION"] = 0
	special ["PLIST"]       = 0
	special ["PROVIDES"]    = 0
	special ["REQUIRES"]    = 0

	depends_like ["TARGET_BUILD_DEPENDS"] = 0
	depends_like ["_ALL_DEPENDS"] = 0
	depends_like ["BUILD_DEPENDS"] = 0
	depends_like ["DEPENDS"]       = 0
	depends_like ["CONFLICTS"]     = 0

	# single or multiple line
	s_or_m ["ALLDISTFILES"]  = 0
	s_or_m ["ALLSRCFILES"]   = 0

	offset = 16
}

BEGIN {
	for (i in s_or_m){
		depends_like [i] = 0
	}
}

NF == 0 {
	prev_field = ""
	print ""
	next
}

function get_alldistfiles (tok,         idx){
	idx = index(tok, ":")
	return substr(tok, 1, idx-1) " " spaces(45-idx) substr(tok, idx+1)
}

NF > 0 && prev_field == "" {
	print "-----------------------------------------------------------"
}

{
	#
	field = $0
	sub(/=.*$/, "", field)

	#
	sub(/^[^=]*=/, "")

	if (NF == 0)
		next

	field_alldistfiles = (field == "ALLDISTFILES")

	#
	if (field in depends_like){
		resort_D0()

		if (field != prev_field) {
			field1 = field ":"
			printf "%s", field1
		}else{
			field1 = ""
		}

		printf "%s", spaces(offset - length(field1))

		if (field_alldistfiles){
			printf "%s\n", get_alldistfiles($1)
		}else{
			printf "%s\n", $1
		}

		for (i=2; i <= NF; ++i){
			printf "%s", spaces(offset)
			if (field_alldistfiles){
				printf "%s\n", get_alldistfiles($i)
			}else{
				printf "%s\n", $i
			}
		}
	}else if (field in special) {
		if (field != prev_field) {
			printf "%s:\n", field
		}

		printf "    %s\n", $0
	}else{
		printf "%s:%s%s\n", field, spaces(offset - length(field) - 1), $0
	}

	#
	prev_field = field
}
