#!/bin/sh
#
#   wfBNg̃\[X̂ύX̂t@CdiffƂA
#   /tmp/diff_<uid>ɏo͂
# 
#   olddir      : t@ĈfBNg
#   -f filename : ̃t@ĈݍsȂꍇ
#   -step       : et@C̏CXebv̂ݎ擾ꍇ
#
#   99/09/28 V1.01  -step support by oga
#   00/02/26 V1.02  "{" support (for vi)
#   00/09/04 V1.03  -diff <diff_cmd>
#   00/10/25 V1.04  -u(unified diff) support
#   00/11/04 V1.05  mgdiff support
#   01/02/02 V1.06  -f multifile support
#   02/02/24 V1.07  check delete file
#   04/03/08 V1.08  -u/-C wƂ  
#   07/03/07 V1.09  -u/-C wNGƂ  
#

WK=/tmp/diff_`id -u -n`
COPT=-C4			# diff̑O̍s
UOPT=
DIFF=diff
STEP=0

if [ "$1" = "" -o "$1" = "-h" ]; then
	echo "usage : diffd [-w] [-u] <olddir> [-step] [{-f filename ...| num<4>}]"
	echo "        <olddir> : rt@ĈfBNg"
	echo "        -w       : 󔒂𖳎čƂ"
	echo "        -u       : unified diff"
	echo "        -f       : wt@ĈݍƂ"
	echo "        -step    : ύXXebv\"
	echo "        num      : ̑Oɏo͂s"
	exit 1
fi

if [ "$1" = "-diff" ]; then
	DIFF="$2"
	shift
	shift
fi

if [ "$1" = "-w" ]; then
	DIFF="$DIFF -w"
	echo "󔒂͖čƂ܂"
	shift
fi

if [ "$1" = "-c" ]; then
	COBOL=1
	echo "COBOL option"
	shift
fi

if [ "$1" = "-u" ]; then
	echo "unified format ō܂"
	UF=1
	UOPT=-u
	COPT=
	shift
fi

if [ "$1" = "-m" ]; then    #V1.05
    MGDIFF=1
    shift
fi

OLDDIR=$1
shift

if [ "$1" = "-step" ]; then  #V1.01
	STEP=1               #V1.01
	shift                #V1.01
fi                           #V1.01

if [ "$1" = "-f" ]; then
	shift
	FILENM=$*
	shift
else
    if [ "$1" != "" -a "$UF" != "1" ]; then  # V1.08-C V1.09-C
    #if [ "$1" != "" ]; then
	NUM=$1
	COPT=-C$1
	echo "diff͑O $NUM sƂ܂B"
	shift
    fi
fi

if [ -f $WK ]; then
	rm $WK
fi

if [ "$FILENM" = "" ]; then
	LIST=`ls $OLDDIR`
else
	LIST=`(cd $OLDDIR; ls ${FILENM}* )`
fi

for i in $LIST
do
	echo $i |awk '{printf"  %-16s: ",$1}'

    if [ ! -f $i ]; then
	    echo "<= 폜܂B"
		continue
	fi

	$DIFF $OLDDIR/$i $i > /dev/null
	if [ $? -eq 1 ]; then
		echo "ύXĂ܂BdiffƂ܂B"
		echo "{"		                >> $WK
		echo " t@C : `pwd`/$i"		>> $WK
		echo "<ύXO>"				>> $WK
		echo "   ȗ"				>> $WK
		echo ""					>> $WK
		echo "<ύX>"				>> $WK

		#$DIFF $COPT $OLDDIR/$i $i		>> $WK
		# diff: conflicting specifications of output style }~ V1.08-C
		# -u  -ČɂȂƖɂȂĂ܂ 
		$DIFF $COPT $UOPT $OLDDIR/$i $i  2>&1 |\
		    grep -v "conflicting specifications of output style" >> $WK

		echo -e "\n"				>> $WK

		if [ $STEP -eq 1 ]; then    # V1.01
		    c $WK                   # V1.01
		    echo -e "\n"            # V1.01
		    rm $WK                  # V1.01
		fi                          # V1.01

		if [ "$MGDIFF" = "1" ]; then    # V1.05
		    mgdiff $OLDDIR/$i $i
		fi

	else
		echo "ύX܂B"
	fi
done
echo "I!!"

if [ $STEP -ne 1 ]; then
  echo "  ʂ ${WK} ɏo͂܂B"
fi
