@@ -51,11 +51,17 @@ update_lists() {
5151 local ppa=${1:- }
5252 local ppa_search=${2:- }
5353 local list=
54- status_file=/tmp/os_lists
54+ local status_file=/tmp/os_lists
55+ local hash_cmd
5556 if [[ -n " $ppa " && -n " $ppa_search " ]]; then
5657 list=" $list_dir " /" $( basename " $( grep -lr " $ppa_search " " $list_dir " ) " ) "
57- status_file=/tmp/" $( echo -n " $ppa_search " | shasum -a 256 | cut -d ' ' -f 1) "
58- elif [ -e " $list_file " ] && grep -Eq ' ^deb |^Types deb' " $list_file " ; then
58+ hash_cmd=" $( command -v sha256sum || command -v shasum) "
59+ if [ -n " $hash_cmd " ]; then
60+ status_file=/tmp/" $( echo -n " $ppa_search " | $hash_cmd | awk ' {print $1}' ) "
61+ else
62+ status_file=/tmp/os_lists_$( date +%s)
63+ fi
64+ elif [ -e " $list_file " ] && grep -Eq ' ^deb |^Types: *deb' " $list_file " ; then
5965 list=" $list_file "
6066 fi
6167 if [ ! -e " $status_file " ]; then
@@ -64,6 +70,36 @@ update_lists() {
6470 fi
6571}
6672
73+ # Determine whether deb822 sources are the default on this system.
74+ get_sources_format () {
75+ if [ -n " $sources_format " ]; then
76+ echo " $sources_format "
77+ return
78+ fi
79+ sources_format=deb
80+ if [ -e " $list_dir " /ubuntu.sources ] || [ -e " $list_dir " /debian.sources ]; then
81+ sources_format=" deb822"
82+ else
83+ find " $list_dir " -type f -name ' *.sources' | grep -q . && sources_format=" deb822"
84+ fi
85+ echo " $sources_format "
86+ }
87+
88+ # Function to get repo patterns based on format.
89+ get_repo_patterns () {
90+ local list_format=$1
91+ local ppa_url=$2
92+ local package_dist=$3
93+ local branches=$4
94+ local deb_pattern=" ^deb .*$ppa_url $package_dist .*$branches $"
95+ local deb822_pattern=" ^URIs: $ppa_url $"
96+ if [ " $list_format " = " deb822" ]; then
97+ printf ' %s|%s\n' " $deb822_pattern " " $deb_pattern "
98+ else
99+ printf ' %s|%s\n' " $deb_pattern " " $deb822_pattern "
100+ fi
101+ }
102+
67103# Function to get fingerprint from an Ubuntu PPA.
68104ubuntu_fingerprint () {
69105 ppa=" $1 "
@@ -106,17 +142,32 @@ add_key() {
106142
107143# Function to check if a PPA and its lists exist
108144check_lists () {
109- ppa=$1
110- ppa_search=$2
111- if grep -Eqr " $ppa_search " " $list_dir " ; then
145+ local ppa=$1
146+ local primary=${2:- }
147+ local secondary=${3:- }
148+ local match_pattern=
149+ local match_file=
150+ check_lists_file=
151+ check_lists_pattern=
152+ if [ -n " $primary " ]; then
153+ match_file=$( grep -Elr " $primary " " $list_dir " 2> /dev/null | head -n 1)
154+ [ -n " $match_file " ] && match_pattern=" $primary "
155+ fi
156+ if [ -z " $match_file " ] && [ -n " $secondary " ]; then
157+ match_file=$( grep -Elr " $secondary " " $list_dir " 2> /dev/null | head -n 1)
158+ [ -n " $match_file " ] && match_pattern=" $secondary "
159+ fi
160+ if [ -n " $match_file " ]; then
161+ local list_count
112162 list_count=" $( sudo find /var/lib/apt/lists -type f -name " *${ppa/ \/ / _} *" | wc -l) "
113163 if [ " $list_count " = " 0" ]; then
114- update_lists " $ppa " " $ppa_search "
164+ update_lists " $ppa " " $match_pattern "
115165 fi
116- return 0 ;
117- else
118- return 1 ;
166+ check_lists_file= $match_file
167+ check_lists_pattern= $match_pattern
168+ return 0
119169 fi
170+ return 1
120171}
121172
122173# Function to add a sources list.
@@ -126,19 +177,39 @@ add_list() {
126177 key_source=${3:- " $ppa_url " }
127178 package_dist=${4:- " $VERSION_CODENAME " }
128179 branches=${5:- main}
129- ppa_search=" deb .*$ppa_url $package_dist .*$branches $"
130- if check_lists " $ppa " " $ppa_search " ; then
131- echo " Repository $ppa already exists" ;
132- return 1;
180+ local list_format
181+ list_format=" $( get_sources_format) "
182+ IFS=' |' read -r primary_pattern secondary_pattern <<< " $(get_repo_patterns " $list_format " " $ppa_url " " $package_dist " " $branches " )"
183+ if check_lists " $ppa " " $primary_pattern " " $secondary_pattern " ; then
184+ if [ " $list_format " = " deb822" ] && [ -n " $check_lists_file " ] && [[ " $check_lists_file " = * .list ]]; then
185+ sudo rm -f " $check_lists_file "
186+ else
187+ echo " Repository $ppa already exists" ;
188+ return 1;
189+ fi
190+ fi
191+ arch=$( dpkg --print-architecture)
192+ [ -e " $key_source " ] && key_file=$key_source || key_file=" $key_dir " /" ${ppa/ \/ / -} " -keyring.gpg
193+ add_key " $ppa " " $ppa_url " " $package_dist " " $key_source " " $key_file "
194+ local list_basename=" ${ppa%%/* } " -" $ID " -" ${ppa#*/ } " -" $package_dist "
195+ sudo rm -f " $list_dir " /" ${ppa/ \/ / -} " .list " $list_dir " /" ${ppa/ \/ / -} " .sources " $list_dir " /" $list_basename " .list " $list_dir " /" $list_basename " .sources || true
196+ local list_path
197+ if [ " $list_format " = " deb822" ]; then
198+ list_path=" $list_dir " /" $list_basename " .sources
199+ cat << EOF | sudo tee "$list_path " >/dev/null
200+ Types: deb
201+ URIs: $ppa_url
202+ Suites: $package_dist
203+ Components: $branches
204+ Architectures: $arch
205+ Signed-By: $key_file
206+ EOF
133207 else
134- arch=$( dpkg --print-architecture)
135- [ -e " $key_source " ] && key_file=$key_source || key_file=" $key_dir " /" ${ppa/ \/ / -} " -keyring.gpg
136- add_key " $ppa " " $ppa_url " " $package_dist " " $key_source " " $key_file "
137- sudo rm -rf " $list_dir " /" ${ppa/ \/ / -} " .list || true
138- echo " deb [arch=$arch signed-by=$key_file ] $ppa_url $package_dist $branches " | sudo tee -a " $list_dir " /" ${ppa%%/* } " -" $ID " -" ${ppa#*/ } " -" $package_dist " .list > /dev/null 2>&1
139- update_lists " $ppa " " $ppa_search "
140- . /etc/os-release
208+ list_path=" $list_dir " /" $list_basename " .list
209+ echo " deb [arch=$arch signed-by=$key_file ] $ppa_url $package_dist $branches " | sudo tee " $list_path " > /dev/null 2>&1
141210 fi
211+ update_lists " $ppa " " $primary_pattern "
212+ . /etc/os-release
142213 return 0;
143214}
144215
@@ -148,8 +219,10 @@ check_ppa() {
148219 ppa_url=${2:- " $lpc_ppa /$ppa /ubuntu" }
149220 package_dist=${3:- " $VERSION_CODENAME " }
150221 branches=${4:- main}
151- ppa_search=" deb .*$ppa_url $package_dist .*$branches $"
152- if check_lists " $ppa " " $ppa_search " ; then
222+ local list_format
223+ list_format=" $( get_sources_format) "
224+ IFS=' |' read -r primary_pattern secondary_pattern <<< " $(get_repo_patterns " $list_format " " $ppa_url " " $package_dist " " $branches " )"
225+ if check_lists " $ppa " " $primary_pattern " " $secondary_pattern " ; then
153226 return 0;
154227 else
155228 return 1;
@@ -213,12 +286,22 @@ update_ppa() {
213286 ppa_url=${2:- " $lpc_ppa /$ppa /ubuntu" }
214287 package_dist=${4:- " $VERSION_CODENAME " }
215288 branches=${5:- main}
216- ppa_search=" deb .*$ppa_url $package_dist .*$branches "
217- update_lists " $ppa " " $ppa_search "
289+ local list_format
290+ list_format=" $( get_sources_format) "
291+ IFS=' |' read -r primary_pattern secondary_pattern <<< " $(get_repo_patterns " $list_format " " $ppa_url " " $package_dist " " $branches " )"
292+ if ! grep -El " $primary_pattern " " $list_dir " > /dev/null 2>&1 && [ -n " $secondary_pattern " ]; then
293+ if grep -El " $secondary_pattern " " $list_dir " > /dev/null 2>&1 ; then
294+ primary_pattern=$secondary_pattern
295+ fi
296+ fi
297+ update_lists " $ppa " " $primary_pattern "
218298 . /etc/os-release
219299}
220300
221301# Variables
302+ sources_format=
303+ check_lists_file=
304+ check_lists_pattern=
222305list_dir=' /etc/apt/sources.list.d'
223306list_file=" /etc/apt/sources.list.d/$ID .sources"
224307[ -e " $list_file " ] || list_file=' /etc/apt/sources.list'
0 commit comments