]> andersk Git - config-package-dev.git/blob - divert.sh.in
Relicense divert.sh.in under the MIT license
[config-package-dev.git] / divert.sh.in
1 #
2 # divert_link <prefix> <suffix>
3 #
4 #   Ensures that the file <prefix><suffix> is properly diverted to
5 #   <prefix>.divert-orig<suffix> by this package, and becomes a
6 #   symbolic link to either <prefix>.divert<suffix> (default) or
7 #   <prefix>.divert-orig<suffix>.
8 #
9 # undivert_unlink <prefix> <suffix>
10 #
11 #   Undoes the action of divert_link <prefix> <suffix> specified
12 #   above.
13 #
14 # Version: 4.0
15 #
16 # Copyright © 2008–2012 Tim Abbott <tabbott@mit.edu> and Anders
17 # Kaseorg <andersk@mit.edu>
18 #
19 # Permission is hereby granted, free of charge, to any person
20 # obtaining a copy of this software and associated documentation files
21 # (the “Software”), to deal in the Software without restriction,
22 # including without limitation the rights to use, copy, modify, merge,
23 # publish, distribute, sublicense, and/or sell copies of the Software,
24 # and to permit persons to whom the Software is furnished to do so,
25 # subject to the following conditions:
26 #
27 # The above copyright notice and this permission notice shall be
28 # included in all copies or substantial portions of the Software.
29 #
30 # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
31 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
34 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
35 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
36 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37 # SOFTWARE.
38 #
39
40 package=#PACKAGE#
41
42 ours=#DEB_DIVERT_EXTENSION#
43 theirs=#DEB_DIVERT_EXTENSION#-orig
44
45 divert_link_divert()
46 {
47     file=$1
48     ourfile=$2
49     theirfile=$3
50     if ! LC_ALL=C dpkg-divert --list "$package" | \
51         grep -xFq "diversion of $file to $theirfile by $package"; then
52         dpkg-divert --divert "$theirfile" --rename --package "$package" --add "$file"
53     fi
54 }
55
56 divert_link_symlink()
57 {
58     file=$1
59     ourfile=$2
60     theirfile=$3
61     if [ ! -L "$file" ] && [ ! -e "$file" ]; then
62         ln -s "$(basename "$ourfile")" "$file"
63     elif [ ! -L "$file" ] || \
64         [ "$(readlink "$file")" != "$(basename "$ourfile")" -a \
65           "$(readlink "$file")" != "$(basename "$theirfile")" ]; then
66         echo "*** OMINOUS WARNING ***: $file is not linked to either $(basename "$ourfile") or $(basename "$theirfile")" >&2
67     fi
68 }
69
70 divert_link()
71 {
72     prefix=$1
73     suffix=$2
74
75     file=$prefix$suffix
76     ourfile=$prefix$ours$suffix
77     theirfile=$prefix$theirs$suffix
78     divert_link_divert "$file" "$ourfile" "$theirfile"
79     divert_link_symlink "$file" "$ourfile" "$theirfile"
80 }
81
82 divert_remove()
83 {
84     file=$1
85     ourfile=""
86     theirfile=$2
87     divert_link_divert "$file" "$ourfile" "$theirfile"
88 }
89
90 undivert_unlink_symlink()
91 {
92     file="$1"
93     ourfile="$2"
94     theirfile="$3"
95     if [ ! -L "$file" ] || \
96         [ "$(readlink "$file")" != "$(basename "$ourfile")" -a \
97           "$(readlink "$file")" != "$(basename "$theirfile")" ]; then
98         echo "*** OMINOUS WARNING ***: $file is not linked to either $(basename "$ourfile") or $(basename "$theirfile")" >&2
99     else
100         rm -f "$file"
101     fi
102 }
103
104 undivert_unlink_divert()
105 {
106     file="$1"
107     if [ ! -L "$file" ] && [ ! -e "$file" ]; then
108         dpkg-divert --remove --rename --package "$package" "$file"
109     else
110         echo "Not removing diversion of $file by $package" >&2
111     fi
112 }
113
114 undivert_unlink()
115 {
116     prefix=$1
117     suffix=$2
118
119     file=$prefix$suffix
120     ourfile=$prefix$ours$suffix
121     theirfile=$prefix$theirs$suffix
122
123     undivert_unlink_symlink "$file" "$ourfile" "$theirfile"
124     undivert_unlink_divert "$file"
125 }
126
127 undivert_unremove()
128 {
129     file=$1
130     undivert_unlink_divert "$file"
131 }
132
133 check_undivert_unlink()
134 {
135     prefix=$1
136     suffix=$2
137
138     file=$prefix$suffix
139     ourfile=$prefix$ours$suffix
140     theirfile=$prefix$theirs$suffix
141
142     if LC_ALL=C dpkg-divert --list "$package" | \
143         grep -xFq "diversion of $file to $theirfile by $package"; then
144         undivert_unlink "$prefix" "$suffix"
145     fi
146 }
147
148 check_undivert_unremove()
149 {
150     file=$1
151     removedfile=$2
152     if LC_ALL=C dpkg-divert --list "$package" | \
153         grep -xFq "diversion of $file to $removedfile by $package"; then
154         undivert_unremove "$file"
155     fi
156 }
This page took 0.040144 seconds and 5 git commands to generate.