#!/bin/sh

# Copyright (c) 2008-2011 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
######################################################################

build_start=$(cat "$build_start_fn")
build_end=$(cat "$build_end_fn")

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

verbose_print "  sort_uniq PKGNAMEs/failed_{because,deps}.txt ... "

find $REPORT1_DIR -type f \
    '(' -name failed_because.txt -o -name failed_deps.txt ')' |
while read f; do
    sort_uniq_inplace "$f"
done

verbose_print 'done\n'

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

# main continuing...
count_lines (){
    awk 'END {print NR}' "$@"
}

left_align (){
   printf "%-$1s" "$2"
}

right_align (){
   printf "%$1s" "$2"
}

get_failed_stage (){
    # $1 - pkgpath
    # $2 - pkgname
    cat "${REPORT1_DIR}/$(get_log_dir $1 $2)/curr_stage.tmp"
}

print_failed_pkg (){
    # $1 - pkgpath
    # $2 - breaks
    # $3 - stage
    # $4 - maintainer
    # $5 - pkgname
    short_pkgpath="$(echo $1 | cut -f1 -d:)"
    log_dir="$(get_log_dir $1 $5)"
    if test -n "$2" && test "$2" != "Breaks"; then
	__href_beg="`printf '<a href=\"../%s/failed_deps.html\" title=\"packages failed due to %s\">' "$log_dir" $short_pkgpath`"
	__href_end='</a>'
    else
	__href_beg=''
	__href_end=''
    fi

    printf "  <tr><td><a href=\"../%s/stage_%s.html\" title=\"build log for %s\">%-27s</a></td> <td class=\"breaks\">${__href_beg}%7s${__href_end}</td> <td>%11s</td> <td align=\"right\">%27s</td>\n    <td align=\"right\">%-25s</td></tr>\n" "$log_dir" "$3" "$short_pkgpath" "$short_pkgpath" "$2" "$3" "$4" "$5"
}

print_failed_deps_pkg (){
    # $1 - pkgpath
    # $2 - pkgname
    # $3 - maintainer
    # $4 - deppath
    # $5 - depname
    failed_stage=''
    if test "$1" != Package -a -n "$4"; then
	failed_stage=$(get_failed_stage "$4" "$5" 2>/dev/null || true)
	if test -z "$failed_stage"; then
	    # progress.txt was manually edited and stage "build" was rerun?
	    return
	fi
    fi
    short_pkgpath="$(echo $1 | cut -f1 -d:)"
    if test "$short_pkgpath" != "$1"; then
	short_pkgpath="$short_pkgpath($2)"
    fi

    if test -n "$1"; then
	printf "  <tr><td><a href=\"../%s/info.html\" title=\"failed due to...\">%-42s</a></td> <td> %s</td></tr>\n" "$(get_log_dir $1 $2)" "$short_pkgpath" "$3"
    else
	printf "  <tr><td align=\"left\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"../%s/stage_%s.html\" title=\"build log for %s\">     %-37s</a></td><td>       %s</td></tr>\n" "$(get_log_dir $4 $5)" "$failed_stage" "$4" "$4" "$3"
    fi
#    printf "  <tr><td>%-26s</td> <td align=\"left\"><a href=\"../%s/stage_%s.html\" title=\"build log for %s\">%-26s</a></td><td> %s</td></tr>\n" "$short_pkgpath" "$5" "$failed_stage" "$4" "$4" "$3"
}

pkgpath2maintainer (){
    # $1 - pkgpath
    awk -v pkgpath="$1" '$1 == pkgpath {print $3; exit}' "$packages_failed_total_fn"
}

pkgpath2pkgname (){
    # $1 - pkgpath
    awk -v pkgpath="$1" '$1 == pkgpath {print $2; exit}' "$packages_failed_total_fn"
}

