-
Notifications
You must be signed in to change notification settings - Fork 0
/
lines.subr
146 lines (115 loc) · 3.78 KB
/
lines.subr
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
check_line () {
filename=$1
#pattern=$(escape_regex $2)
pattern=$2
if [ `pcregrep -Mie "$pattern" "$filename" | wc -l` -gt 0 ]; then
echo 1
else
echo 0
fi
}
insert_after () {
filename=$1
pattern=$(escape_regex $2)
text=$(normalize "$3")
size=$(cat $filename | wc -l)
patched=$(pcregrep -Mine "$2(\n|.)*${text}" $filename | wc -l)
if [ $patched -gt 0 ]; then
debug "after" "already patched, skipping...";
return;
fi
debug "insert_after" "filename: $filename"
debug "insert_after" "pattern : $pattern"
debug "insert_after" "text : $text"
debug "insert_after" "filesize: $size"
match=$(pcregrep -Mine "$2" $filename)
msize=$(pcregrep -Mine "$2" $filename | grep "^[0-9]*:" | tail -n1 | cut -d: -f1)
msize2=$(pcregrep -Mine "$2" $filename | pcregrep -Mie "^[0-9]+:(\n|.)*")
msize3=$(echo "$msize2" | tail -n1 | grep "^[0-9]*:")
if [ $(echo "$msize2" | tail -n1 | grep "^[0-9]*:" | wc -l) -eq 0 ] && [ "$msize" != "" ]; then debug "after" "need to count $msize"; msize=$(( $msize+`echo "$msize2"|wc -l` )); fi
if [ "$msize" == "" ]; then debug "insert_after" "nothing matched, skipping"; return; fi
debug "after" "matched lines: $msize"
start=$(echo $match | cut -d: -f1)
end=$(( 1 + $msize ))
debug "after" "inserting at line: ${end}"
# sed "${end}i\\
#${text}" $filename > /tmp/ppf.tmp
# mv /tmp/ppf.tmp $filename
echo "$text" > /tmp/tmp.text
marker="__MARKER__"
sed "${end}i\\
$marker\\
" $filename > /tmp/ppf1.tmp
sed -e "/$marker/r /tmp/tmp.text" /tmp/ppf1.tmp | sed -e "/$marker/d" > /tmp/ppf.tmp
mv /tmp/ppf.tmp $filename
rm /tmp/tmp.text
rm /tmp/ppf1.tmp
}
insert_before () {
filename=$1
pattern=$(escape_regex $2)
text=$(normalize $3)
# #$(escape $(normalize "$3"))
size=$(cat $filename | wc -l | tr -d ' ')
#debug "insert_before" "patching matching: ${text}(\n|.)*${pattern}"
patched=$(pcregrep -Mie "${text}(\n|.)*${pattern}" $filename | wc -l)
if [ $patched -gt 0 ]; then
debug "insert_before" "already patched, skipping...";
return;
fi
debug "insert_before" "filename: $filename"
debug "insert_before" "pattern : $pattern"
#debug "insert_before" "text : $text"
debug "insert_before" "filesize: $size"
match=$(pcregrep -Mine "$2" $filename)
msize=$(pcregrep -Mine "$2" $filename | tail -n1 | cut -d: -f1)
debug "insert_before" "matched lines: $msize"
start=$(echo $match | cut -d: -f1)
if [ "$msize" == "" ]; then return; fi
debug "insert_before" "inserting at line: ${start}"
#result=$(sed "${start}i\\
#${text}" $filename)
echo "$text" > /tmp/tmp.text
marker="__MARKER__"
sed "${start}i\\
$marker\\
" $filename > /tmp/ppf1.tmp
sed -e "/$marker/r /tmp/tmp.text" /tmp/ppf1.tmp | sed -e "/$marker/d" > /tmp/ppf.tmp
mv /tmp/ppf.tmp $filename
rm /tmp/tmp.text
rm /tmp/ppf1.tmp
}
fix_eof_line () {
if [ `tail -c 2 $1` ]; then
debug "fixeof" "fixing missing line"
echo "" >> $1
fi
}
update () {
filename=$1
pattern=$(escape_regex $2)
text=$(normalize $3)
sed "s/${pattern}/${text}/" $filename > /tmp/ppf.tmp
mv /tmp/ppf.tmp $filename
}
upsert_before () {
filename=$1
pattern=$(escape_regex $2)
text=$(normalize $3)
pattern2=$4 #$(escape_regex $4)
text2=$5 #$(normalize $5)
#debug "upsert" "filename: $filename"
#debug "upsert" "pattern : $pattern"
#debug "upsert" "text : $text"
#debug "upsert" "pattern2: $pattern2"
#debug "upsert" "text2 : $text2"
#debug "upsert_before" "checking if $pattern exists in $filename..."
#debug "upsert_check" "$(check_pf_line $2)"
if [ $(check_pf_line "$2") -eq 0 ]; then
debug "upsert_before" "calling insert_before()"
insert_before $filename $pattern2 "$text2"
else
debug "upsert_before" "calling update()"
update $filename "$pattern" "$text"
fi
}