#!/bin/ksh
#

timestamp="$(date +"%a %b %e %Y") $(grep -E "^$(whoami):" /etc/passwd | cut -d: -f5) <$(whoami)@$(hostname)>"
wrote_release=

stash=

function write_stash {
   if [ -n "${stash}" ]; then
      # stash always has a leading space
      stash="${stash# }"
      # Remove leading filename(s)
      stash="${stash#*([A-Za-z0-9\.\[\]\*-/, ]): }"
      echo "$(echo "${stash}" | fold -s -w79 | sed -e 's/^/  /' -e '1,1s/./-/')"
      stash=
   fi
}

while IFS= read line; do
   if [ "${line#	}" = "${line}" ]; then
      # No leading tab
      write_stash
      timestamp="${line}"
   else
      # Leading tab
      line="${line#	}"
      if [ "${line#\* Released }" != "${line}" ]; then
         # Released line
         write_stash
         version="${line#\* Released }"
         version="${version%.}"
         version="${version% internally}"
         echo "* ${timestamp} ${version}"
         wrote_release=T
      else
         # Normal line
         if [ "${line#\* }" != "${line}" ]; then
            # * line (starting comment)
            write_stash
            if [ -z "${wrote_release}" ]; then
               echo "* ${timestamp} dev"
               wrote_release=T
            fi
            line="${line#\* }"
            line="${line%%*( )}"
            stash="${stash} ${line}"
         else
            # continuation line
            line="${line#  }"
            line="${line%%*( )}"
            stash="${stash} ${line}"
         fi
      fi
   fi
done
