#!/bin/sh
# tla-changelogs-to-log -- Construct an arch patch-log from ChangeLog changes
# Usage: tla-changelogs-to-log SUMMARY
#
#  Copyright (C) 2003  Miles Bader <miles@gnu.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Written by Miles Bader <miles@gnu.org>
# 
#--
# This output an arch patch-log constructed from the changed portitions
# of changelog files in the tree.  The patch-log is output to stdout.

# (---- beginning of hdr.shpp ----)
# hdr.shpp

me=`basename $0`

bindir='/usr/local/bin'
AWK='/usr/bin/nawk'; export AWK
TLA='tla'; export TLA
SED='/bin/sed'; export SED

TLA_TOOL_PFX="${bindir+$bindir/}"
export TLA_TOOL_PFX

# (---- end of hdr.shpp ----)
# (---- beginning of simple-cmd-line.shpp ----)
# simple-cmd-line.shpp -- Simple command-line processing for no-option commands

# (---- beginning of cmd-line.shpp ----)
# cmd-line.shpp -- Command-line helper functions for shell scripts

script="$0"
case "$script" in
  */*) ;;
  *)   script="${TLA_TOOL_PFX}$script";;
esac

usage ()
{
  $SED -n -e '/^\([^#]\|#-* *$\)/{s@.*@Usage: '"$me"' [--help]@p;q;}'	\
         -e '/^# *Usage:/,/^# *$/{s/^# //p;q;}'				\
     < "$script"
}

short_help ()
{
  $SED -n -e '/^\([^#]\|-*# *$\|# *Usage:\)/q'				\
	 -e '/^#!/d;s/^.*-- */# /;s/^#[ 	]*//p'			\
     < "$script" | fmt
}

help_body ()
{
  $SED -n '/^ *$/q;/^#-/,/^[^#]/s/^#\( \|$\)//p' < "$script"
}

help ()
{
  usage
  short_help
  echo ''
  help_body
}

version ()
{
  echo "$me $1"
  $SED -n '/^[^#]/q;/^#-/q;s/^# *\(Written by\)/\
\1/p' < "$script"
  $SED -n '/^[^#]/q;/^#-/q;s/^# *\(Copyright\)/\
\1/p' < "$script"
}

unrec_opt ()
{
  echo 1>&2 "$me: unrecognized option "\`"$1'"
  echo 1>&2 "Try "\`"$me --help' for more information."
}

cmd_line_err ()
{
  usage 1>&2
  echo 1>&2 "Try "\`"$me --help' for more information."
}

long_opt_val ()
{
  echo "$1" | $SED 's/^[^=]*=//'
}

short_opt_val ()
{
  echo "$1" | $SED 's/^-.//'
}

# (---- end of cmd-line.shpp ----)

case "$1" in
  --help)
    usage
    short_help
    echo ''
    echo "      --help           display this help message and exit"
    echo ''
    help_body
    exit 0
    ;;
  --)
    shift;;
  -*)
    unrec_opt "$1"; exit 1;;
esac

# (---- end of simple-cmd-line.shpp ----)

test "$#" -eq 0 || { cmd_line_err; exit 1; }

TREE_ROOT=`$TLA tree-root 2>/dev/null` || { echo 1>&2 "$me: Not in an arch project tree"; exit 2; }
cd "$TREE_ROOT"

$TLA changes --diffs |
$AWK '
 BEGIN {
   # Line prefixes used for header/body lines from changelog (with any
   # original whitespace removed).  The following is a slight tweak
   # (three spaces instead of a tab before body lines) to the normal
   # changelog format to allow more room for the file prefixes we add.
   #
   hdr_pfx = ""
   body_pfx = "   "

   exit_status = 1
 }

 /^[+][+][+] [^ \t]*\/[Cc]hange[Ll]og([^a-zA-Z\/][^\/]*$|$)/ {
   in_cl = 1
   after_file = 0
   hdr = 0
   prefix = $2
   sub (/[Cc]hange[Ll]og([^a-zA-Z\/][^\/]*$|$)/, "", prefix)
   sub (/^mod\//, "", prefix)
   next
 }

 in_cl && /^(---|@@) / { next }
 in_cl && /^[+ ][^ \t]/ { hdr = $0; blank = 1; after_file = 0; next }
 in_cl && /^[+ ][ \t]*$/ { blank = 1; after_file = 0; next }
 in_cl && /^[+]/ {
   sub (/^[+][ \t]*/, "")

   if (blank && prev) {
     print ""
     continuing_file_list = 0
   }

   if (hdr) {
     sub (/^[+ ]/, hdr_pfx, hdr)
     print hdr
     print ""
     hdr = 0
   }

   if ($1 == "*" || continuing_file_list) {
     filename_field = ($1 == "*" ? 2 : 1)
     while (filename_field <= NF) {
       if ($filename_field ~ /^[[(]/)
	 break;
       $filename_field = prefix $filename_field
       if ($filename_field ~ /:$/)
	 break;
       filename_field++
     }
     continuing_file_list = (filename_field > NF)
     file_line = (filename_field > 1)
     after_file = 1
   } else
     file_line = 0

   # Add a prefix to non-file lines
#   if (!file_line && after_file && !continuing_file_list)
#     $0 = "  " $0

   print body_pfx $0
   exit_status = 0

   prev = 1
   blank = 0
   next
 }
 { in_cl = 0 }

 END { exit (exit_status) }
'

