aboutsummaryrefslogtreecommitdiffstats
path: root/System/apt-downgrade-src-package-to-official-archive.sh
blob: 1c2e24fa363413112a99516a3336f328a631fd57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/bin/bash
########################################################################################################################
# apt-downgrade-src-package-to-official-archive.sh
########################################################################################################################
#
# All rights reserved Ⓒ 2017-2023 sdxlive.com
#
# Written by Jean-Christophe Manciot <jcmanciot@sdxlive.com>
#
# Licensed under a GPLv3 License.
# You may not use this file except in compliance with the License. You may obtain a copy of the License at
#
#    https://www.gnu.org/licenses/gpl-3.0.md
#
# The licensor cannot revoke these freedoms as long as you follow the license terms.
#
# Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. 
# You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
#
########################################################################################################################

########################################################################################################################
#
# Downgrading all installed binary packages listed from a source package with a version ending with +<os_version> or 
# -<os_version> to the package version provided by the official Debian/Ubuntu repositories.
#
########################################################################################################################
#
# Requirements:
# ------------
#
# - We assume that apt and lsb-release alongside all their dependencies are already installed.
# - We expect that 'deb-src' URIs are set in your sources.list
#
########################################################################################################################
#
# Parameters:
# ----------
#
# - <src_package>: source package from which all binary packages are listed
# - <os_version>: optional binary packages version ending with +<os_version> or -<os_version>
#                 it defaults to the current OS version.
#                 For instance:
#       * 10|10.0       matches buster Debian distribution
#       * 11|11.0       matches bullseye Debian distribution
#       * 19.04         matches disco Ubuntu distribution
#       * 19.10         matches eoan Ubuntu distribution
#       * 20.04         matches focal Ubuntu distribution
#       * 20.10         matches groovy Ubuntu distribution
# - <auto>: optional, one of
#       * true|yes: the binary packages will be downgraded without any user's confirmation
#                   Use with caution.
#       * false|no: the binary packages will be downgraded only with user's confirmation for each source package
#                   (default)
# - <hold>: optional, one of
#       * true|yes: all installed binary packages are held once they've been downgraded
#                   (default)
#       * false|no: no binary package is held once it has been downgraded
# - <versions_group>: optional, one of
#       + positive: downgrading only the subset of binary packages with a version ending with +<os_version> 
#       + negative: downgrading only the subset of binary packages with a version ending with -<os_version> 
#       + all: downgrading all binary packages with a version ending with +<os_version> or -<os_version> 
#              (default)
# - <apt_non_interactive_enabled>: boolean
#       * true: 
#               + debconf/frontend is set to Noninteractive at the beginning of the script then
#               + debconf/frontend is set to Dialog at the end of the script
#       * false: debconf/frontend is not modified
#                (default)
#
#######################################################################################################################
#
# Usage example:
# -------------
#
# apt-downgrade-src-package-to-official-archive.sh util-linux
#
########################################################################################################################
# set -x

src_package=$1
os_version=$2
auto=$3
hold=$4
versions_group=$5
apt_non_interactive_enabled=$6
PARAMETERS_NUMBER=1

