#!/bin/ksh
#------------------------------------------------------------------------------#
# This file is part of PALM.
#
# PALM 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 3 of the License, or (at your option) any later 
# version.
#
# PALM is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# PALM. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright 1997-2014  Leibniz Universitaet Hannover
#------------------------------------------------------------------------------#
#
# Current revisions:
# -----------------
# 
# 
# Former revisions:
# -----------------
# $Id: document_changes 1805 2016-04-05 16:32:34Z maronga $
#
# 1804 2016-04-05 16:30:18Z maronga
# Removed printing of "this is an alpha version"
# 
# 1326 2014-03-21 10:44:31Z maronga
# Initial revision
# 
# Description:
# ------------
# This tool moves text from "Current Revisions:" to "Former Revisions:" and
# adds the svn timestamp. It works with the folders SOURCE and SCRIPTS
#
# Usage:
# a) go to the source directory (e.g. current_version/trunk/SOURCE and 
#    perform "document_changes",
# b) or call "document_changes" directly and add the path to the source code as
#    argument
#    (e.g. document_changes current_version/trunk/SOURCE)
#
# Note:
# The script will only work properly if the two following conditions are met:
# 1) the 2nd line after "Current Revisions:" must contain text,
# 2) the last line before "Former Revisions:" must not contain text.
#------------------------------------------------------------------------------#

#
#-- define variables
    comments=""         #: variable contains the CR text
    comment_char=="!"   #: comment character
    count_changes=0     #: count the number of files with CR text
    count_files=0       #: count the number of files
    filename=""         #: current file name to be scanned
    file_firstchar=""   #: first character of filename
    file_lastchar=""    #: last character of filename
    file_extension=""   #: filename extension
    found_comment=false #: true/false if a CR text was found
    IFS=''              #: set standard delimiter to empty string
    input_dir=""        #: directory of the source code
    line=""             #: containts the current line of filename
    line_count=0        #: counter to the line no. of the file
    line_start=9999999  #: line no. where CR text starts
    line_stop=0         #: line no. where Cr text ends
    line_tmp=""         #: first 19 characters of line
    list_delete=""      #: contains the line no. that must be deleted from CR
    timestamp=""        #: the svn timestamp of the file

#
#-- get input directory
    input_dir=$1
    if  [[ "$input_dir" == "" ]]
    then
       input_dir=`pwd`
    fi
    printf "\n  *** Checking files in $input_dir\n"

    for filename in $input_dir/*; do

#
#--    exclude backup and invisible files
       file_firstchar=${filename:0:1}
       file_lastchar=`echo -n $filename| tail -c -1`
       file_extension=${filename##*.}
       if [[ "$file_firstchar" == "." || "$file_extension" == "x" || "$file_lastchar" == "~" || "$file_extension" == "tar" ]]
       then
          continue
       fi

       (( count_files = count_files + 1 ))

       line_count=0
       found_comment=false
       list_delete=""
       line_start=9999999
       line_stop=0
       comments=""

#
#--    read one line at a time
       while read line
       do

          (( line_count = line_count + 1 ))
          line_tmp=""
          line_tmp=${line:2:17}

#
#--       check if stopping point is reached
          if [[ $line_stop -eq 0 && "$line_tmp" == "Former revisions:" ]]
          then
             line_stop=$line_count
          fi

#
#--       check if starting point is reached
          if [[ $line_start -eq 9999999 && "$line_tmp" == "Current revisions" ]]
          then
             (( line_start = line_count + 2 ))
             comment_char=${line:0:1}
          fi

#
#--       read comment line
          if [[ $line_count -ge $line_start && $line_stop -eq 0 ]]
          then

#
#--          check for empty comments
             line_no_space=`echo ${line:2:10} | sed -e 's/^[ \t]*//'`
             if [[ $line_count -eq $line_start && "$line_no_space" != "" ]]
             then
                printf "\n  *** Revisions found in $filename\n"
                found_comment=true
                cp $filename $filename~
             fi

#
#--          when comments are found, save comment lines to $comments
             if [[ "$found_comment" == true ]]
             then
                comments="$comments\n$line"
                list_delete="$list_delete;${line_count}d"
             fi
          fi

#
#--       get the timestamp from the current revision
          if [[ "$comments" != "" && $line_count -eq $line_stop+2 ]]
          then
            timestamp=`echo $line | cut -d" " -s -f4-7`
            comments="$comment_char $timestamp$comments"
          fi

       done <"$filename"

#
#--    move comments from current revisions to former revisions
       if [[ "$found_comment" == true ]]
       then

          (( count_changes = count_changes + 1 ))

#
#--       insert comments to Former Revisions
          (( line_stop = line_stop + 4 ))
          sed -i "${line_stop}i$comments" $filename

#
#--       delete comments from current revisions and insert two blank lines
          list_delete=${list_delete#?}
          sed -i "$list_delete" $filename
          sed -i "${line_start}i${comment_char} " $filename
          sed -i "${line_start}i${comment_char} " $filename

       fi

    done

#
#-- inform the user about the changes
    if  [[ $count_changes -gt 0 ]]
    then
       printf "\n  *** $count_files documents checked.\n"
       printf "  *** $count_changes changed documents found.\n"
       printf "\n"
       printf "  *** All files in $input_dir were checked.\n"     
       printf "  *** You can now proceed with checking other directories\n"
       printf "      - or you might want to perform svn commit -m 'last commit documented' trunk\n"
       printf "\n"      
       printf "  *** Please do not forget to commit your changes in the changelog \n under https://palm.muk.uni-hannover.de/trac/wiki/doc/tec/changelog !"      
    else
       printf "\n  *** $count_files documents checked.\n"
       printf "  *** No documented modifications found.\n"
    fi
