summaryrefslogtreecommitdiff
path: root/diff/diff-daemon.sh
blob: daa258d886d4d3fe1417b65eb81e37d8b9d4a153 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/sh
name='diff-daemon'
ver=1.1
#  Copyright (C) 2009 Luke Shumaker
#  
#  This file is part of diff-daemon.
#  
#  diff-daemon 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, or (at your option) any later version.
#  
#  diff-daemon 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 diff-daemon; see the file COPYING.
#  If not, write to the Free Software Foundation,
#  59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

# count the number of times the char "$1" occurs in string "$2"
# note that the string can't contain any newlines.
chCount() {
	char=$1
	string="$2"
	echo "$string" | tr ':' '\n' | wc -l
	# this returns the correct answer+1
	# this is OK for now, because it is only used for inequality
}

l_common() {
	# If they're the same, just echo one of them
	if [ "$1" = "$2" ]; then
		str="$1"
	else
		# it will be faster if the string with fewer #s is first
		if [ "`chCount ':' "$1"`" -lt "`chCount ':' "$2"`" ]; then
			a0="$1"
			a1="$2"
		else
			a0="$2"
			a1="$1"
		fi
		str=''
		#for int in ${a0//:/: }; do # bash optimized version
		for int in `echo "$a0" | tr ':' ' '`; do
			str="$str`echo ":$a1" | sed -n "s/.*:\($int:\).*/\1/p"`"
		done
	fi
	echo "$str"
}

l_add() {
	str=''
	#for int in ${1//:/: }; do # bash optimized version
	for int in `echo "$1" | tr ':' ' '`; do
		str="$str`expr "$int" + "$2"`:"
	done
	echo "$str"
}

# add a new line with contents $1
print_new() {
	echo "n$1"
}

# insert $1 blank lines
# if $1 is not defined, default to 1
print_blank() {
	echo "b${1-1}"
}

print_copy() {
	if [ -n "$m1" ]; then
		m1="`echo "$m1" | sed 's/:.*//'`"
		if [ "$mi" = '0' ]; then
			mp="${m1}"
		else
			m0="`expr $m1 - $mi`"
			mp="$m0,$m1"
		fi
		if [ -z "`sed -n "$mp p" "$file0"`" ]; then
			print_blank "`expr 1 + $m1 - $m0`"
		else
			echo "c$mp"
		fi
	fi
}

print_uniq() {
	if [ -n "$line" ]; then
		print_new "$line"
	else
		print_blank
	fi
}

matches() {
	grep -n "^$line$" "$file0" | cut -d ':' -f 1 | tr '\n' ':'
}

# main loop

file0="$1"
file1="$2"

i='1'
c=`wc -l "$file1" | cut -d ' ' -f 1`
while [ "$i" -le "$c" ]; do
	line="`sed -n "$i p" "$file1"`"
	
	# it should be simpler to count the `:'s in $m1
	ms="`echo "$m1" | sed 's/:.*//'`"
	#echo "ms=$ms"
	if [ -n "$m1" ] && [ "$m1" = "$ms:" ]; then
		#echo "yay $ms"
		ret1="`expr $ms + 1`"
		if [ "$line" = "`sed -n "$ret1 p" "$file0"`" ]; then
			m1="$ret1:"
			mi=`expr "$mi" + 1`
		else
			print_copy
			m1="`matches`"
			if [ -n "$m1" ]; then
				# start new trend
				mi='0'
			else
				print_uniq
			fi
		fi
	else
		matches="`matches`"
		if [ -n "$matches" ]; then
			ret1=`l_common "$matches" "$(l_add "$m1" '1')"`
			if [ -n "$ret1" ]; then
				# continue trend
				m1="$ret1"
				mi=`expr "$mi" + 1`
			else
				# start new trend
				print_copy
				m1="$matches"
				mi='0'
			fi
		else
			print_copy
			m1=''
			print_uniq
		fi
	fi
	
	i=`expr $i + 1`
done
print_copy