case "$#" in
        0)
                end_of_help_message_line_number=$(awk '/^# set -x/{ print NR; exit }' $(which apt-downgrade-src-package-to-official-archive.sh))
                ((end_of_help_message_line_number--))
                if [[ (-n $(apt-cache -q=0 2>&1 policy zenity | grep Installed)) && (! ($(apt-cache -q=0 2>&1 policy zenity | grep Installed) =~ none)) ]]; then
                        awk -v var=$end_of_help_message_line_number 'NR >= 2 && NR <= var' $(which apt-downgrade-src-package-to-official-archive.sh) | zenity --text-info --title "apt-downgrade-src-package-to-official-archive.sh help" --width 1350 --height 1000
                else
                        awk -v var=$end_of_help_message_line_number 'NR >= 2 && NR <= var' $(which apt-downgrade-src-package-to-official-archive.sh)
                fi
                exit 1
                ;;
        $PARAMETERS_NUMBER)
                case "$1" in
                        -h|--help)
                                end_of_help_message_line_number=$(awk '/^# set -x/{ print NR; exit }' $(which apt-downgrade-src-package-to-official-archive.sh))
                                ((end_of_help_message_line_number--))
                                if [[ (-n $(apt-cache -q=0 2>&1 policy zenity | grep Installed)) && (! ($(apt-cache -q=0 2>&1 policy zenity | grep Installed) =~ none)) ]]; then
                                        awk -v var=$end_of_help_message_line_number 'NR >= 2 && NR <= var' $(which apt-downgrade-src-package-to-official-archive.sh) | zenity --text-info --title "apt-downgrade-src-package-to-official-archive.sh help" --width 1350 --height 1000
                                else
                                        awk -v var=$end_of_help_message_line_number 'NR >= 2 && NR <= var' $(which apt-downgrade-src-package-to-official-archive.sh)
                                fi
                                exit 1
                                ;;
                        *)
                                os_distribution=$(lsb_release -a  2>/dev/null | grep "Distributor ID" | cut -d ':' -f 2)
                                os_distribution=${os_distribution//[[:space:]]/}
                                os_version=$(lsb_release -a  2>/dev/null | grep "Release" | cut -d ':' -f 2)
                                os_version=${os_version//[[:space:]]/}
                                if [[ ($os_distribution == Debian) && ($os_version =~ testing) ]]; then
                                        os_version='12.0'
                                fi
                                auto=false
                                hold=true
                                versions_group=all
                                apt_non_interactive_enabled=false
                                ;;
                esac
                ;;
        $((PARAMETERS_NUMBER+1)))
                auto=false
                hold=true
                versions_group=all
                apt_non_interactive_enabled=false
                ;;
        $((PARAMETERS_NUMBER+2)))
                hold=true
                versions_group=all
                apt_non_interactive_enabled=false
                ;;
        $((PARAMETERS_NUMBER+3)))
                versions_group=all
                apt_non_interactive_enabled=false
                ;;
        $((PARAMETERS_NUMBER+4)))
                apt_non_interactive_enabled=false
                ;;
        $((PARAMETERS_NUMBER+5)))
                ;;
        *)
                echo -e ''$_{1..180}'\b-'
                echo Wrong number of parameters: $(basename $0) $@
                echo -e ''$_{1..180}'\b-'
                exit 1
                ;;
esac

dpkg-lock.sh apt update

if [[ $apt_non_interactive_enabled == true ]]; then
        echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections
fi        

# Installing dependencies needed to play an alert or a completion sound when necessary
dpkg-lock.sh apt -y install alsa-utils emacspeak pidgin-data

# Listing all installed binary packages from <src_package>
unset bin_packages
readarray -t bin_packages < <(apt-list-bin-packages-from-src.sh "$src_package")
unset bin_packages_list
for bin_package in "${bin_packages[@]}"
do
        if [[ -n $(apt list --installed 2>/dev/null | grep "$bin_package") ]]; then
                if [[ -z "$bin_packages_list" ]]; then
                        bin_packages_list="^${bin_package}/"
                else
                        bin_packages_list="${bin_packages_list}|^${bin_package}/"
                fi
        fi
done

# Downgrading all installed binary packages listed from the source package with a version ending with +<os_version> or 
# -<os_version> to the package version provided by the official Debian/Ubuntu repositories
apt-downgrade-bin-packages-to-official-archive.sh "$os_version" full "$auto" "$versions_group" "$bin_packages_list" '' $apt_non_interactive_enabled
return_code=$?
if [[ $return_code -ne 0 ]]; then
        aplay /usr/share/emacs/site-lisp/emacspeak/sounds/classic/alert-user.wav
        if [[ $apt_non_interactive_enabled == true ]]; then
                echo 'debconf debconf/frontend select Dialog' | debconf-set-selections
        fi        
        exit 1
fi

# Previous script may have set debconf/frontend to Dialog
if [[ $apt_non_interactive_enabled == true ]]; then
        echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections
fi        

if [[ ("$hold" == true) || ("$hold" == yes) ]]; then
        # Holding all installed binary packages listed from the source package
        dpkg-lock.sh apt-mark hold "$bin_packages_list"
fi

aplay /usr/share/sounds/purple/login.wav
if [[ $apt_non_interactive_enabled == true ]]; then
        echo 'debconf debconf/frontend select Dialog' | debconf-set-selections
fi        
exit 0