OSDN Git Service

Version 3.0.0+toward_3.0.1_20130605073650
[portsreinstall/current.git] / lib / libfileedit.sh
1 #!/bin/sh -e
2 # ==============================================================================
3 # portsreinstall library script
4 # - Editing operations on files -
5 # Copyright (C) 2013 Mamoru Sakaue, MwGhennndo, All Rights Reserved.
6 # This software is distributed under the 2-Clause BSD License.
7 # ==============================================================================
8
9 # ============= Remove lines exactly matching a string from a file =============
10 fileedit_rm_a_line ()
11 {
12         local item dstpath tmpfile pattern
13         item=$1
14         dstpath=$2
15         [ -n "$item" ] || return 0
16         [ -e "$dstpath" ] || return 0
17         tmpfile=$dstpath.fileedit_rm_a_line.tmp
18         pattern=`str_escape_regexp "$item"`
19         grep -v -E "^$pattern$" "$dstpath" 2> /dev/null > $tmpfile || :
20         mv "$tmpfile" "$dstpath"
21 }
22
23 # ============= Add a line to a file if no matching one exists in the file =============
24 fileedit_add_a_line_if_new ()
25 {
26         local item dstpath tmpfile pattern dstdir
27         item=$1
28         dstpath=$2
29         [ -n "$item" ] || return 0
30         [ -n "$dstpath" ] || return 0
31         tmpfile=$dstpath.fileedit_add_a_line_if_new.tmp
32         pattern=`str_escape_regexp "$item"`
33         if [ -e "$dstpath" ]
34         then
35                 cp "$dstpath" "$tmpfile"
36                 grep -q -E "^$pattern$" "$dstpath" \
37                         || echo "$item" >> $tmpfile
38         else
39                 dstdir=`dirname "$dstpath"`
40                 [ -d "$dstdir" ] || mkdir -p "$dstdir"
41                 echo "$item" > $tmpfile
42         fi
43         mv "$tmpfile" "$dstpath"
44 }
45
46 # ============= Remove lines exactly matching a string from multiple files =============
47 fileedit_rm_a_line_from_files ()
48 {
49         local item
50         item=$1
51         [ -n "$item" ] || return 0
52         while read filepath
53         do
54                 fileedit_rm_a_line "$item" "$filepath"
55         done
56 }
57
58 # ============= Add a line to each of multiple files if no matching one exists in the file =============
59 fileedit_add_a_line_to_files_if_new ()
60 {
61         local item
62         item=$1
63         [ -n "$item" ] || return 0
64         while read filepath
65         do
66                 fileedit_add_a_line_if_new "$item" "$filepath"
67         done
68 }
69
70 # ============= Add multiple lines to a file for what don't match any existing ones in the file =============
71 fileedit_add_lines_if_new ()
72 {
73         local dstpath advance tmpfile tmpclip origin dstdir
74         dstpath=$1
75         advance=$2
76         tmpfile=$dstpath.fileedit_add_lines_if_new.tmp
77         tmpclip=${TMPDIR}/fileedit_add_lines_if_new:add
78         if [ -e "$dstpath" ]
79         then
80                 while read origin
81                 do
82                         grep -q -E "^`str_escape_regexp \"$origin\"`$" "$dstpath" \
83                                 || echo "$origin"
84                 done > $tmpclip
85                 if [ "@$advance" = @advance ]
86                 then
87                         cat "$tmpclip" "$dstpath"
88                 else
89                         cat "$dstpath" "$tmpclip"
90                 fi > $tmpfile
91         else
92                 dstdir=`dirname "$dstpath"`
93                 [ -d "$dstdir" ] || mkdir -p "$dstdir"
94                 cat > $tmpfile
95         fi
96         mv "$tmpfile" "$dstpath"
97 }
98
99 # ============= Combine (possibly missing) lists into one =============
100 fileedit_combine_lists ()
101 {
102         cat "$@" 2> /dev/null | sort -u || :
103 }
104
105 # ============= Manipulate old and new lines from an old and a new versions of a file =============
106 fileedit_manipulate_old_new_lines ()
107 {
108         local oldsrc newsrc olddiff newdiff
109         oldsrc=$1
110         newsrc=$2
111         olddiff=$3
112         newdiff=$4
113         tmpdiff=${TMPDIR}/fileedit_manipulate_old_new_lines::diff
114         [ -e "$oldsrc" ] || oldsrc=/dev/null
115         [ -e "$newsrc" ] || newsrc=/dev/null
116         diff "$oldsrc" "$newsrc" > $tmpdiff && return 1
117         grep '^<' "$tmpdiff" | sed 's/^< //' > $olddiff || :
118         grep '^>' "$tmpdiff" | sed 's/^> //' > $newdiff || :
119 }
120
121 # ============= Manipulate old lines from an old and a new versions of a file =============
122 fileedit_manipulate_old_lines ()
123 {
124         local oldsrc newsrc
125         oldsrc=$1
126         newsrc=$2
127         tmpdiff=${TMPDIR}/fileedit_manipulate_old_lines::diff
128         [ -e "$oldsrc" ] || oldsrc=/dev/null
129         [ -e "$newsrc" ] || newsrc=/dev/null
130         diff "$oldsrc" "$newsrc" > $tmpdiff && return 1
131         grep '^<' "$tmpdiff" | sed 's/^< //'
132 }
133
134 # ============= Manipulate new lines from an old and a new versions of a file =============
135 fileedit_manipulate_new_lines ()
136 {
137         local oldsrc newsrc
138         oldsrc=$1
139         newsrc=$2
140         tmpdiff=${TMPDIR}/fileedit_manipulate_new_lines::diff
141         [ -e "$oldsrc" ] || oldsrc=/dev/null
142         [ -e "$newsrc" ] || newsrc=/dev/null
143         diff "$oldsrc" "$newsrc" > $tmpdiff && return 1
144         grep '^>' "$tmpdiff" | sed 's/^> //'
145 }
146
147 # ============= Check whether removed lines exists in updating from an old and a new versions of a file =============
148 fileedit_exists_old_lines ()
149 {
150         local oldsrc newsrc
151         oldsrc=$1
152         newsrc=$2
153         tmpdiff=${TMPDIR}/fileedit_exists_old_lines::diff
154         [ -e "$oldsrc" ] || oldsrc=/dev/null
155         [ -e "$newsrc" ] || newsrc=/dev/null
156         diff "$oldsrc" "$newsrc" > $tmpdiff && return 1
157         grep -q '^<' "$tmpdiff"
158 }