distbb_diff2html (){
    awk -v report1_dir=$REPORT1_DIR -F'\t' -v q="'" '
function p (progress, pkgpath, pkgbase, version, breaks) {
   printf "<tr>%s <td> %s</td><td> %s</td><td> %s</td><td> %s</td></tr>\n",
	progress, pkgpath, pkgbase, breaks, version
}
BEGIN {
   printf "<table width=\"100%%\">"
   p("<td></td>", "<b>Package</b>", "<b>PKGBASE</b>",
     "<b>Version(s)</b>", "<b>Breaks</b>")
}
{
   # $1 - result
   # $2 - PKGPATH
   # $3 - PKGBASE
   # $4 - version(s)

#   sub(/:.*$/, "", $2) # strip building options

   ver = $4
   sub(/.* /, "", ver)

   pkgpath_m = $2
   sub(/:.*/, "", pkgpath_m)  # remove building options
   gsub(/\//, "_", pkgpath_m) # s|/|_|
   logsubdir = pkgpath_m "_" $3 "-" ver

   breaks = ""

   if ($1 ~ /^N?-$/){
      stage_fn  = report1_dir "/" logsubdir "/curr_stage.tmp"
      breaks_fn = report1_dir "/" logsubdir "/failed_deps.txt"

      getline failed_stage < stage_fn
      close(stage_fn)

      breaks_pipe = ("awk " q "END {print NR}" q " " breaks_fn)
      breaks_pipe | getline breaks
      close(breaks_pipe)
      if (breaks+0 == 0)
         breaks = ""

      $2 = sprintf("<a href=\"../%s/stage_%s.html\" title=\"build log for %s\">%-27s</a>", logsubdir, failed_stage, $2, $2)

      $1 = "<td class=\"pkg-failed\">" $1 "</td>"
   }else if ($1 ~ /[+-]d$/){
      next
   }else if ($1 ~ /[+]/){
      $1 = "<td class=\"pkg-built\">" $1 "</td>"
   }else{
      $1 = "<td>" $1 "</td>"
   }
   p($1, $2, $3, $4, breaks)

   diff = 1
}
END {
   if (!diff) print "<tr><td>no difference</td></tr>"
   printf "</table>"
}
' "$@"
}

print_distbb_diff_help_msg (){
    cat <<EOF
<p><i>'-' package was built successfully but now fails<br>
'+' package failed previously but now is built successfully<br>
'c' pkg_src_summary failed for given package<br>
'd' package fails/failed due to broken dependency<br>
'N' package is new<br>
'R' package was removed<br>
</i></p>
EOF
}

report_html_header (){
    cat <<EOF
<h1>pkgsrc-$PKGSRCVER bulk build results (distbb-$version)</h1>

<h2>$OPSYS $OSVER</h2>

<!--  <p><a href="$REPORT_URL" title="Full report">$REPORT_URL</a></p> -->

EOF
}

report_html_responsible (){
    if test "$RESPONSIBLE"; then
	echo "<p>$RESPONSIBLE</p> <!-- REMOVE ME -->"
    fi
}

report_html_notes (){
    echo '<hr>'
    if test "$NOTES"; then
	echo '<h3><a name="notes"></a>Notes</h3>'
	cat "$NOTES"
    fi
}

report_html_jumpto (){
    cat <<EOF
<div class="infobox">
<p>Jump to:
<ul>
    <li><a href="#summary">Summary</a></li>
    <li><a href="#failures per maintainer">Failures per maintainer</a></li>
    <li><a href="#failed">Failed packages</a></li>
    <li><a href="#failed depends">Failed due to deps</a></li>
    <li><a href="#old vs new">Progress: prev. vs. curr.</a></li>
    <li><a href="#best vs new">Progress: best vs. curr.</a></li>
    <li><a href="#checks">Sanity checks and tips</a></li>
</ul>
</div>

EOF
}

report_html_summary (){
    cat <<EOF

<h3><a name="summary"></a>Summary</h3>
EOF

    report_html_jumpto

    cat <<EOF
<table><tr>  <td>Build started:</td><td align="right">$(right_align 38 "${build_start}")</td><td>&nbsp;</td></tr>
  <tr><td>Build ended:</td>  <td align="right">$(right_align 38 "${build_end}")</td><td>&nbsp;</td></tr>

  <tr><td>&nbsp;</td>                  <td>&nbsp;</td><td>&nbsp;</td></tr>
  <tr><td>$(left_align 40 "Inspected packages")</td><td align="right">$packages_inspected_cnt</td><td></td></tr>
  <tr class="pkg-total"><td>$(left_align 40 "Built packages (total)")</td><td align="right">$packages_built_total</td><td>&nbsp;&nbsp;&nbsp;<a href="$(basename $packages_built_total_fn)" title="All built packages"> list</a></td></tr>
  <tr><td>&nbsp;&nbsp;  $(left_align 38 "built previously")</td><td align="right">$packages_built_prev_cnt</td><td>&nbsp;&nbsp;&nbsp;<a href="$(basename $packages_built_prev_fn)" title="Packages built previously"> list</a></td></tr>
  <tr class="pkg-built"><td>&nbsp;&nbsp;  $(left_align 38 "really built")</td><td align="right">$packages_built_cnt</td><td>&nbsp;&nbsp;&nbsp;<a href="$(basename $packages_built_fn)" title="Successfully built packages"> list</a></td></tr>
  <tr><td>$(left_align 40 "Failed packages (total)")</td><td align="right">$packages_failed_total</td><td>&nbsp;&nbsp;&nbsp;<a href="$(basename $packages_failed_total_fn)" title="All failed packages"> list</a></td></tr>
  <tr class="pkg-failed"><td>&nbsp;&nbsp;  $(left_align 38 "really failed")</td><td align="right">$packages_failed_cnt</td><td>&nbsp;&nbsp;&nbsp;<a href="$(basename $packages_failed_fn)" title="Really failed packages"> list</a></td></tr>
  <tr><td>&nbsp;&nbsp;  $(left_align 38 "marked as not available")</td><td align="right">$packages_failed_notavail_cnt</td><td>&nbsp;&nbsp;&nbsp;<a href="$(basename $packages_failed_notavail_fn)" title="Packages marked as not available"> list</a></td></tr>
  <tr class="pkg-faileddeps"><td>&nbsp;&nbsp;  $(left_align 38 "failed due to them")</td><td align="right">$packages_failed_deps_cnt</td><td>&nbsp;&nbsp;&nbsp;<a href="$(basename $packages_failed_deps_fn)" title="Packages failed due to dependencies"> list</a></td></tr>
EOF

    if  test "$packages_failed_scan_cnt" -gt 0 ||
	test -s "$build_src_summary_log_fn"
    then
	cat <<EOF
  <tr class="pkg-failed"><td>&nbsp;&nbsp;  $(left_align 38 "pkg_src_summary failed")</td><td align="right">$packages_failed_scan_cnt</td><td>&nbsp;&nbsp;&nbsp;<a href="$(basename $packages_failed_scan_fn)" title="Packages failed while building pkg_src_summary"> list</a></td> <td>&nbsp;&nbsp;<a href="$(basename $build_src_summary_log_fn)" title="pkg_src_summary logs">logs</a></td></tr>
EOF
    fi

    if test -s "$errors_fn"
    then
	cat <<EOF
  <tr class="pkg-failed"><td>$(left_align 40 "Error messages")</td><td align="right"></td><td></td> <td>&nbsp;&nbsp;<a href="$(basename $errors_fn)" title="pkg_summary2build_graph logs">logs</a></td></tr>
EOF
    fi

    if test "$PACKAGES_URL"; then
	url="<a href=\"$PACKAGES_URL\" title=\"binary packages\">binary packages</a>"
    else
	url='binary packages'
    fi

    cat <<EOF
</table>
<p>Packages not listed here resulted in a $url.<br>
Results of failed packages are available below.<br>
Progress messages are <a href="$(basename $progress_fn)" title="Progress messages">here</a>.<br> <!-- REMOVE ME -->
pkg_src_summary is <a href="$(basename $pkg_src_summary_fn)" title="pkgsrc source packages summary">here</a>.<br> <!-- REMOVE ME -->
pkg_src_summary vs. pkg_summary <a href="$(basename $summary_cmp_fn)" title="binary packages against pkgsrc tree">comparison</a>.<br> <!-- REMOVE ME -->
</p>
EOF

}

report_html_fails_per_maintainer (){
    # failures per maintainer
    cat <<EOF
<hr>
<h3><a name="failures per maintainer"></a>Failures per maintainer</h3>

EOF
    printf '<table width="30%%">'
    printf '<tr><td>%-36s</td> <td align="right">%4s</td></tr>\n' '<b>Maintainer</b>' '<b>Breaks</b>'
    echo ''

    cat "$packages_failed_fn" |
    while read pkgpath pkgname maintainer; do
	echo "$maintainer $pkgpath"
	awk -v m=$maintainer '
	   { print m, $1}' "${REPORT1_DIR}/$(get_log_dir $pkgpath $pkgname)/failed_deps.txt"
    done | sort_uniq |
    awk '{++count [$1]}
    END {for (m in count) print m, count [m] }' |
    sort -k2,2rn |
    head -n "$OFFENDERS_CNT" |
    awk '{
	printf "<tr><td>%-27s</td> <td align=\"right\">%4d</td></tr>\n", $1, $2
    }'

    printf '</table>\n'
}

