#!/bin/sh

# Copyright (c) 2008-2014 Aleksey Cheusov <vle@gmx.net>
# All rights reserved.
#
# See LICENSE file

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

set -e
. /usr/pkg/bin/pipestatus

# usage: stage_gen_report BUILD_ID
test $# -eq 1

# init
BUILD_ID="$1"

: ${DISTBB_CONF:=/usr/pkg/etc/distbb.conf}
. "$DISTBB_CONF"
. /usr/pkg/libexec/distbb/common

# main
######################################################################

gen_required_vs_provides (){
    if test -z "$exclude_stage_summary"; then
	verbose_print "  PROVIDES/REQUIRES consistency ... "
	pkg_lint_summary -p "$PKGSRC_PREFIX" -l "$PKG_SUMMARY" |
	awk '{$1=$2=""; print substr($0, 3)}' > "$check_required_vs_provides_fn"
	verbose_print '\n'
    fi
}

gen_broken_REQUIRES (){
    if test -z "$exclude_stage_summary"; then
	verbose_print "  broken REQUIRES ... "
	re='/home|/srv|/work|/tmp'
	pkg_grep_summary -f PKGNAME,PKGPATH,REQUIRES,PKG_OPTIONS \
	    -t re REQUIRES "^($re)" < "$PKG_SUMMARY" |
	grep -E "^REQUIRES=($re)|^PKGNAME=|^PKGPATH=|^PKG_OPTIONS=|^$" > "$check_broken_REQUIRES_fn" || true
	verbose_print '\n'
    fi
}

gen_unregistered_CONFLICTS (){
    if test -f "$enriched_summary_fn"; then
	verbose_print "  unregistered CONFLICTS ... "
	pkg_lint_summary -n "$enriched_summary_fn" |
	awk '{$1=$2=""; print substr($0, 3)}' > "$check_unregistered_conflicts_fn"
	verbose_print '\n'
    fi
}

gen_missing_progs (){
    verbose_print "  missing programs ... "

    cat "$failed_files".tmp |
    xargs /usr/pkg/libexec/distbb/distbb_grep '[Cc]ommand not found|Error (code )?127' > "$check_missing_progs_fn"

    verbose_print '\n'
}

failed_files=$tmpdir/failed_files

gen_failed_files (){
    verbose_print "  failed targets file list ... "

    find "$REPORT1_DIR" -name curr_stage.tmp |
    runawk -f xgetline.awk -f dirname.awk -f basename.awk -e '
    {
	getline stage < $0
	close($0)

	print dirname($0) "/stage_" stage ".tmp"
    }' > "$failed_files".tmp

    for i in package build configure; do
	grep -iE "/stage_$i.tmp"'$' "$failed_files".tmp > "$failed_files.$i".tmp || :
    done

    verbose_print '\n'
}

gen_configure_errors (){
    verbose_print "  configure errors ... "

    cat "$failed_files".configure.tmp |
    xargs /usr/pkg/libexec/distbb/distbb_grep 'configure: error:' > "$check_configure_errors_fn"

    verbose_print '\n'
}

gen_PLIST_errors (){
    verbose_print "  PLIST problems ... "

    cat "$failed_files".package.tmp |
    xargs -n200 runawk -f dirname.awk -f basename.awk -e '
    /The following files are in the PLIST/, /Error/{
	if (/The following files are in the PLIST/)
	    print prefix basename(dirname(FILENAME)) "/" basename(FILENAME)
	prefix = ".\n"
	print "   " $0
	if (/Error/)
	    print "\n"
    }
    END {  if (prefix) print "."  }
    ' > "$check_PLIST_errors_fn"

    verbose_print '\n'
}

gen_missing_headers (){
    verbose_print "  missing headers ... "

    cat "$failed_files".build.tmp |
    xargs /usr/pkg/libexec/distbb/distbb_grep -C0 \
	'[.](c|cpp|C|CC|cxx|c[+][+]|h|hpp):.*No such file or directory|error: missing required header' \
	> "$check_missing_headers_fn"

    verbose_print '\n'
}

gen_missing_libraries (){
    verbose_print "  missing libraries ... "

    cat "$failed_files".build.tmp |
    xargs /usr/pkg/libexec/distbb/distbb_grep -C0 'ld: cannot find -l|ld: library not found' \
	> "$check_missing_libraries_fn"

    verbose_print '\n'
}

gen_permission_denied (){
    verbose_print '  "permission denied" bugs ... '

    cat "$failed_files".tmp |
    xargs /usr/pkg/libexec/distbb/distbb_grep 'Permission denied' \
	> "$check_permission_denied_fn"

    verbose_print '\n'
}

gen_libtool_bugs (){
    verbose_print '  libtool bugs ... '

    cat "$failed_files".tmp |
    xargs /usr/pkg/libexec/distbb/distbb_grep 'libtool: link: specify a tag' \
	> "$check_libtool_bugs_fn"

    verbose_print '\n'
}

gen_pkgsrc_warnings (){
    verbose_print '  pkgsrc warnings ... '

    cat "$failed_files".tmp |
    xargs /usr/pkg/libexec/distbb/distbb_grep '^WARNING:' \
	> "$check_pkgsrc_warnings_fn"

    verbose_print '\n'
}

gen_segfaults (){
    verbose_print '  segmentation faults ... '

    cat "$failed_files".tmp |
    xargs /usr/pkg/libexec/distbb/distbb_grep 'Segmentation fault' \
	> "$check_segfault_fn"

    verbose_print '\n'
}

gen_memory_allocation (){
    verbose_print '  memory allocation failures ... '

    cat "$failed_files".tmp |
    xargs /usr/pkg/libexec/distbb/distbb_grep 'Cannot allocate memory|out of memory|virtual memory exhausted|terminate.*std::bad_alloc' \
	> "$check_memalloc_fn"

    verbose_print '\n'
}

gen_failed_files

gen_required_vs_provides
gen_broken_REQUIRES
#gen_unregistered_CONFLICTS
gen_missing_progs
gen_configure_errors
gen_PLIST_errors
gen_missing_headers
gen_missing_libraries
gen_permission_denied
gen_libtool_bugs
gen_pkgsrc_warnings
gen_segfaults
gen_memory_allocation

find "$meta_dir" -name 'check_*.txt' |
    while read f; do
	if ! test -s "$f"; then
	    rm "$f"
	fi
    done
