#!/usr/pkg/bin/runawk

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

#use "alt_assert.awk"
#use "power_getopt.awk"

#.begin-str help
# convert output of pkg_cmp_summary(1) to human readable format
# -h    display help message
# -0    output neither prefixes nor suffixes
# -1    output prefixes
# -2    output suffixes
# -3    output prefixes and suffixes (the default)
#.end-str

function print_res (){
	if (op != "=" || pref || suff)
		print pref op suff "\t" pkgpath "\t" pkgname "\t" ver1 "\t" ver2 | \
		"sort -k3 | awk -F'\t' '{sub(/  <  /, \" \", $1); printf \"%6s %-30s %-15s %-12s %-12s\\n\", $1, $2, $3, $4, $5}'"
}

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

	out_pref = out_suff = 1
	if (getarg("1") || getarg("0"))
		out_suff = 0
	if (getarg("2") || getarg("0"))
		out_pref = 0
}

/^[^ ]/ {
	pkgpath = pkgname = ver1 = ver2 = ""
	pkgpath = $2
	pkgname = $3
	if ($1 ~ /^[<>=!]$/){
		ver1    = $4
		ver2    = $5
	}else if ($1 == "-"){
		ver1    = $4
	}else if ($1 == "+"){
		ver2    = $4
	}

	op = $1
	pref = ""
	suff = ""

	getline
	while (NF > 0){
		assert($0 ~ /^ /)

		val1 = (out_pref ? $2 : "SUBSEP")
		getline
		val2 = (out_suff ? $2 : "SUBSEP")

		if ($1 == "automatic"){
			if (out_pref && op != "+"){
				addon = (val1 == "yes" ? "" : "u")
				if (val1 != val2 || op == "-")
					pref = addon pref
			}

			if (out_suff && op != "-"){
				addon = (val2 == "yes" ? "" : "u")
				if (val1 != val2 || op == "+")
					suff = suff addon
			}
		}

		if ($1 == "try_out"){
			if (out_pref && op != "+"){
				addon = (val1 == "yes" ? "t" : "")
				if (val1 != val2 || op == "-")
					pref = addon pref
			}

			if (out_suff && op != "-"){
				addon = (val2 == "yes" ? "t" : "")
				if (val1 != val2 || op == "+")
					suff = suff addon
			}
		}

		if ($1 == "keep"){
			if (out_pref && op != "+"){
				addon = (val1 == "yes" ? "k" : "")
				if (val1 != val2 || op == "-")
					pref = addon pref
			}

			if (out_suff && op != "-"){
				addon = (val2 == "yes" ? "k" : "")
				if (val1 != val2 || op == "+")
					suff = suff addon
			}
		}

		getline
	}

	len = length(pref)
	if (len == 1) pref = " " pref
	else if (len == 0) pref = "  " pref

	len = length(suff)
	if (len == 1) suff = suff " "
	else if (len == 0) suff = suff "  "

	print_res()
}