report_html_failed_pkgs (){
    # failed packages
    cat <<EOF
<hr>
<h3><a name="failed"></a>Failed packages</h3>

EOF
    printf '%s' '<table width="100%">'
    print_failed_pkg "Package" "Breaks" "Stage" "Maintainer" "" |
    sed -e 's,<a[^<>]*>,,' \
	-e 's,</a[^<>]*>,,' \
	-e 's,class="[^"]*",,g' \
	-e 's,[A-Z][a-z ]*,<b>&</b>,g'

    cat "$packages_failed_fn" "$packages_failed_notavail_fn" |
    while read pkgpath pkgname maintainer; do
	breaks=$(count_lines "${REPORT1_DIR}/$(get_log_dir $pkgpath $pkgname)/failed_deps.txt")
	failed_stage=$(get_failed_stage "$pkgpath" "$pkgname")
	echo "$pkgpath $breaks $failed_stage $maintainer $pkgname"
    done |
    sort -k2,2nr -k1,1 |
    while read pkgpath breaks failed_stage maintainer pkgname; do
	if test "$breaks" -eq 0; then
	    breaks=''
	fi
	print_failed_pkg "$pkgpath"  "$breaks" "$failed_stage" "$maintainer" "$pkgname"
    done
    printf '%s\n' "</table>"
}

