#!/bin/sh

BASE=/beehive
LBASE=/beehive/logs
SBASE=/beehive/stats
PROOT=/ALT
PLATFORMS="Sisyphus" # p6
# TODO more than 2 arch
ARCHS="x86_64 i586"
WORKDIR="${WORKDIR:-$(dirname "$0")}"
INFODIR="${INFODIR:-info}"
ERRDIR="${ERRDIR:-errors}"
UFBDIR="${UFBDIR:-ufb}"
PATDIR="${PATDIR:-pattern}"
COMMENTS="${COMMENTS:-comments.txt}"
# TODO change this
COMMURL="http://www.altlinux.org/Sisyphus/FTBFS"
touch "$COMMENTS"

export LC_ALL=C

eecho() { test -n "$QUIET" || echo -e "$@" >&2; }

# get and format comments
getcomments() { # comments_url
  local CU="$1"
  ( LC_ALL=ru_RU.UTF-8 curl -s "$CU" ) | sed -rn '
/class="mw-headline" id="Packages"/,/class="mw-headline"/{
/^[^d]*<td>.*<.td><td.*/s@^[^d]*<td> *([^<]*[^ ]) *<.td><td *([^>]*)>(([^<]+|<[^/]|</[^Tt])+).*@\1 \2 \3@p
}' | sort
}

checklog() { # package_biuld_log
  sed -rn '
  /beehive: total build limit.*exceeded/{s/.*/limit/p;q}
  /[1-9][0-9]* CPU time limit exceeded/{s/.*/cpu/p;q}
  /hasher-priv: master: idle time limit.* exceeded/{s/.*/hang/p;q}
  /hsh-rebuild: .* sisyphus_check failed/{s/.*/check/p;q}
  #
  /^E: .*xorg/{s/.*/xorg/p;q}
  /^E: .*emacs/{s/.*/*emacs/p;q}
  /error: .*boost[:'"'"']/{s/.*/boost/p;q}
  /g.kmm.*error/{s/.*/gtkmm/p;q}
  #
  /File not found:.*.share.man/{s/.*/man_extension/p;q}
  /makeinfo: command not found/{s/.*/makeinfo/p;q}
  /^E:.*not found|^E: Couldn.t find|^E: .*no installation candidate|^hsh-install: Failed to calculate package file list|configure: error: Can.t locate/{s/.*/unmet/p;q}
  /ERROR: .*RPATH/{s/.*/rpath/p;q}
  /note: .* is defined in DSO .* so try adding it/{s/.*/DSO/p;q}
  /error: .*was not declared in this scope/{s/.*/c++/p;q}
  /error: .*[Gg][Tt][Kk]/{s/.*/gtk/p;q}
  /error: .*[Xx][MmSs][Ll]/{s/.*/xml/p;q}
  $s/.*//p
  ' < "$1"
}

seplist() { # repo arch
  local REPO="$1"
  local AR="$2"

  local pkg
  local fpkg
  local epkg
  mkdir -p "$ERRDIR/$REPO"
  ls "$LBASE/$REPO-$AR/latest/error/" | while read pkg; do
    fpkg="$PROOT/$REPO/files/SRPMS/$(echo "$pkg" | sed -r 's/[0-9]+://').src.rpm"
    test -r "$fpkg" && {
      epkg="$LBASE/$REPO-$AR/latest/error/$pkg"
      cp "$epkg" "$ERRDIR/$REPO/"
      echo "$pkg $fpkg"
    } || echo "$pkg NO" >&3
  done
}
    
mklists() { # repo
  local REPO="$1"
  local LIST0=list0.tmp
  local LIST1=list1.tmp
  local LIST2=list.$REPO.x86_64+i586
  local LIST4=list.$REPO.i586
  local LIST6=list.$REPO.x86_64
  local LISTE=list.$REPO.no
  local A6="${ARCHS%% *}"
  local A4="${ARCHS##* }"
  # /beehive/logs/Sisyphus-x86_64/latest/error
  # split into x86_64, i586 and x86_64+i586
  seplist "$REPO" "$A6" > $LIST0 3>> $LISTE
  seplist "$REPO" "$A4" > $LIST1 3>> $LISTE
  join -o 0,1.2 $LIST0 $LIST1 > $LIST2
  join -v1 $LIST0 $LIST1 > $LIST6
  join -v2 $LIST0 $LIST1 > $LIST4
  echo "$LIST2 $LIST6 $LIST4 $LISTE"
}

