OSDN Git Service

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