report_html_failed_deps (){
    # failed dependencies
    cat <<EOF
<hr>
<h3><a name="failed depends"></a>Failed due to dependencies</h3>
EOF

    printf '%s' '<table width="100%">'

    echo ''
    print_failed_deps_pkg 'Package/Dependency' '' 'Maintainer' |
    sed -e 's,<a[^<>]*>,,' \
	-e 's,</a[^<>]*>,,' \
	-e 's,[A-Z][a-z ]*,<b>&</b>,g'
    echo ''

    while read pkgpath pkgname maintainer; do
	fn="${REPORT1_DIR}/$(get_log_dir $pkgpath $pkgname)/failed_because.txt"
	depX=$(head -n "$MAX_FAILED_DEPS_CNT" "$fn")
	print_failed_deps_pkg \
	    "$pkgpath" "$pkgname" "$maintainer" '' ''
	for deppath in $depX; do
	    maintainer=$(pkgpath2maintainer "$deppath")
	    depname=$(pkgpath2pkgname "$deppath")
	    print_failed_deps_pkg '' '' "$maintainer" "$deppath" "$depname"
	done
    done < "$packages_failed_deps_fn"
    printf '%s\n' "</table>"
}

report_html_prev_vs_curr (){
    # previous bulk build vs. this one
    if test -n "$DISTBB_DIFF"; then
	if test -f $bb_history_fn; then
	    bbid_prev=$(tail -2 $bb_history_fn | head -1)
	    bbdir_prev="$REPORTS_DIR/$bbid_prev"
	else
	    bbid_prev=''
	    bbdir_prev='/does/not/exist'
	fi

	if test "$bbid_prev" != "$BUILD_ID" &&
	    test -n "$bbid_prev" &&
	    test -d "$bbdir_prev"
	then
	    cat <<EOF
<hr>
<h3><a name="old vs new"></a>Progress: $bbid_prev vs. $BUILD_ID</h3>
EOF
	    print_distbb_diff_help_msg
	    distbb_diff $bbdir_prev $REPORT1_DIR | distbb_diff2html
	fi
    fi
}

