# # divert_link # # Ensures that the file is properly diverted to # .divert-orig by this package, and becomes a # symbolic link to either .divert (default) or # .divert-orig. # # undivert_unlink # # Undoes the action of divert_link specified # above. # # Version: 4.0 # # Copyright © 2008–2012 Tim Abbott and Anders # Kaseorg # # Permission is hereby granted, free of charge, to any person # obtaining a copy of this software and associated documentation files # (the “Software”), to deal in the Software without restriction, # including without limitation the rights to use, copy, modify, merge, # publish, distribute, sublicense, and/or sell copies of the Software, # and to permit persons to whom the Software is furnished to do so, # subject to the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # package=#PACKAGE# ours=#DEB_DIVERT_EXTENSION# theirs=#DEB_DIVERT_EXTENSION#-orig divert_link_divert() { file=$1 ourfile=$2 theirfile=$3 if ! LC_ALL=C dpkg-divert --list "$package" | \ grep -xFq "diversion of $file to $theirfile by $package"; then dpkg-divert --divert "$theirfile" --rename --package "$package" --add "$file" fi } divert_link_symlink() { file=$1 ourfile=$2 theirfile=$3 if [ ! -L "$file" ] && [ ! -e "$file" ]; then ln -s "$(basename "$ourfile")" "$file" elif [ ! -L "$file" ] || \ [ "$(readlink "$file")" != "$(basename "$ourfile")" -a \ "$(readlink "$file")" != "$(basename "$theirfile")" ]; then echo "*** OMINOUS WARNING ***: $file is not linked to either $(basename "$ourfile") or $(basename "$theirfile")" >&2 fi } divert_link() { prefix=$1 suffix=$2 file=$prefix$suffix ourfile=$prefix$ours$suffix theirfile=$prefix$theirs$suffix divert_link_divert "$file" "$ourfile" "$theirfile" divert_link_symlink "$file" "$ourfile" "$theirfile" } divert_remove() { file=$1 ourfile="" theirfile=$2 divert_link_divert "$file" "$ourfile" "$theirfile" } undivert_unlink_symlink() { file="$1" ourfile="$2" theirfile="$3" if [ ! -L "$file" ] || \ [ "$(readlink "$file")" != "$(basename "$ourfile")" -a \ "$(readlink "$file")" != "$(basename "$theirfile")" ]; then echo "*** OMINOUS WARNING ***: $file is not linked to either $(basename "$ourfile") or $(basename "$theirfile")" >&2 else rm -f "$file" fi } undivert_unlink_divert() { file="$1" if [ ! -L "$file" ] && [ ! -e "$file" ]; then dpkg-divert --remove --rename --package "$package" "$file" else echo "Not removing diversion of $file by $package" >&2 fi } undivert_unlink() { prefix=$1 suffix=$2 file=$prefix$suffix ourfile=$prefix$ours$suffix theirfile=$prefix$theirs$suffix undivert_unlink_symlink "$file" "$ourfile" "$theirfile" undivert_unlink_divert "$file" } undivert_unremove() { file=$1 undivert_unlink_divert "$file" } check_undivert_unlink() { prefix=$1 suffix=$2 file=$prefix$suffix ourfile=$prefix$ours$suffix theirfile=$prefix$theirs$suffix if LC_ALL=C dpkg-divert --list "$package" | \ grep -xFq "diversion of $file to $theirfile by $package"; then undivert_unlink "$prefix" "$suffix" fi } check_undivert_unremove() { file=$1 removedfile=$2 if LC_ALL=C dpkg-divert --list "$package" | \ grep -xFq "diversion of $file to $removedfile by $package"; then undivert_unremove "$file" fi }