#!/bin/sh
# uncomment the following line on NT (change the path as needed) and move
# it to the first line of this file.
#!c:/appls/mksnt/mksnt/sh

#
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
# 
# The contents of this file are subject to the Mozilla Public License Version 
# 1.1 (the "License"); you may not use this file except in compliance with 
# the License. You may obtain a copy of the License at 
# http://www.mozilla.org/MPL/
# 
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
# 
# The Original Code is mozilla.org code.
# 
# The Initial Developer of the Original Code is
# Netscape Communications Corporation.
# Portions created by the Initial Developer are Copyright (C) 1996-2003
# the Initial Developer. All Rights Reserved.
# 
# Contributor(s):
#   Mark Smith <MarkCSmithWork@aol.com>
# 
# Alternatively, the contents of this file may be used under the terms of
# either of the GNU General Public License Version 2 or later (the "GPL"),
# or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
# 
# ***** END LICENSE BLOCK ***** 

#
# cvsdl: perform a 'cvs diff' to examine the last change made to a file on the
#   current branch.
#
# usage: cvsdl [-n] [-v] [-revs] [cvs-diff-options] [files...]
#   where:
#      "revs" is a number that indicates how many revisions to go back
#              when diffing (default is 1)
# and  "-n"   shows the cvs diff command that would be executed without
#              actually executing it.
# and  "-v"   enables verbose output.
#
#   e.g., cvsdl -c mozilla/directory/c-sdk/ldap/Makefile
#   e.g., cvsdl -n mozilla/directory/c-sdk/ldap/Makefile
#   e.g., cvsdl -3 mozilla/directory/c-sdk/ldap/Makefile
#
# if no files are listed, all in the current directory are diff'd.
# cvs-diff-options can also be passed in the CVSDLOPTIONS environment variable.
#
# Known Bug: the detection of "first revision on a branch" is broken.

DIFFOPTIONS="$CVSDLOPTIONS"
REVSBACK=1
EXECUTE=1
VERBOSE=0

while [ $# -ne 0 ]; do
    if [ "$(expr "$1" : '\(.\)')" != "-" ]; then
    break;
    fi
    OPTION=$(echo "$1" | sed -e 's/^-//')
    echo "$OPTION" | grep '^[0-9]*$' > /dev/null 2>&1
    if [ $? -eq 0 ]; then
    REVSBACK=$OPTION
    elif [ "$OPTION" = "n" ]; then
    EXECUTE=0
    elif [ "$OPTION" = "v" ]; then
    VERBOSE=1
    else
    if [ -z "$DIFFOPTIONS" ]; then
        DIFFOPTIONS=$1
    else
        DIFFOPTIONS="$DIFFOPTIONS "$1
    fi
    fi
    shift;
done

if [ $# -eq 0 ]; then
    # Requires array support to fix this issue, not provided by this shell
    # shellcheck disable=SC2125
    FILES=*
else
    FILES=$*
fi


if [ $VERBOSE -ne 0 ]; then
    echo revsback="$REVSBACK"
    echo diffoptions="$DIFFOPTIONS"
    echo files="$FILES"
fi

for FN in $FILES; do
    if [ -d "$FN" ]; then
    echo "Skipping directory $FN"
    else
    CURREV=$(cvs -n stat "$FN" | grep 'Working revision' | awk '{print $3}')
    RC=$?
    if [ "$RC" != 0 ]; then
        exit $RC;
    fi
    if [ "$CURREV" = "No" ]; then
        echo "$FN:  No CVS entry"
        continue;
    fi

    LASTDIGITOFCUR=$(echo "$CURREV" | awk -F. '{print ($NF)}')
    LASTDIGITOFPREV=$(echo "$CURREV" | awk -F. '{print ($NF)-1}')
    if [ "$LASTDIGITOFCUR" -lt "$REVSBACK" ]; then
        echo "$FN:  there are only $LASTDIGITOFCUR revision(s) on this branch.  Nothing to diff."
        continue;
    elif [ "$LASTDIGITOFCUR" -eq "$REVSBACK" ]; then
        PREVREV=$(echo  "$CURREV" | sed -e 's/\.[0-9]*\.[0-9]*$//')
        echo "$FN:  diffing with branch point..."
    elif [ "$LASTDIGITOFPREV" -eq 0 ]; then
        PREVREV=$(echo  "$CURREV" | sed -e 's/\.[0-9]*\.[0-9]*$//')
        if [ -z "$PREVREV" ]; then
        echo "$FN:  the current revision is the first one.  Nothing to diff."
        continue;
        fi
#       echo "$FN:  the current revision is the first one on this branch..."
#       diff PREVREV and CURREV
    else
        PREVREV=$(echo "$CURREV" | awk -F. '{for (i=1; i < NF; ++i) \
            printf( "%s.", $i ); print ($NF)-revsback}' revsback="$REVSBACK")
    fi
    echo cvs -n diff "$DIFFOPTIONS" -r "$PREVREV" -r "$CURREV" "$FN"
    if [ $EXECUTE -ne 0 ]; then
        cvs -n diff "$DIFFOPTIONS" -r "$PREVREV" -r "$CURREV" "$FN"
    fi
    fi
done