report_html_best_vs_curr (){
    # virtually "best" bulk build vs. this one
    if test -n "$DISTBB_DIFF"; then
	bbid_best=.best_bb
	bbdir_best="$REPORTS_DIR/$bbid_best"

	best_is_ok=''
	if test -d $bbdir_best -a \
	    -f $bbdir_best/META/packages_built_total.txt -a \
	    -f $bbdir_best/META/pkg_src_summary.txt
	then
	    best_is_ok=1
	fi

	if test -n "$best_is_ok"; then
	    cat <<EOF
<hr>
<h3><a name="best vs new"></a>Progress: "virtually best" bulk build vs. $BUILD_ID</h3>
<p><i>Below '-' means that package was built at least
once in the past, but now fails</i></p>
EOF
	    print_distbb_diff_help_msg
	    distbb_diff $bbdir_best $REPORT1_DIR | distbb_diff2html
	fi
    fi
}

check_filename_to_text (){
    awk -v fn="$1" '
    BEGIN {
        sub(/^check_/, "", fn)
        sub(/[.]txt$/, "", fn)
        gsub(/__/, "\034", fn)
        gsub(/_/, " ",     fn)
        gsub(/\034/, "_",  fn)
        print fn
    }'
}

report_html_checks (){
    # failed packages
    cat <<EOF
<hr>
<h3><a name="checks"></a>Sanity checks and tips</h3>

EOF

    check_files=$(find "$meta_dir" -name 'check_*.txt')

    if test -n "$check_files"; then
	printf '<table width="40%%">'
    fi
#    printf '<tr><td>%-30s</td> <td align="right">%4s</td></tr>\n' '<b>Description</b>' '<b></b>'
    echo ''

    for i in $check_files; do
	if test -s "$i"; then
	    fn=`basename $i`
	    html_fn=${fn%.txt}.html
	    html_i=${i%.txt}.html

	    # report.html
	    msg=`check_filename_to_text "$fn"`
	    printf '<tr><td>%-30s</td> <td align="left"><a href="%s" title="link">list</a></td></tr>\n' "$msg" "$html_fn"

	    # check_*.html
	    {
		html_header

		runawk -f distbb.awk -e '
		/^[^ ]*\/stage_/, $0 == "." {
		    log_analysis = 1
		    if (/^[^ ]*\/stage_/){
			title = $0
			sub(/^.*\/stage_/, "", title)
			sub(/[.]tmp/, "", title)

			link = "../" $0
			sub(/[.]tmp/, ".html", link)

			pkg = $0
			sub(/\/.*/, "", pkg)
			sub(/_/, "/", pkg)
			sub(/_/, " ", pkg)

			printf "<div><a href=\"%s\" title=\"%s\">%s</a></div>\n", link, title, pkg
			print "<pre>"
		    }else if ($0 == "."){
			print "</pre>"
		    }else{
			print quote_html_symbols($0)
		    }
		    next
		}
		{
		    if (NR == 1) print "<pre>"
		    print quote_html_symbols($0)
		}
		END { if (!log_analysis) print "</pre>" }
		' "$i"

		html_footer
	    } > $html_i
	fi
    done

    if test -n "$check_files"; then
	printf '%s\n' "</table>"
    fi
}

