]> andersk Git - config-package-dev.git/blame - divert.sh.in
Relicense divert.sh.in under the MIT license
[config-package-dev.git] / divert.sh.in
CommitLineData
ab8e2a67
TA
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#
936cb21e 14# Version: 4.0
ab8e2a67 15#
9447ea83
AK
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#
ab8e2a67
TA
39
40package=#PACKAGE#
41
42ours=#DEB_DIVERT_EXTENSION#
43theirs=#DEB_DIVERT_EXTENSION#-orig
44
936cb21e 45divert_link_divert()
ab8e2a67 46{
936cb21e
TA
47 file=$1
48 ourfile=$2
49 theirfile=$3
dd49a2d5 50 if ! LC_ALL=C dpkg-divert --list "$package" | \
ab8e2a67
TA
51 grep -xFq "diversion of $file to $theirfile by $package"; then
52 dpkg-divert --divert "$theirfile" --rename --package "$package" --add "$file"
53 fi
936cb21e
TA
54}
55
56divert_link_symlink()
57{
58 file=$1
59 ourfile=$2
60 theirfile=$3
ab8e2a67
TA
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
936cb21e 70divert_link()
ab8e2a67
TA
71{
72 prefix=$1
73 suffix=$2
74
75 file=$prefix$suffix
76 ourfile=$prefix$ours$suffix
77 theirfile=$prefix$theirs$suffix
936cb21e
TA
78 divert_link_divert "$file" "$ourfile" "$theirfile"
79 divert_link_symlink "$file" "$ourfile" "$theirfile"
80}
ab8e2a67 81
936cb21e
TA
82divert_remove()
83{
84 file=$1
85 ourfile=""
86 theirfile=$2
87 divert_link_divert "$file" "$ourfile" "$theirfile"
88}
89
90undivert_unlink_symlink()
91{
92 file="$1"
93 ourfile="$2"
94 theirfile="$3"
ab8e2a67
TA
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
936cb21e
TA
102}
103
104undivert_unlink_divert()
105{
106 file="$1"
ab8e2a67
TA
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
936cb21e
TA
114undivert_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"
bb79498c 124 undivert_unlink_divert "$file"
936cb21e
TA
125}
126
127undivert_unremove()
128{
129 file=$1
130 undivert_unlink_divert "$file"
131}
132
50bf7563
AK
133check_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
dd49a2d5 142 if LC_ALL=C dpkg-divert --list "$package" | \
50bf7563
AK
143 grep -xFq "diversion of $file to $theirfile by $package"; then
144 undivert_unlink "$prefix" "$suffix"
145 fi
146}
147
148check_undivert_unremove()
149{
150 file=$1
151 removedfile=$2
dd49a2d5 152 if LC_ALL=C dpkg-divert --list "$package" | \
50bf7563
AK
153 grep -xFq "diversion of $file to $removedfile by $package"; then
154 undivert_unremove "$file"
155 fi
156}
This page took 0.391831 seconds and 5 git commands to generate.