mkinfo() { # package-list-file repo arch
  local REPO="$2"
  local AR="$3"
  local IDIR="$INFODIR/$REPO"
  local UDIR="$UFBDIR/$REPO"
  local PDIR="$PATDIR/$REPO"
  local pkg
  local fpkg
  mkdir -p "$IDIR" "$UDIR" "$PDIR"
  # /ALT/Sisyphus/files/SRPMS/
  while read pkg fpkg; do
    rpmquery --queryformat "%{NAME}\n%{SUMMARY}\n" -i -p "$fpkg" | {
      read name
      eecho -n  "\r$name                            "
      read summary
      echo "$name" | join -2 2 - "$SBASE/$REPO-$AR/ufb-2" > "$UDIR/$pkg"
      echo "$name" | join - "$SBASE/$REPO-$AR/ftbfs-joined" > "$PDIR/$pkg"
      checklog "$ERRDIR/$REPO/$pkg" >> "$PDIR/$pkg"
      echo "$summary" >> "$PDIR/$pkg"
      echo "$name" | join - "$COMMENTS" | sed 's/[^ ]* *//' >> "$PDIR/$pkg"
      cat > "$IDIR/$pkg"
    }
  done < "$1"
  echo "<h3>${1##list.} $(stat -c "%z" $SBASE/$REPO-$AR/ftbfs-joined)</h3>"
  htmltstart "Description" "Age/Log" Comment "UFB" Labet Maintainers
  while read pkg fpkg; do
    {
      read name version age maint
      read flags
      read summary
      comment="`cat`"
      # TODO превратить summary в balloon
      # пакет <возраст=лог> комментарии <зависимости> причина сопровождающий 
      ln -sf "$pkg" "$IDIR/$pkg.txt"
      ln -sf "$pkg" "$ERRDIR/$REPO/$pkg.txt"
      ln -sf "$pkg" "$UDIR/$pkg.txt"
      htmltr "<nobr>$name</nobr>:::$summary:::$IDIR/$pkg.txt" "$age:::$ERRDIR/$REPO/$pkg.txt" "$comment" "$(wc -l < "$UDIR/$pkg"):::$UDIR/$pkg.txt" "$flags" "$maint"
    } < "$PDIR/$pkg"
  done < "$1"
  htmltend Total "" "`wc -l < $1`" "" "" ""
}

htmlstart() { # title
  cat << @@@
  <html><head>
  <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
  <title>$1</title>
  <script type="text/javascript" src="sorttable.js"></script>
  </head>
  <body>
  <h1>$1</h1>
  You can add your comments <a href="$COMMURL">at altlinux.org</a><p/>
@@@
}

htmlend() {
  echo "</body></html>"
}

htmltstart() { # title_fields
  echo '<table class="sortable">'
  echo "<thead>"
  htmltr "$@" | sed 's/td>/th>/g'
  echo "</thead>"
  echo "<tbody>"
}

htmltend() { # totals
  test "$#" -le 0 || {
    echo "<tfoot>"
    htmltr "$@" | sed 's/td>/th>/g'
    echo "</tfoot>"
  }
  echo "</tbody>"
  echo "</table>"
}

htmltr() {
  local F
  local Q
  local M
  local A
  local S
  Q='"'
  echo -n "<tr>"
  for F; do
    A=""
    case "$F" in
      bgcolor=*|style=*) A=" ${F%% *}"; F="${F#* }";;
    esac
    case "$F" in
      *:::*:::*) M="${F#*:::}"; M="${M%%:::*}";
	     S="<a href=$Q${F##*:::}$Q title=$Q$M$Q>${F%%:::*}</a>";;
      *:::*) S="<a href=$Q${F##*:::}$Q>${F%%:::*}</a>";;
      *) S="$F";;
    esac
    echo -n "<td$A>$S</td>"
  done
  echo "</tr>"
}

# TODO more thorough
mknone() { # filelist
  echo "<h3>Not in repo</h3>"
  htmltstart "Package" "Status (now unused)"
  while read l s; do htmltr "$l" "$s"; done < "$1"
  htmltend Total "`wc -l < $1`"
}

cd "$WORKDIR" || exit 1
rm -rf list.*.* "$INFODIR" "$ERRDIR" "$UFBDIR" "$PATDIR"
getcomments "$COMMURL" > "$COMMENTS"
htmlstart "mkFTBFS `date`"
for R in $PLATFORMS; do
  eecho "               $R:"
  for LFILE in $(mklists "$R"); do
    eecho "     $LFILE"
    case "$LFILE" in
      *.no) # not in repo anymore
        mknone "$LFILE" "$R"
        ;;
      *+*) mkinfo "$LFILE" "$R" "${LFILE##*+}" ;;
      *.*) mkinfo "$LFILE" "$R" "${LFILE##*.}" ;;
      *) eecho "WTF is $LFILE?"; break ;;
    esac
    eecho -n "\r"
  done
done
htmlend
# /beehive/stats/Sisyphus-x86_64/ftbfs-joined