report_html_footer (){
    # the end
    printf '<hr>\n'
}

report_html (){
    report_html_header

    report_html_responsible
    report_html_summary
    report_html_notes
    report_html_failed_pkgs
    report_html_failed_deps
    report_html_prev_vs_curr
    report_html_best_vs_curr
    report_html_fails_per_maintainer
    report_html_checks

    report_html_footer
}

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

packages_failed_notavail_cnt=$(count_lines "$packages_failed_notavail_fn.tmp")

packages_failed_scan_cnt=$(count_lines "$packages_failed_scan_fn")
packages_failed_deps_cnt=$(count_lines "$packages_failed_deps_fn.tmp")
packages_failed_cnt=$(count_lines "$packages_failed_fn.tmp")
packages_failed_total=$(($packages_failed_cnt+$packages_failed_deps_cnt+$packages_failed_scan_cnt+$packages_failed_notavail_cnt))

packages_built_cnt=$(count_lines "$packages_built_fn.tmp")
packages_built_prev_cnt=$(count_lines "$packages_built_prev_fn.tmp")
packages_built_total=$(($packages_built_cnt+$packages_built_prev_cnt))

packages_inspected_cnt=$(($packages_failed_total+$packages_built_total))

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

html_header (){
    cat <<EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">

<html> <head> <title>$os</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<link rel="icon" href="../distbb.ico" type="image/x-icon">
<link rel="stylesheet" href="../distbb.css" type="text/css">
</head>

<body>

EOF
}

html_footer (){
    cat <<EOF
</body>
</html>
EOF
}

report_full_html (){
    html_header

    report_html > $REPORT1_DIR/META/tmp/report_body.html
    cat $REPORT1_DIR/META/tmp/report_body.html

    html_footer
}

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

verbose_print "  META/report.html ... "

report_full_html > "$meta_dir/report.html"

verbose_print 'done\n'

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

report_html2txt (){
   awk '
   /Jump to:/, NF == 0 {
      next
   }
   /REMOVE ME/ {
      next
   }
   {
      gsub(/<hr>/, "---------------------------------------------------------------------------")
      gsub(/<!--|-->/, "")
      gsub(/<[^<>]*>/, "")
      gsub(/&[a-z]*;/, "")
   }
   $2 == "PKGBASE" {
      printf "%-4s %-24s %-20s %5s %24s\n\n", "", $1, $2, $3, $4
      next
   }
   /Sanity checks and tips/, /------------/ {
      next
   }
   /Progress:/, /------------/ {
      if ($1 !~ /^[nN]?[-+][dc]?$/ && $1 != "R"){
         print
         next
      }

      sub(/[+]/, "+", $1)
      sub(/[-]/, "-", $1)

      diff    = $1
      pkgpath = $2
      pkgbase = $3
      $1 = $2 = $3 = ""
      vers = substr($0, 4)
      if (match(vers, /[[:alnum:]._]+( -> [[:alnum:]._]+)?$/) > 1){
         breaks = substr(vers, 1, RSTART-2)
         vers   = substr(vers, RSTART, RLENGTH)
      }else{
         breaks = ""
      }

      printf "%-4s %-24s %-20s %5s %24s\n", diff, pkgpath, pkgbase, breaks, vers
      next
   }
   /^Failed packages/, /------------/ {
      if (NF == 4) printf "  %-35s %6s %-34s\n", $1, $2, $4
      else if (NF == 3) printf "  %-35s %6s %-34s\n", $1, "", $3
      else if (NF != 1) print $0
      next
   }
   /Summary/, /Failures per/ {
      if (NF > 0 && $NF ~ /^(list|logs)$/)
         gsub(/list|logs/, "")
      if (/Progress messages/)
         next
   }
   { print }
   ' "$@"
}

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

