#!/usr/pkg/bin/runawk

# Copyright (c) 2012-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 "power_getopt.awk"
#use "dirname.awk"

#.begin-str help
# usage: makefiles2deps [OPTIONS] [files...]
# makefiles2deps - reads makefile filenames from stdin or files, scans
#   them for .include "inc.mk" and .sinclude "inc.mk" directives, and
#   outputs a dependency graph on makefiles.
# OPTIONS:
#   -h         display this help
#.end-str

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

	head = tail = 0
}

{
	fn = $1

	queue [tail++] = fn
	seen [fn] = 1
}

function dir_plus_base (dir, base){
	if (base ~ /^\//)
		return base

	sub(/^[.]\//, "", base)
	while (base ~ /^[.][.]\//){
		base = substr(base, 4)
		sub(/^[.]\//, "", base)

		dir = dirname(dir)
	}

	return dir "/" base
}

function scan (fn,       q1,rec_fn,ret){
	while (0 < (ret = (getline < fn))){
		q1 = match($0, /["]/)
		if (!q1)
			continue

		if ($0 !~ /^[.][[:space:]]*s?include[[:space:]]+["][^$]+["][[:space:]]*$/)
			continue

		rec_fn = substr($0, q1+1)
		rec_fn = substr(rec_fn, 1, match(rec_fn, /["]/)-1)
		rec_fn = dir_plus_base(dirname(fn), rec_fn)

		++pairs_count
		pairs1 [pairs_count] = rec_fn
		pairs2 [pairs_count] = fn

		v = seen [rec_fn] = seen [rec_fn] + 1
		if (v == 1)
			queue [tail++] = rec_fn
	}

	if (ret < 0)
		unreadable [fn] = 1

	close(fn)
}

END {
	while (head != tail){
		fn = queue [head++]
		scan(fn)
	}

	for (i=1; i <= pairs_count; ++i){
		rec_fn = pairs1 [i]
		fn     = pairs2 [i]
		if (! (rec_fn in unreadable)){
			print rec_fn, fn
			already_printed [rec_fn] = already_printed [fn] = 1
		}else{
			print fn
			already_printed [fn] = 1
		}
	}
	for (i=0; i < tail; ++i){
		fn = queue [i]
		if (! (fn in unreadable) && ! (fn in already_printed)){
			print fn
		}
	}
}
