#!/usr/pkg/bin/runawk

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

#use "trim.awk"
#use "power_getopt.awk"

#.begin-str help
# pkg_enrich_summary converts PLIST_SRC, DISTINFO_FILE and DESCR_SRC
# given on input to PLIST, ALLDISTFILES and DESCRIPTION and
# optionally adds BOOTSTRAP_DEPENDS and TOOL_DEPENDS to BUILD_DEPENDS
#
# usage: pkg_enrich_summary [options] [files...]
# options:
#   -h         display help message
#   -b         add BOOTSTRAP_DEPENDS to BUILD_DEPENDS
#   -t         add TOOLS_DEPENDS to BUILD_DEPENDS
#.end-str

BEGIN {
	if (getarg("h")){
		print_help()
		exitnow(0)
	}
	with_bootstrap = getarg("b")
	with_tool_deps = getarg("t")

	pkgsrcdir = ENVIRON ["PKGSRCDIR"]
	assert(pkgsrcdir, "PKGSRCDIR variable should be set")
}

with_bootstrap && /^BOOTSTRAP_DEPENDS=/ {
	bootstrapdeps = substr($0, 19)
	next
}

with_tool_deps && /^TOOL_DEPENDS=/ {
	tooldeps = substr($0, 14)
	next
}

/^BUILD_DEPENDS=/ {
	builddeps = substr($0, 15)
	next
}

/^PKGPATH=/ {
	print
	pkgpath = trim_lrc(substr($0, 9))
	next
}

/^PLIST_SRC=/ {
	plist_fn = trim_lrc(substr($0, 11))
	next
}

/^DISTINFO_FILE=/ {
	distinfo_fn = trim_lrc(substr($0, 15))
	next
}

/^DISTFILES=/ {
	distfiles = trim_lrc(substr($0, 11))
	next
}

/^DESCR_SRC=/ {
	$0 = substr($0, 11)
	for (i=1; i <= NF; ++i){
		while (0 < ret = (getline ln < $i)){
			print "DESCRIPTION=" ln
		}

		if (ret < 0)
			printf "reading from `" $i "` failed\n" > "/dev/stderr"

		close($i)
	}
	next
}

NF == 0 {
	# BUILD_DEPENDS
	if (builddeps != "" || bootstrapdeps || tooldeps)
		print "BUILD_DEPENDS=" builddeps " " bootstrapdeps " " tooldeps
	builddeps = bootstrapdeps = tooldeps = ""

	# ALLDISTFILES
	if (distfiles != "" && distinfo_fn != ""){
		printf "ALLDISTFILES="
		while (0 < (ret = (getline < distinfo_fn))){
			if ($1 == "Size")
				printf "%s:%s ", substr($2, 2, length($2)-2), $4
		}

		if (ret < 0)
			printf "reading from `" distinfo_fn "` failed\n" > "/dev/stderr"

		close(distinfo_fn)
		printf "\n"
	}

	# PLIST
	if (plist_fn != ""){
		plist_cnt = split(plist_fn, plist_arr_fn)
		for (i=1; i <= plist_cnt; ++i){
			plist_fn = plist_arr_fn [i]
			if (plist_fn !~ /^\//)
				plist_fn = pkgsrcdir "/" pkgpath "/" plist_fn

			while (0 < (ret = (getline < plist_fn))){
				if ($0 !~ /@comment/)
					print "PLIST=" $0
			}

			if (ret < 0)
				printf "reading from `" plist_fn "` failed\n" > "/dev/stderr"

			close(plist_fn)
		}
	}
	pkgpath = plist_fn = ""

	#
	print ""
	next
}

{
	gsub(/ONLY_FOR_PLATFORM/, "ONLYFOR")
	gsub(/NOT_FOR_PLATFORM/, "NOTFOR")
	print
}