verbose_print "  META/report{_mini,}.txt ... "

report_txt_fn="$meta_dir/report.txt"
report_html2txt $REPORT1_DIR/META/tmp/report_body.html |
sed 's| *$||' > "$report_txt_fn"

report_mini_txt_fn="$meta_dir/report_mini.txt"
awk -v size_limit="$REPORT_MINI_SIZE_LIMIT" '
BEGIN {
   sz = 0
   size_limit += 0
   cutoff_msg = "\n========= report is cut off because of its size, see a full report ========="
   cutoff_msg = cutoff_msg "====================================================================="
   cutoff_msg_sz = length(cutoff_msg)
}

/Failures per maintainer/, /------/ {
   next
}
/Failed due to dependencies/, /------/ {
   next
}
/Progress:/, /------/ {
   next
}
{
   sz += length($0)+1
   if (sz+cutoff_msg_sz > size_limit){
      print cutoff_msg
      exit
   }
   print
}
' "$report_txt_fn" > "$report_mini_txt_fn"

verbose_print 'done\n'

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

verbose_print "  PKGNAMEs/summary.txt ... "

runawk -v report1="$REPORT1_DIR" -e '
#use "xclose.awk"

NF == 0 {
   summary_dir = report1 "/" pkgpath_m "_" pkgname
   if (0 == system("test -d \"" summary_dir "\"")){
      summary_fn = summary_dir "/summary.txt"
      cvrt = ("pkg_summary4view > " summary_fn)
      print substr(summary, 2) | cvrt
      xclose(cvrt)
   }

   summary = pkgname = ""
   next
}
{
   summary = summary "\n" $0
   if ($0 ~ /PKGNAME=/){
      pkgname = substr($0, 9)
   }
   if ($0 ~ /PKGPATH=/){
      pkgpath_m = substr($0, 9)
      sub(/:.*/, "", pkgpath_m)  # remove options settings
      gsub(/\//, "_", pkgpath_m) # s|/|_|
   }
}
' "$pkg_src_summary_fn"

verbose_print 'done\n'

######################################################################
pkgname_pkgpath2html (){
    # $1 - PKGNAME
    # $2 - PKGPATH
    printf 'PKGNAME: %s<br>\nPKGPATH: %s<br>\n' "$1" "$2"
}
######################################################################

stages_bar (){
    printf '<div>Stages:&nbsp;'
    for t in $TARGETS; do
	if test -s "$pkgdir/stage_$t.tmp"; then
	    if test "$t" = "$stage"; then
		printf "$stage"
	    else
		printf '<a href="stage_%s.html" title="build log for %s">%s</a>\n' \
		    "$t" "$pkgpath" "$t"
	    fi

	    printf '&nbsp;'
	fi
    done
    printf '</div>\n'
}

more_info_bar (){
    short_pkgpath="$(echo $pkgpath | cut -f1 -d:)"
    printf '<div>More info:&nbsp;'
    printf '<a href="summary.txt" title="summary for package %s">summary</a>' \
	"$short_pkgpath"
    printf '&nbsp;'
    printf '<a href="failed_deps.html" title="list of packages failed because of this one">failed_deps</a></div>\n'
}

link_to_top (){
    printf '<div class="up-link"><a href="../META/report.html" title="report.html">Up</a></div>\n'
}

bars (){
    # $1: "u" - link at the top, "d" - link at the bottom
    if test $1 = u; then link_to_top; fi
    stages_bar
    more_info_bar
    if test $1 = d; then link_to_top; fi
}

quote_html_symbols (){
    runawk -f distbb.awk -e '{ print quote_html_symbols($0) }' "$@"
}

stage2html (){
    html_header

    pkgdir="$(echo $1 | sed 's,/[^/]*$,,')"
    pkgpath="$(echo $1 | sed 's,^.*/\([^/]*/[^/]*\)/[^/]*$,\1,')"
    stage="$(echo $1 | sed 's,^.*stage_\(.*\)[.].*$,\1,')"

    bars u

    # log itself
    printf "<pre>"
    quote_html_symbols "$1"
    printf "</pre>"

    if test "$(count_lines $1)" -gt 20; then
	bars d
    fi

    html_footer
}

gen_stage_xxx (){
    verbose_print "  PKGNAMEs/stage_xxx.html ... "

    find "$REPORT1_DIR" -name 'stage_*.tmp' |
    while read stage_fn; do
	if test -s $stage_fn; then
	    html_fn=${stage_fn%%.tmp}.html
	    stage2html "$stage_fn" >"$html_fn"
	fi
    done

    verbose_print "done\n"
}

gen_stage_xxx

######################################################################
failed_because_txt2html (){
    # $1 - PKGNAME
    # $2 - PKGPATH
    html_header

    short_pkgpath="$(echo $2 | cut -f1 -d:)"

    printf '<p>'

    pkgname_pkgpath2html "$1" "$2"
    printf 'More info:&nbsp;'
    printf '<a href="summary.txt" title="summary for package">summary</a><br>'
    printf 'List of failed dependencies:<br>\n'

    depX="$(cat ${REPORT1_DIR}/$(get_log_dir $pkgpath $pkgname)/failed_because.txt)"
    for deppath in $depX; do
	depname=$(pkgpath2pkgname "$deppath")
	stage="$(get_failed_stage $deppath $depname 2>/dev/null || true)"
	if test -n "$stage"; then
	    printf '&nbsp;&nbsp;&nbsp;<a href="../%s/stage_%s.html" title="summary for package">%s</a><br>' \
		"$(get_log_dir $deppath $depname)" "$stage" "$deppath"
	fi
    done

    printf '</p>\n'

    html_footer
}

verbose_print "  PKGNAMEs/info.html ... "

while read pkgpath pkgname maintainer; do
    fn="${REPORT1_DIR}/$(get_log_dir $pkgpath $pkgname)/info.html"
    failed_because_txt2html "$pkgname" "$pkgpath" > "$fn"
done < "$packages_failed_deps_fn"

verbose_print "done\n"

######################################################################
failed_deps_txt2html (){
    # $1 - PKGNAME
    # $2 - PKGPATH
    html_header

    short_pkgpath="$(echo $2 | cut -f1 -d:)"

    printf '<p>'

    pkgname_pkgpath2html "$1" "$2"
    printf 'List of packages failed because of this one:<br>\n'

    depX="$(cat ${REPORT1_DIR}/$(get_log_dir $pkgpath $pkgname)/failed_deps.txt)"
    for deppath in $depX; do
	depname=$(pkgpath2pkgname "$deppath")
	printf '&nbsp;&nbsp;&nbsp;<a href="../%s/info.html" title="information for package">%s</a><br>' \
	    "$(get_log_dir $deppath $depname)" "$deppath"
    done

    printf '</p>\n'

    html_footer
}

verbose_print "  PKGNAMEs/failed_deps.html ... "

cat "$packages_failed_fn" "$packages_failed_notavail_fn" |
while read pkgpath pkgname maintainer; do
    fn="${REPORT1_DIR}/$(get_log_dir $pkgpath $pkgname)/failed_deps.html"
    failed_deps_txt2html "$pkgname" "$pkgpath" > "$fn"
done

verbose_print "done\n"

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