Repository: termux/termux-tools
Pull request #165: Getting `termux-tools` ready for `apt` 3.0.0
Author: login: TomJo2000
Created: 2025-04-23T22:25:39Z | Merged: 2025-05-11T19:41:23Z

- Part of termux/termux-packages#24212

Apt 3.0.0 makes the "multiline" `deb822` format the standard for package sources.
This PR aims to introduce support for this repo source format in `termux-tools`.

This initial commit only adds support in `pkg`.
I plan to add support in `termux-info` tomorrow, however that turned into half a refactor and I'm tired.

---

The changes to `pkg` introduce two possible return values from `has_repo`.
- `deb822`, the new standard format (determined by the `.sources` file extension)
- `legacy`, the old oneline format (determined by the `.list` file extension)
Support for the legacy oneline `.list` format isn't slated to be removed until 2029[^1],
so it is kept for backwards compatibility in the meantime.
- An empty return string (`""`) indicates that the repo is not present in either format.

I also fixed a couple shellcheck warnings and some indentation.
I may do a more extensive refactor of `pkg` as part of this PR later.

[^1]: https://man.archlinux.org/man/sources.list.5#ONE-LINE-STYLE_FORMAT


Diff:
diff --git a/scripts/pkg.in b/scripts/pkg.in
index 65158cdd..8fc9ea8a 100644
--- a/scripts/pkg.in
+++ b/scripts/pkg.in
@@ -8,6 +8,7 @@ if [[ "$(id -u)" == "0" ]]; then
 fi
 
 # Setup TERMUX_APP_PACKAGE_MANAGER
+# shellcheck source=/dev/null
 source "@TERMUX_PREFIX@/bin/termux-setup-package-manager" || exit 1
 
 MIRROR_BASE_DIR="@TERMUX_PREFIX@/etc/termux/mirrors"
@@ -108,14 +109,22 @@ last_modified() {
 	echo $((now - mtime))
 }
 
+sed_escape_replacement() {
+	local r="$1"
+	r="${r//[\\]/\\\\}" # Escape `\`.
+	r="${r//[\/]/\\\/}" # Escape `/`.
+	r="${r//[&]/\\\&}"  # Escape `&`.
+	echo "$r"
+}
+
 has_repo() {
 	# Check if root-repo or x11-repo are installed
 	repo="$1"
 
-	if [ -f "@TERMUX_PREFIX@/etc/apt/sources.list.d/$repo.list" ]; then
-		echo true
-	else
-		echo false
+	if [[ -f "@TERMUX_PREFIX@/etc/apt/sources.list.d/$repo.sources" ]]; then
+		echo deb822
+	elif [[ -f "@TERMUX_PREFIX@/etc/apt/sources.list.d/$repo.list" ]]; then
+		echo legacy
 	fi
 }
 
@@ -132,6 +141,7 @@ get_mirror_url() {
 	local -r _has_repo_root="$3"
 
 	unset_mirror_variables
+	# shellcheck source=/dev/null
 	source "$_mirror"
 
 	if [[ -z "${MAIN:-}" ]]; then
@@ -178,7 +188,11 @@ get_mirror_weight() {
 
 select_mirror() {
 	local current_mirror
-	current_mirror=$(grep -oE 'https?://[^ ]+' <(grep -m 1 -E '^[[:space:]]*deb[[:space:]]+' @TERMUX_PREFIX@/etc/apt/sources.list) || true)
+	if [ -f "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources" ]; then
+		current_mirror=$(grep -oE 'https?://[^ ]+' <(grep -m 1 -E '^[[:space:]]*URIs:[[:space:]]+' "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources") || :)
+	elif [ -f "@TERMUX_PREFIX@/etc/apt/sources.list" ]; then
+		current_mirror=$(grep -oE 'https?://[^ ]+' <(grep -m 1 -E '^[[:space:]]*deb[[:space:]]+' "@TERMUX_PREFIX@/etc/apt/sources.list") || :)
+	fi
 
 	# Do not update mirror if $TERMUX_PKG_NO_MIRROR_SELECT was set.
 	if [ -n "${TERMUX_PKG_NO_MIRROR_SELECT-}" ] && [ -n "$current_mirror" ]; then
@@ -189,14 +203,14 @@ select_mirror() {
 
 	if [ -d "@TERMUX_PREFIX@/etc/termux/chosen_mirrors" ]; then
 		# Mirror group selected
-		mirrors=($(find @TERMUX_PREFIX@/etc/termux/chosen_mirrors/ -type f ! -name "*\.dpkg-old" ! -name "*\.dpkg-new" ! -name "*~"))
+		mirrors=($(find "@TERMUX_PREFIX@/etc/termux/chosen_mirrors/" -type f ! -name "*\.dpkg-old" ! -name "*\.dpkg-new" ! -name "*~"))
 	elif [ -f "@TERMUX_PREFIX@/etc/termux/chosen_mirrors" ]; then
 		# Single mirror selected
-		mirrors=("$(realpath @TERMUX_PREFIX@/etc/termux/chosen_mirrors)")
+		mirrors=("$(realpath "@TERMUX_PREFIX@/etc/termux/chosen_mirrors")")
 	elif [ -L "@TERMUX_PREFIX@/etc/termux/chosen_mirrors" ]; then
 		# Broken symlink, use all mirrors
 		mirrors=("${MIRROR_BASE_DIR}/default")
-		mirrors+=($(find ${MIRROR_BASE_DIR}/{asia,chinese_mainland,europe,north_america,oceania,russia}/ -type f ! -name "*\.dpkg-old" ! -name "*\.dpkg-new" ! -name "*~"))
+		mirrors+=($(find "${MIRROR_BASE_DIR}"/{asia,chinese_mainland,europe,north_america,oceania,russia}/ -type f ! -name "*\.dpkg-old" ! -name "*\.dpkg-new" ! -name "*~"))
 	else
 		echo "No mirror or mirror group selected. You might want to select one by running 'termux-change-repo'"
 		mirrors=("${MIRROR_BASE_DIR}/default")
@@ -339,29 +353,53 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	if [ -z "$selected_mirror" ]; then
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
 	fi
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822')
+				sed -i -e "s|URIs:.*|$(sed_escape_replacement "URIs: $MAIN")|" \
+				"@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources"
+			;;
+			# There should always be a main repo, so fallback to
+			# creating sources.list if the `main.sources` file is missing
+			*) echo "deb $MAIN stable main" > "@TERMUX_PREFIX@/etc/apt/sources.list";;
+		esac
+
+		case "${has_repo_x11:-}" in
+			'deb822')
+				sed -i -e "s|URIs:.*|$(sed_escape_replacement "URIs: $X11")|" \
+				"@TERMUX_PREFIX@/etc/apt/sources.list.d/x11.sources"
+			;;
+			'legacy') echo "deb $X11 x11 main" > "@TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list";;
+		esac
+
+		case "${has_repo_root:-}" in
+			'deb822')
+				sed -i -e "s|URIs:.*|$(sed_escape_replacement "URIs: $ROOT")|" \
+				"@TERMUX_PREFIX@/etc/apt/sources.list.d/root.sources"
+			;;
+			'legacy') echo "deb $ROOT root stable" > "@TERMUX_PREFIX@/etc/apt/sources.list.d/root.list";;
+		esac
+	)
 }
 
 update_apt_cache() {
 	local current_host
-	current_host=$(head -n 1 <(sed -nE -e 's|^\s*deb\s+https?://(.+)\s+stable\s+main$|\1|p' @TERMUX_PREFIX@/etc/apt/sources.list) || true)
+	if [ -f "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources" ]; then
+		current_host=$(head -n 1 <(sed -nE 's|^\s*URIs:\s+https?://(.+)$|\1|p' "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources") || :)
+	elif [ -f "@TERMUX_PREFIX@/etc/apt/sources.list" ]; then
+		current_host=$(head -n 1 <(sed -nE 's|^\s*deb\s+https?://(.+)\s+stable\s+main$|\1|p' "@TERMUX_PREFIX@/etc/apt/sources.list") || :)
+	fi
 
 	if [ -z "$current_host" ]; then
 		# No primary repositories configured?
@@ -385,7 +423,11 @@ update_apt_cache() {
 	cache_modified=$(last_modified "@TERMUX_CACHE_DIR@/apt/pkgcache.bin")
 
 	local sources_modified
-	sources_modified=$(last_modified "@TERMUX_PREFIX@/etc/apt/sources.list")
+	if [ -f "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources" ]; then
+		sources_modified=$(last_modified "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources")
+	elif [ -f "@TERMUX_PREFIX@/etc/apt/sources.list" ]; then
+		sources_modified=$(last_modified "@TERMUX_PREFIX@/etc/apt/sources.list")
+	fi
 
 	if (( sources_modified <= cache_modified )) || (( cache_modified > 1200 )); then
 		apt update
@@ -394,8 +436,8 @@ update_apt_cache() {
 
 force_check_mirror=false
 if [ "${1-}" = "--check-mirror" ]; then
-    force_check_mirror=true
-    shift 1
+	force_check_mirror=true
+	shift 1
 fi
 
 if [[ $# = 0 || $(echo "$1" | grep "^h") ]]; then
@@ -419,7 +461,7 @@ case "$TERMUX_APP_PACKAGE_MANAGER" in
 			rei*) apt install --reinstall "$@";;
 			se*) select_mirror; update_apt_cache; apt search "$@";;
 			un*|rem*|rm|del*) apt remove "$@";;
-                        upd*) select_mirror; apt update;;
+			upd*) select_mirror; apt update;;
 			up|upg*) select_mirror; apt update; apt full-upgrade "$@";;
 			*) ERROR=true;;
 		esac;;
@@ -435,7 +477,7 @@ case "$TERMUX_APP_PACKAGE_MANAGER" in
 			rei*) pacman -S "$@";;
 			se*) pacman -Sys "$@";;
 			un*|rem*|rm|del*) pacman -Rcns "$@";;
-                        upd*) pacman -Sy "$@";;
+			upd*) pacman -Sy "$@";;
 			up|upg*) pacman -Syu "$@";;
 			*) ERROR=true;;
 		esac;;
diff --git a/scripts/termux-info.in b/scripts/termux-info.in
index f7ec7826..9cfc07fc 100644
--- a/scripts/termux-info.in
+++ b/scripts/termux-info.in
@@ -44,30 +44,33 @@ updates() {
 }
 
 repo_subscriptions_apt() {
-	local main_sources
-	main_sources=$(grep -E '^[[:space:]]*deb[[:space:]]' "@TERMUX_PREFIX@/etc/apt/sources.list")
-
-	if [ -n "$main_sources" ]; then
-		echo "# sources.list"
-		echo "$main_sources"
-	fi
+	local apt_dir="@TERMUX_PREFIX@/etc/apt"
+
+	local filename source_entry repo_package sources_type
+	for filename in "${apt_dir}"/sources.list{,.d/*}; do
+		[[ -f "$filename" ]] || continue
+		case "$filename" in
+			*.sources) sources_type="deb822";;
+			*.list)    sources_type="legacy";;
+			*) continue;; # Not a valid source type, skip
+		esac
+		source_entry="$(<"$filename")"
+		repo_package=$(dpkg -S "$filename" 2>/dev/null | cut -d : -f 1)
+
+		local printf_format=""
+		if [[ -n "$repo_package" ]]; then
+			printf_format="# %s(%s) [%s]\n"
+		else
+			printf_format="# %s%s [%s]\n"
+		fi
 
-	if [ -d "@TERMUX_PREFIX@/etc/apt/sources.list.d" ]; then
-		local filename repo_package supl_sources
-		while read -r filename; do
-			repo_package=$(dpkg -S "$filename" 2>/dev/null | cut -d : -f 1)
-			supl_sources=$(grep -E '^[[:space:]]*deb[[:space:]]' "$filename")
-
-			if [ -n "$supl_sources" ]; then
-				if [ -n "$repo_package" ]; then
-					echo "# $repo_package (sources.list.d/$(basename "$filename"))"
-				else
-					echo "# sources.list.d/$(basename "$filename")"
-				fi
-				echo "$supl_sources"
-			fi
-		done < <(find "@TERMUX_PREFIX@/etc/apt/sources.list.d" -maxdepth 1 ! -type d)
-	fi
+		# shellcheck disable=SC2059
+		printf "${printf_format}" \
+			"${repo_package}" \
+			"${filename/$apt_dir\/}" \
+			"${sources_type}"
+		echo "$source_entry"
+	done
 }
 
 repo_subscriptions_pacman() {
@@ -86,6 +89,7 @@ repo_subscriptions_pacman() {
 }
 
 # Setup TERMUX_APP_PACKAGE_MANAGER
+# shellcheck source=/dev/null
 source "@TERMUX_PREFIX@/bin/termux-setup-package-manager" || exit 1
 
 case "${TERMUX__USER_ID:-}" in ''|*[!0-9]*|0[0-9]*) TERMUX__USER_ID=0;; esac
@@ -96,7 +100,7 @@ output=""
 if [ -n "${TERMUX_VERSION:-}" ]; then
 # Application version is exported in Termux v0.107 or higher only.
 output+="Termux Variables:
-$(compgen -e TERMUX_ | while read v; do echo "${v}=${!v}"; done)
+$(compgen -e TERMUX_ | while read -r v; do echo "${v}=${!v}"; done)
 "
 else
 output+="Termux Variables:
@@ -148,7 +152,7 @@ if [[ "$sdk_version" =~ ^[0-9]+$ ]] && [ "$sdk_version" -ge "26" ]; then
 fi
 TERMUX_PLUGINS="$(pm list packages --user "$TERMUX__USER_ID" $show_version_code 2>&1 </dev/null | grep -E "^package:$escaped_package_name\.[a-zA-Z]" | cut -d ":" -f 2- | grep -vE "^$escaped_package_name\.tapm\.[a-zA-Z]")"
 if [ -n "${TERMUX_PLUGINS:-}" ]; then
-	        output+="$(printf "\nInstalled termux plugins:\n${TERMUX_PLUGINS}\n")"
+	output+="$(printf "\nInstalled termux plugins:\n${TERMUX_PLUGINS}\n")"
 fi
 echo "$output"
 


Reviews:
user: login: twaik
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-04-26T09:59:07Z
user: login: robertkirkman
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-04-26T21:29:32Z
user: login: TomJo2000
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-04-26T21:32:23Z
user: login: Grimler91
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-08T07:22:17Z
user: login: Grimler91
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-08T17:22:47Z
user: login: TomJo2000
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-08T23:41:02Z
user: login: Grimler91
body: This PR looks good to me, I will aim at merging and releasing it tomorrow evening unless there are more questions/comments by then!
state: APPROVED
author_association: MEMBER
submitted_at: 2025-05-10T21:25:11Z
user: login: agnostic-apollo
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T07:32:43Z
user: login: agnostic-apollo
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T07:37:15Z
user: login: agnostic-apollo
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T07:40:20Z
user: login: agnostic-apollo
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T07:42:10Z
user: login: agnostic-apollo
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T08:28:13Z
user: login: TomJo2000
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T11:27:03Z
user: login: TomJo2000
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T11:29:12Z
user: login: agnostic-apollo
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T11:30:15Z
user: login: TomJo2000
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T11:33:03Z
user: login: agnostic-apollo
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T11:38:45Z
user: login: agnostic-apollo
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T11:39:55Z
user: login: TomJo2000
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T11:39:59Z
user: login: agnostic-apollo
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T11:50:07Z
user: login: TomJo2000
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T12:18:46Z
user: login: agnostic-apollo
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T12:20:28Z
user: login: TomJo2000
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T12:23:37Z
user: login: agnostic-apollo
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T12:27:18Z
user: login: agnostic-apollo
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T12:29:00Z
user: login: TomJo2000
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T12:29:03Z
user: login: TomJo2000
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T12:38:54Z
user: login: agnostic-apollo
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T12:45:04Z
user: login: TomJo2000
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T12:45:50Z
user: login: TomJo2000
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T12:53:48Z
user: login: agnostic-apollo
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T13:02:44Z
user: login: TomJo2000
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T13:03:15Z
user: login: TomJo2000
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T13:06:08Z
user: login: agnostic-apollo
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T13:06:19Z
user: login: TomJo2000
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T13:06:47Z
user: login: agnostic-apollo
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T13:08:14Z
user: login: TomJo2000
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T13:08:28Z
user: login: agnostic-apollo
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T13:33:10Z
user: login: agnostic-apollo
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T13:33:47Z
user: login: agnostic-apollo
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T13:34:17Z
user: login: agnostic-apollo
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T13:37:56Z
user: login: TomJo2000
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T13:46:22Z
user: login: agnostic-apollo
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T13:55:38Z
user: login: TomJo2000
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T13:57:03Z
user: login: agnostic-apollo
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T14:30:03Z
user: login: agnostic-apollo
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T14:30:45Z
user: login: TomJo2000
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T14:30:56Z
user: login: TomJo2000
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-11T14:35:19Z

Review comments:
diff_hunk: @@ -339,24 +341,33 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
-	fi
+	}
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822') sed -ni -e "s|URIs:.*|URIs: $MAIN|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources";;
+			'legacy') echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list;;
+		esac
+
+		case "${has_repo_x11:-}" in
+			'deb822') sed -ni -e "s|URIs:.*|URIs: $X11|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/x11.sources";;
+			'legacy') echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list;;
+		esac
+
+		case "${has_repo_root:-}" in
+			'deb822') sed -ni -e "s|URIs:.*|URIs: $ROOT|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/root.sources";;
+			'legacy') echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list;;
+		esac
+	)
path: scripts/pkg.in
user: login: twaik
body: tur and glibc repos are not taken into account here.
created_at: 2025-04-26T09:59:07Z
author_association: MEMBER
diff_hunk: @@ -339,24 +341,33 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
-	fi
+	}
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822') sed -ni -e "s|URIs:.*|URIs: $MAIN|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources";;
+			'legacy') echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list;;
+		esac
+
+		case "${has_repo_x11:-}" in
+			'deb822') sed -ni -e "s|URIs:.*|URIs: $X11|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/x11.sources";;
+			'legacy') echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list;;
+		esac
+
+		case "${has_repo_root:-}" in
+			'deb822') sed -ni -e "s|URIs:.*|URIs: $ROOT|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/root.sources";;
+			'legacy') echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list;;
+		esac
+	)
path: scripts/pkg.in
user: login: robertkirkman
body: But, TUR and Glibc repos do not get affected by `termux-change-repo` or `pkg` generally, correct?

Their `sources.list.d` files  are specifically created and packaged elsewhere by licy183 and maxython respectively, for example,

https://github.com/termux/termux-packages/blob/8ef23ecd0969fd997ad8e067aad0067c66c449df/packages/tur-repo/build.sh#L11

So, to migrate them to deb822, `packages/tur-repo/build.sh` and `packages/glibc-repo/build.sh` would be edited separately, not `termux-tools/scripts/pkg.in`, right?
created_at: 2025-04-26T21:29:31Z
author_association: MEMBER
diff_hunk: @@ -339,24 +341,33 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
-	fi
+	}
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822') sed -ni -e "s|URIs:.*|URIs: $MAIN|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources";;
+			'legacy') echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list;;
+		esac
+
+		case "${has_repo_x11:-}" in
+			'deb822') sed -ni -e "s|URIs:.*|URIs: $X11|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/x11.sources";;
+			'legacy') echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list;;
+		esac
+
+		case "${has_repo_root:-}" in
+			'deb822') sed -ni -e "s|URIs:.*|URIs: $ROOT|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/root.sources";;
+			'legacy') echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list;;
+		esac
+	)
path: scripts/pkg.in
user: login: TomJo2000
body: - Exactly.
They're separate repos and are handled as part of termux/termux-packages#24212

If they do eventually gain more mirrors that support can be added to `pkg` and `termux-change-repo` at a later time.
created_at: 2025-04-26T21:32:23Z
author_association: MEMBER
diff_hunk: @@ -339,24 +341,33 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
-	fi
+	}
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822') sed -ni -e "s|URIs:.*|URIs: $MAIN|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources";;
path: scripts/pkg.in
user: login: Grimler91
body: Might be better to keep original *.sources in $PREFIX/etc/termux/ (or another suitable place), and then `sed -n -e "s|URIs:.*|URIs: $MAIN|" "$some_path" > "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources"`. That would make it possible to recover even if *.sources are emptied as in [my issue](https://github.com/termux/termux-tools/pull/165#issuecomment-2862035309)
created_at: 2025-05-08T07:22:16Z
author_association: MEMBER
diff_hunk: @@ -339,24 +341,33 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
-	fi
+	}
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
path: scripts/pkg.in
user: login: Grimler91
body: For legacy case this fails, has_repo main checks for $PREFIX/etc/apt/sources.list.d/main.list, but that file does not exist. We can fix it by using `*)` instead of `'legacy')`, or by adding a separate *) case. There should always be a main repo, probably, so maybe the check can be dropped (as before) 
created_at: 2025-05-08T16:33:30Z
author_association: MEMBER
diff_hunk: @@ -339,24 +341,33 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
-	fi
+	}
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822') sed -ni -e "s|URIs:.*|URIs: $MAIN|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources";;
path: scripts/pkg.in
user: login: Grimler91
body: Seems like it is the `-n` option that empties the .sources files:

```
$ cat main.sources 
# The main termux repository, with cloudflare cache
Types: deb
URIs: https://mirror.accum.se/mirror/termux.dev/termux-main
Suites: stable
Components: main
Signed-By: /data/data/com.termux/files/usr/etc/apt/trusted.gpg.d/grimler.gpg, /data/data/com.termux/files/usr/etc/apt/trusted.gpg.d/termux-autobuilds.gpg

# The main termux repository, without cloudflare cache
#Types: deb
#URIs: https://mirror.accum.se/mirror/termux.dev/termux-main
#Suites: stable
#Components: main
#Signed-By: /data/data/com.termux/files/usr/etc/apt/trusted.gpg.d/grimler.gpg, /data/data/com.termux/files/usr/etc/apt/trusted.gpg.d/termux-autobuilds.gpg
$ sed -ni -e "s|URIs:.*|URIs: https://mirror.accum.se/mirror/termux.dev/termux-main|" main.sources 
$ cat main.sources
[ empty ]
```
created_at: 2025-05-08T17:08:23Z
author_association: MEMBER
diff_hunk: @@ -339,24 +341,33 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
-	fi
+	}
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822') sed -ni -e "s|URIs:.*|URIs: $MAIN|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources";;
path: scripts/pkg.in
user: login: TomJo2000
body: Yep sorry that was leftover from testing, thought I had already removed those.
`-n` disables the implied `p` command causing the pattern space to be printed automatically.

<details><summary>I was using it during testing because I using <code>--debug</code> to step through the sed command.</summary>
<p> 

```console
# ~$ sed --debug -n -e "s/URIs:.*/URIs: testing/" $TERMUX__PREFIX/etc/apt/sources.list.d/main.sources
SED PROGRAM:
  s/URIs:.*/URIs: testing/
INPUT:   '/data/data/com.termux/files/usr/etc/apt/sources.list.d/main.sources' line 1
PATTERN: # The main termux repository, with cloudflare cache
COMMAND: s/URIs:.*/URIs: testing/
PATTERN: # The main termux repository, with cloudflare cache
END-OF-CYCLE:
INPUT:   '/data/data/com.termux/files/usr/etc/apt/sources.list.d/main.sources' line 2
PATTERN: Types: deb
COMMAND: s/URIs:.*/URIs: testing/
PATTERN: Types: deb
END-OF-CYCLE:
INPUT:   '/data/data/com.termux/files/usr/etc/apt/sources.list.d/main.sources' line 3
PATTERN: URIs: https://packages-cf.termux.dev/apt/termux-main/
COMMAND: s/URIs:.*/URIs: testing/
MATCHED REGEX REGISTERS
  regex[0] = 0-53 'URIs: https://packages-cf.termux.dev/apt/termux-main/'
PATTERN: URIs: testing
END-OF-CYCLE:
INPUT:   '/data/data/com.termux/files/usr/etc/apt/sources.list.d/main.sources' line 4
PATTERN: Suites: stable
COMMAND: s/URIs:.*/URIs: testing/
PATTERN: Suites: stable
END-OF-CYCLE:
INPUT:   '/data/data/com.termux/files/usr/etc/apt/sources.list.d/main.sources' line 5
PATTERN: Components: main
COMMAND: s/URIs:.*/URIs: testing/
PATTERN: Components: main
END-OF-CYCLE:
INPUT:   '/data/data/com.termux/files/usr/etc/apt/sources.list.d/main.sources' line 6
PATTERN: Signed-By: /data/data/com.termux/files/usr/etc/apt/trusted.gpg.d/grimler.gpg, /data/data/com.termux/files/usr/etc/apt/trusted.gpg.d/termux-autobuilds.gpg
COMMAND: s/URIs:.*/URIs: testing/
PATTERN: Signed-By: /data/data/com.termux/files/usr/etc/apt/trusted.gpg.d/grimler.gpg, /data/data/com.termux/files/usr/etc/apt/trusted.gpg.d/termux-autobuilds.gpg
END-OF-CYCLE:
INPUT:   '/data/data/com.termux/files/usr/etc/apt/sources.list.d/main.sources' line 7 PATTERN:
COMMAND: s/URIs:.*/URIs: testing/
PATTERN:
END-OF-CYCLE:
INPUT:   '/data/data/com.termux/files/usr/etc/apt/sources.list.d/main.sources' line 8
PATTERN: # The main termux repository, without cloudflare cache
COMMAND: s/URIs:.*/URIs: testing/
PATTERN: # The main termux repository, without cloudflare cache
END-OF-CYCLE:
INPUT:   '/data/data/com.termux/files/usr/etc/apt/sources.list.d/main.sources' line 9
PATTERN: #Types: deb
COMMAND: s/URIs:.*/URIs: testing/
PATTERN: #Types: deb
END-OF-CYCLE:
INPUT:   '/data/data/com.termux/files/usr/etc/apt/sources.list.d/main.sources' line 10
PATTERN: #URIs: https://packages-cf.termux.dev/apt/termux-main/
COMMAND: s/URIs:.*/URIs: testing/
MATCHED REGEX REGISTERS
  regex[0] = 1-54 'URIs: https://packages-cf.termux.dev/apt/termux-main/'
PATTERN: #URIs: testing
END-OF-CYCLE:
INPUT:   '/data/data/com.termux/files/usr/etc/apt/sources.list.d/main.sources' line 11
PATTERN: #Suites: stable
COMMAND: s/URIs:.*/URIs: testing/
PATTERN: #Suites: stable
END-OF-CYCLE:
INPUT:   '/data/data/com.termux/files/usr/etc/apt/sources.list.d/main.sources' line 12
PATTERN: #Components: main
COMMAND: s/URIs:.*/URIs: testing/
PATTERN: #Components: main
END-OF-CYCLE:
INPUT:   '/data/data/com.termux/files/usr/etc/apt/sources.list.d/main.sources' line 13
PATTERN: #Signed-By: /data/data/com.termux/files/usr/etc/apt/trusted.gpg.d/grimler.gpg, /data/data/com.termux/files/usr/etc/apt/trusted.gpg.d/termux-autobuilds.gpg
COMMAND: s/URIs:.*/URIs: testing/
PATTERN: #Signed-By: /data/data/com.termux/files/usr/etc/apt/trusted.gpg.d/grimler.gpg, /data/data/com.termux/files/usr/etc/apt/trusted.gpg.d/termux-autobuilds.gpg
END-OF-CYCLE:
```

</p>
</details>
created_at: 2025-05-08T23:41:02Z
author_association: MEMBER
diff_hunk: @@ -339,29 +345,44 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
path: scripts/pkg.in
user: login: agnostic-apollo
body: Use standard `if` like everywhere else.
created_at: 2025-05-11T07:32:43Z
author_association: MEMBER
diff_hunk: @@ -339,29 +345,44 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
-	fi
+	}
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $MAIN|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources";;
+			# There should always be a main repo, so fallback to
+			# creating sources.list if the `main.sources` file is missing
+			*) echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list;;
+		esac
+
+		case "${has_repo_x11:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $X11|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/x11.sources";;
+			'legacy') echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list;;
+		esac
+
+		case "${has_repo_root:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $ROOT|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/root.sources";;
+			'legacy') echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list;;
+		esac
+	)
path: scripts/pkg.in
user: login: agnostic-apollo
body: `sed` replacement string should ideally be escaped as urls could have special characters. Something like following.

```shell
sed_replacement_string() {

    local return_value

    local string="$1"

    string="$(printf "%s" "$string" |
        sed -r -e '$!{:a;N;$!ba;}; s/[&/\]/\\&/g; s/\n/\\&/g'; echo -n x)"
    string="${string%x}"
    return_value=$?
    if [ $return_value -ne 0 ]; then
        echo "Failed to run escape sed replacement string: $1" 1>&2
        return $return_value
    fi

    printf "%s" "$string"

}
```
created_at: 2025-05-11T07:37:15Z
author_association: MEMBER
diff_hunk: @@ -44,30 +44,22 @@ updates() {
 }
 
 repo_subscriptions_apt() {
-	local main_sources
-	main_sources=$(grep -E '^[[:space:]]*deb[[:space:]]' "@TERMUX_PREFIX@/etc/apt/sources.list")
-
-	if [ -n "$main_sources" ]; then
-		echo "# sources.list"
-		echo "$main_sources"
-	fi
-
-	if [ -d "@TERMUX_PREFIX@/etc/apt/sources.list.d" ]; then
-		local filename repo_package supl_sources
-		while read -r filename; do
-			repo_package=$(dpkg -S "$filename" 2>/dev/null | cut -d : -f 1)
-			supl_sources=$(grep -E '^[[:space:]]*deb[[:space:]]' "$filename")
-
-			if [ -n "$supl_sources" ]; then
-				if [ -n "$repo_package" ]; then
-					echo "# $repo_package (sources.list.d/$(basename "$filename"))"
-				else
-					echo "# sources.list.d/$(basename "$filename")"
-				fi
-				echo "$supl_sources"
-			fi
-		done < <(find "@TERMUX_PREFIX@/etc/apt/sources.list.d" -maxdepth 1 ! -type d)
-	fi
+	local apt_dir='@TERMUX_PREFIX@/etc/apt'
+
+	local filename source_entry repo_package sources_type
+	for filename in $apt_dir/sources.list{,.d/*}; do
path: scripts/termux-info.in
user: login: agnostic-apollo
body: Quote `$apt_dir`
created_at: 2025-05-11T07:40:20Z
author_association: MEMBER
diff_hunk: @@ -44,30 +44,22 @@ updates() {
 }
 
 repo_subscriptions_apt() {
-	local main_sources
-	main_sources=$(grep -E '^[[:space:]]*deb[[:space:]]' "@TERMUX_PREFIX@/etc/apt/sources.list")
-
-	if [ -n "$main_sources" ]; then
-		echo "# sources.list"
-		echo "$main_sources"
-	fi
-
-	if [ -d "@TERMUX_PREFIX@/etc/apt/sources.list.d" ]; then
-		local filename repo_package supl_sources
-		while read -r filename; do
-			repo_package=$(dpkg -S "$filename" 2>/dev/null | cut -d : -f 1)
-			supl_sources=$(grep -E '^[[:space:]]*deb[[:space:]]' "$filename")
-
-			if [ -n "$supl_sources" ]; then
-				if [ -n "$repo_package" ]; then
-					echo "# $repo_package (sources.list.d/$(basename "$filename"))"
-				else
-					echo "# sources.list.d/$(basename "$filename")"
-				fi
-				echo "$supl_sources"
-			fi
-		done < <(find "@TERMUX_PREFIX@/etc/apt/sources.list.d" -maxdepth 1 ! -type d)
-	fi
+	local apt_dir='@TERMUX_PREFIX@/etc/apt'
+
+	local filename source_entry repo_package sources_type
+	for filename in $apt_dir/sources.list{,.d/*}; do
+		[[ -f "$filename" ]] || continue
+		case "$filename" in
+			*.sources) sources_type="deb822";;
+			*.list)    sources_type="legacy";;
+			*) continue;; # Not a valid source type, skip
+		esac
+		source_entry="$(<"$filename")"
+		repo_package=$(dpkg -S "$filename" 2>/dev/null | cut -d : -f 1)
+
+		echo "# ${repo_package:+"$repo_package ("}${filename/${apt_dir}\/}${repo_package:+")"} [${sources_type}]"
path: scripts/termux-info.in
user: login: agnostic-apollo
body: This one liner is really hard to understand, should be split IMO.
created_at: 2025-05-11T07:42:10Z
author_association: MEMBER
diff_hunk: @@ -339,29 +345,44 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
-	fi
+	}
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $MAIN|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources";;
+			# There should always be a main repo, so fallback to
+			# creating sources.list if the `main.sources` file is missing
+			*) echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list;;
+		esac
+
+		case "${has_repo_x11:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $X11|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/x11.sources";;
+			'legacy') echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list;;
+		esac
+
+		case "${has_repo_root:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $ROOT|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/root.sources";;
+			'legacy') echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list;;
+		esac
+	)
path: scripts/pkg.in
user: login: agnostic-apollo
body: Can use `sed_escaped_replacement_string() { replace="$1"; replace="${replace//[\\]/\\\\}"; replace="${replace//[\/]/\\\/}"; replace="${replace//[&]/\\\&}"; echo "$replace"; }
` instead.
created_at: 2025-05-11T08:28:13Z
author_association: MEMBER
diff_hunk: @@ -44,30 +44,22 @@ updates() {
 }
 
 repo_subscriptions_apt() {
-	local main_sources
-	main_sources=$(grep -E '^[[:space:]]*deb[[:space:]]' "@TERMUX_PREFIX@/etc/apt/sources.list")
-
-	if [ -n "$main_sources" ]; then
-		echo "# sources.list"
-		echo "$main_sources"
-	fi
-
-	if [ -d "@TERMUX_PREFIX@/etc/apt/sources.list.d" ]; then
-		local filename repo_package supl_sources
-		while read -r filename; do
-			repo_package=$(dpkg -S "$filename" 2>/dev/null | cut -d : -f 1)
-			supl_sources=$(grep -E '^[[:space:]]*deb[[:space:]]' "$filename")
-
-			if [ -n "$supl_sources" ]; then
-				if [ -n "$repo_package" ]; then
-					echo "# $repo_package (sources.list.d/$(basename "$filename"))"
-				else
-					echo "# sources.list.d/$(basename "$filename")"
-				fi
-				echo "$supl_sources"
-			fi
-		done < <(find "@TERMUX_PREFIX@/etc/apt/sources.list.d" -maxdepth 1 ! -type d)
-	fi
+	local apt_dir='@TERMUX_PREFIX@/etc/apt'
+
+	local filename source_entry repo_package sources_type
+	for filename in $apt_dir/sources.list{,.d/*}; do
path: scripts/termux-info.in
user: login: TomJo2000
body: Yeah I'm not sure how I missed that one.
created_at: 2025-05-11T11:27:03Z
author_association: MEMBER
diff_hunk: @@ -44,30 +44,22 @@ updates() {
 }
 
 repo_subscriptions_apt() {
-	local main_sources
-	main_sources=$(grep -E '^[[:space:]]*deb[[:space:]]' "@TERMUX_PREFIX@/etc/apt/sources.list")
-
-	if [ -n "$main_sources" ]; then
-		echo "# sources.list"
-		echo "$main_sources"
-	fi
-
-	if [ -d "@TERMUX_PREFIX@/etc/apt/sources.list.d" ]; then
-		local filename repo_package supl_sources
-		while read -r filename; do
-			repo_package=$(dpkg -S "$filename" 2>/dev/null | cut -d : -f 1)
-			supl_sources=$(grep -E '^[[:space:]]*deb[[:space:]]' "$filename")
-
-			if [ -n "$supl_sources" ]; then
-				if [ -n "$repo_package" ]; then
-					echo "# $repo_package (sources.list.d/$(basename "$filename"))"
-				else
-					echo "# sources.list.d/$(basename "$filename")"
-				fi
-				echo "$supl_sources"
-			fi
-		done < <(find "@TERMUX_PREFIX@/etc/apt/sources.list.d" -maxdepth 1 ! -type d)
-	fi
+	local apt_dir='@TERMUX_PREFIX@/etc/apt'
+
+	local filename source_entry repo_package sources_type
+	for filename in $apt_dir/sources.list{,.d/*}; do
+		[[ -f "$filename" ]] || continue
+		case "$filename" in
+			*.sources) sources_type="deb822";;
+			*.list)    sources_type="legacy";;
+			*) continue;; # Not a valid source type, skip
+		esac
+		source_entry="$(<"$filename")"
+		repo_package=$(dpkg -S "$filename" 2>/dev/null | cut -d : -f 1)
+
+		echo "# ${repo_package:+"$repo_package ("}${filename/${apt_dir}\/}${repo_package:+")"} [${sources_type}]"
path: scripts/termux-info.in
user: login: TomJo2000
body: Fair enough, I'll see if I can make it more readable with a `printf`.
created_at: 2025-05-11T11:29:12Z
author_association: MEMBER
diff_hunk: @@ -44,30 +44,22 @@ updates() {
 }
 
 repo_subscriptions_apt() {
-	local main_sources
-	main_sources=$(grep -E '^[[:space:]]*deb[[:space:]]' "@TERMUX_PREFIX@/etc/apt/sources.list")
-
-	if [ -n "$main_sources" ]; then
-		echo "# sources.list"
-		echo "$main_sources"
-	fi
-
-	if [ -d "@TERMUX_PREFIX@/etc/apt/sources.list.d" ]; then
-		local filename repo_package supl_sources
-		while read -r filename; do
-			repo_package=$(dpkg -S "$filename" 2>/dev/null | cut -d : -f 1)
-			supl_sources=$(grep -E '^[[:space:]]*deb[[:space:]]' "$filename")
-
-			if [ -n "$supl_sources" ]; then
-				if [ -n "$repo_package" ]; then
-					echo "# $repo_package (sources.list.d/$(basename "$filename"))"
-				else
-					echo "# sources.list.d/$(basename "$filename")"
-				fi
-				echo "$supl_sources"
-			fi
-		done < <(find "@TERMUX_PREFIX@/etc/apt/sources.list.d" -maxdepth 1 ! -type d)
-	fi
+	local apt_dir='@TERMUX_PREFIX@/etc/apt'
+
+	local filename source_entry repo_package sources_type
+	for filename in $apt_dir/sources.list{,.d/*}; do
+		[[ -f "$filename" ]] || continue
+		case "$filename" in
+			*.sources) sources_type="deb822";;
+			*.list)    sources_type="legacy";;
+			*) continue;; # Not a valid source type, skip
+		esac
+		source_entry="$(<"$filename")"
+		repo_package=$(dpkg -S "$filename" 2>/dev/null | cut -d : -f 1)
+
+		echo "# ${repo_package:+"$repo_package ("}${filename/${apt_dir}\/}${repo_package:+")"} [${sources_type}]"
path: scripts/termux-info.in
user: login: agnostic-apollo
body: Yeah, good idea.
created_at: 2025-05-11T11:30:15Z
author_association: MEMBER
diff_hunk: @@ -339,29 +345,44 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
-	fi
+	}
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $MAIN|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources";;
+			# There should always be a main repo, so fallback to
+			# creating sources.list if the `main.sources` file is missing
+			*) echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list;;
+		esac
+
+		case "${has_repo_x11:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $X11|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/x11.sources";;
+			'legacy') echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list;;
+		esac
+
+		case "${has_repo_root:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $ROOT|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/root.sources";;
+			'legacy') echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list;;
+		esac
+	)
path: scripts/pkg.in
user: login: TomJo2000
body: > ```shell
> sed_replacement_string() {
> 
>     local return_value
> 
>     local string="$1"
> 
>     string="$(printf "%s" "$string" |
>         sed -r -e '$!{:a;N;$!ba;}; s/[&/\]/\\&/g; s/\n/\\&/g'; echo -n x)"
>     string="${string%x}"
>     return_value=$?
>     if [ $return_value -ne 0 ]; then
>         echo "Failed to run escape sed replacement string: $1" 1>&2
>         return $return_value
>     fi
> 
>     printf "%s" "$string"
> 
> }
> ```

How does this one work anyway?
The `string="${string%x}"` in particular doesn't make sense to me.
It kinda smells like the `test x$foo = xyes` checks from autotools configure scripts.
created_at: 2025-05-11T11:33:03Z
author_association: MEMBER
diff_hunk: @@ -339,29 +345,44 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
-	fi
+	}
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $MAIN|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources";;
+			# There should always be a main repo, so fallback to
+			# creating sources.list if the `main.sources` file is missing
+			*) echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list;;
+		esac
+
+		case "${has_repo_x11:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $X11|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/x11.sources";;
+			'legacy') echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list;;
+		esac
+
+		case "${has_repo_root:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $ROOT|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/root.sources";;
+			'legacy') echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list;;
+		esac
+	)
path: scripts/pkg.in
user: login: agnostic-apollo
body: It preserves newlines at end of string removed by the subshell by echoing `x` at end inside subshell and then removing it outside subshell. But no need to use `sed`, its from a posix implementation IIRC, use bash one I sent.


created_at: 2025-05-11T11:38:45Z
author_association: MEMBER
diff_hunk: @@ -339,29 +345,44 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
-	fi
+	}
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $MAIN|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources";;
+			# There should always be a main repo, so fallback to
+			# creating sources.list if the `main.sources` file is missing
+			*) echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list;;
+		esac
+
+		case "${has_repo_x11:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $X11|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/x11.sources";;
+			'legacy') echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list;;
+		esac
+
+		case "${has_repo_root:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $ROOT|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/root.sources";;
+			'legacy') echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list;;
+		esac
+	)
path: scripts/pkg.in
user: login: agnostic-apollo
body: The `$!{:a;N;$!ba;};` part reads the entire string as posix `sed` does not support `-z`/`--null-data`.
created_at: 2025-05-11T11:39:54Z
author_association: MEMBER
diff_hunk: @@ -339,29 +345,44 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
-	fi
+	}
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $MAIN|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources";;
+			# There should always be a main repo, so fallback to
+			# creating sources.list if the `main.sources` file is missing
+			*) echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list;;
+		esac
+
+		case "${has_repo_x11:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $X11|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/x11.sources";;
+			'legacy') echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list;;
+		esac
+
+		case "${has_repo_root:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $ROOT|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/root.sources";;
+			'legacy') echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list;;
+		esac
+	)
path: scripts/pkg.in
user: login: TomJo2000
body: Aren't both of these POSIX compliant?
created_at: 2025-05-11T11:39:59Z
author_association: MEMBER
diff_hunk: @@ -339,29 +345,44 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
-	fi
+	}
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $MAIN|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources";;
+			# There should always be a main repo, so fallback to
+			# creating sources.list if the `main.sources` file is missing
+			*) echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list;;
+		esac
+
+		case "${has_repo_x11:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $X11|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/x11.sources";;
+			'legacy') echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list;;
+		esac
+
+		case "${has_repo_root:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $ROOT|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/root.sources";;
+			'legacy') echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list;;
+		esac
+	)
path: scripts/pkg.in
user: login: agnostic-apollo
body: Posix does not support pattern replacement `/`, only `%`/`#` suffix/prefix removal.
created_at: 2025-05-11T11:50:07Z
author_association: MEMBER
diff_hunk: @@ -339,29 +345,44 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
path: scripts/pkg.in
user: login: TomJo2000
body: Will be fixed in the next push.
created_at: 2025-05-11T12:18:46Z
author_association: MEMBER
diff_hunk: @@ -339,29 +345,44 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
path: scripts/pkg.in
user: login: agnostic-apollo
body: Thanks.
created_at: 2025-05-11T12:20:28Z
author_association: MEMBER
diff_hunk: @@ -339,29 +345,44 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
-	fi
+	}
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $MAIN|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources";;
+			# There should always be a main repo, so fallback to
+			# creating sources.list if the `main.sources` file is missing
+			*) echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list;;
+		esac
+
+		case "${has_repo_x11:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $X11|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/x11.sources";;
+			'legacy') echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list;;
+		esac
+
+		case "${has_repo_root:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $ROOT|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/root.sources";;
+			'legacy') echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list;;
+		esac
+	)
path: scripts/pkg.in
user: login: TomJo2000
body: Right, so where do you actually want that `sed_escaped_replacement_string()` function defined?
The only places where it's applicable is in the `deb822)` case for the repos.
So should it be defined in the same subshell as them, or should it be a "global" function within `pkg`?
created_at: 2025-05-11T12:23:37Z
author_association: MEMBER
diff_hunk: @@ -339,29 +345,44 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
-	fi
+	}
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $MAIN|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources";;
+			# There should always be a main repo, so fallback to
+			# creating sources.list if the `main.sources` file is missing
+			*) echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list;;
+		esac
+
+		case "${has_repo_x11:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $X11|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/x11.sources";;
+			'legacy') echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list;;
+		esac
+
+		case "${has_repo_root:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $ROOT|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/root.sources";;
+			'legacy') echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list;;
+		esac
+	)
path: scripts/pkg.in
user: login: agnostic-apollo
body: Add a global function and then use a subshell in the sed replacement part, it shouldn't fail as only internal variable pattern replacement calls. A shorter function like `sed_escape_replacement` should be better.
created_at: 2025-05-11T12:27:18Z
author_association: MEMBER
diff_hunk: @@ -339,29 +345,44 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
-	fi
+	}
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $MAIN|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources";;
+			# There should always be a main repo, so fallback to
+			# creating sources.list if the `main.sources` file is missing
+			*) echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list;;
+		esac
+
+		case "${has_repo_x11:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $X11|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/x11.sources";;
+			'legacy') echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list;;
+		esac
+
+		case "${has_repo_root:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $ROOT|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/root.sources";;
+			'legacy') echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list;;
+		esac
+	)
path: scripts/pkg.in
user: login: agnostic-apollo
body: Can define it after `last_modified()`
created_at: 2025-05-11T12:28:59Z
author_association: MEMBER
diff_hunk: @@ -339,29 +345,44 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
-	fi
+	}
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $MAIN|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources";;
+			# There should always be a main repo, so fallback to
+			# creating sources.list if the `main.sources` file is missing
+			*) echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list;;
+		esac
+
+		case "${has_repo_x11:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $X11|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/x11.sources";;
+			'legacy') echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list;;
+		esac
+
+		case "${has_repo_root:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $ROOT|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/root.sources";;
+			'legacy') echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list;;
+		esac
+	)
path: scripts/pkg.in
user: login: TomJo2000
body: So it should be global, but the function itself should invoke a subshell?
created_at: 2025-05-11T12:29:03Z
author_association: MEMBER
diff_hunk: @@ -339,29 +345,44 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
-	fi
+	}
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $MAIN|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources";;
+			# There should always be a main repo, so fallback to
+			# creating sources.list if the `main.sources` file is missing
+			*) echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list;;
+		esac
+
+		case "${has_repo_x11:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $X11|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/x11.sources";;
+			'legacy') echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list;;
+		esac
+
+		case "${has_repo_root:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $ROOT|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/root.sources";;
+			'legacy') echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list;;
+		esac
+	)
path: scripts/pkg.in
user: login: TomJo2000
body: > Can use `sed_escaped_replacement_string() { replace="$1"; replace="${replace//[\\]/\\\\}"; replace="${replace//[\/]/\\\/}"; replace="${replace//[&]/\\\&}"; echo "$replace"; } ` instead.

Would this be an acceptable way to rephrase the function or do you prefer having a local temp variable?
```bash
sed_escape_replacement() {
	:    "${1//[\\]/\\\\}" # escape \
	:    "${_//[\/]/\\\/}" # escape /
	echo "${_//[&]/\\\&}"  # escape &
}
```
created_at: 2025-05-11T12:38:53Z
author_association: MEMBER
diff_hunk: @@ -339,29 +345,44 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
-	fi
+	}
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $MAIN|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources";;
+			# There should always be a main repo, so fallback to
+			# creating sources.list if the `main.sources` file is missing
+			*) echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list;;
+		esac
+
+		case "${has_repo_x11:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $X11|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/x11.sources";;
+			'legacy') echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list;;
+		esac
+
+		case "${has_repo_root:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $ROOT|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/root.sources";;
+			'legacy') echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list;;
+		esac
+	)
path: scripts/pkg.in
user: login: agnostic-apollo
body: This doesn't look readable to me at least. You can use:

```shell
sed_escape_replacement() {
	local r="$1";
	r="${r//[\\]/\\\\}" # Escape `\`.
	r="${r//[\/]/\\\/}" # Escape `/`.
	r="${r//[&]/\\\&}" # Escape `&`.
	echo "$r"
}
```
created_at: 2025-05-11T12:45:04Z
author_association: MEMBER
diff_hunk: @@ -339,29 +345,44 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
-	fi
+	}
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $MAIN|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources";;
+			# There should always be a main repo, so fallback to
+			# creating sources.list if the `main.sources` file is missing
+			*) echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list;;
+		esac
+
+		case "${has_repo_x11:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $X11|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/x11.sources";;
+			'legacy') echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list;;
+		esac
+
+		case "${has_repo_root:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $ROOT|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/root.sources";;
+			'legacy') echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list;;
+		esac
+	)
path: scripts/pkg.in
user: login: TomJo2000
body: So with a temp variable, got it.
created_at: 2025-05-11T12:45:49Z
author_association: MEMBER
diff_hunk: @@ -339,29 +345,44 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
-	fi
+	}
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $MAIN|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources";;
+			# There should always be a main repo, so fallback to
+			# creating sources.list if the `main.sources` file is missing
+			*) echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list;;
+		esac
+
+		case "${has_repo_x11:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $X11|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/x11.sources";;
+			'legacy') echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list;;
+		esac
+
+		case "${has_repo_root:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $ROOT|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/root.sources";;
+			'legacy') echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list;;
+		esac
+	)
path: scripts/pkg.in
user: login: TomJo2000
body: Just to double check, should this be called with the full substitution string, or only on the variable in there?
```bash
sed -i -e "$(sed_escape_replacement "s|URIs:.*|URIs: $MAIN|")" # ...
sed -i -e "s|URIs:.*|URIs: $(sed_escape_replacement "$MAIN")|" # ...
```
The second option would seem sufficient to me.
(and is subjectively more readable)
created_at: 2025-05-11T12:53:48Z
author_association: MEMBER
diff_hunk: @@ -339,29 +345,44 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
-	fi
+	}
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $MAIN|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources";;
+			# There should always be a main repo, so fallback to
+			# creating sources.list if the `main.sources` file is missing
+			*) echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list;;
+		esac
+
+		case "${has_repo_x11:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $X11|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/x11.sources";;
+			'legacy') echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list;;
+		esac
+
+		case "${has_repo_root:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $ROOT|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/root.sources";;
+			'legacy') echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list;;
+		esac
+	)
path: scripts/pkg.in
user: login: agnostic-apollo
body: No no, only the replacement string, preferably with the `URIs:` prefix. The match string has different logic for escaping, like it needs to escape `*`, `+`, `()`, etc in literal strings.

`"s|URIs:.*|$(sed_escape_replacement "URIs: $MAIN")|"`
created_at: 2025-05-11T13:02:44Z
author_association: MEMBER
diff_hunk: @@ -339,29 +345,44 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
-	fi
+	}
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $MAIN|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources";;
+			# There should always be a main repo, so fallback to
+			# creating sources.list if the `main.sources` file is missing
+			*) echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list;;
+		esac
+
+		case "${has_repo_x11:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $X11|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/x11.sources";;
+			'legacy') echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list;;
+		esac
+
+		case "${has_repo_root:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $ROOT|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/root.sources";;
+			'legacy') echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list;;
+		esac
+	)
path: scripts/pkg.in
user: login: TomJo2000
body: That works for me as well.
created_at: 2025-05-11T13:03:15Z
author_association: MEMBER
diff_hunk: @@ -44,30 +44,22 @@ updates() {
 }
 
 repo_subscriptions_apt() {
-	local main_sources
-	main_sources=$(grep -E '^[[:space:]]*deb[[:space:]]' "@TERMUX_PREFIX@/etc/apt/sources.list")
-
-	if [ -n "$main_sources" ]; then
-		echo "# sources.list"
-		echo "$main_sources"
-	fi
-
-	if [ -d "@TERMUX_PREFIX@/etc/apt/sources.list.d" ]; then
-		local filename repo_package supl_sources
-		while read -r filename; do
-			repo_package=$(dpkg -S "$filename" 2>/dev/null | cut -d : -f 1)
-			supl_sources=$(grep -E '^[[:space:]]*deb[[:space:]]' "$filename")
-
-			if [ -n "$supl_sources" ]; then
-				if [ -n "$repo_package" ]; then
-					echo "# $repo_package (sources.list.d/$(basename "$filename"))"
-				else
-					echo "# sources.list.d/$(basename "$filename")"
-				fi
-				echo "$supl_sources"
-			fi
-		done < <(find "@TERMUX_PREFIX@/etc/apt/sources.list.d" -maxdepth 1 ! -type d)
-	fi
+	local apt_dir='@TERMUX_PREFIX@/etc/apt'
+
+	local filename source_entry repo_package sources_type
+	for filename in $apt_dir/sources.list{,.d/*}; do
+		[[ -f "$filename" ]] || continue
+		case "$filename" in
+			*.sources) sources_type="deb822";;
+			*.list)    sources_type="legacy";;
+			*) continue;; # Not a valid source type, skip
+		esac
+		source_entry="$(<"$filename")"
+		repo_package=$(dpkg -S "$filename" 2>/dev/null | cut -d : -f 1)
+
+		echo "# ${repo_package:+"$repo_package ("}${filename/${apt_dir}\/}${repo_package:+")"} [${sources_type}]"
path: scripts/termux-info.in
user: login: TomJo2000
body: Not sure it helped that much, but it's something.
created_at: 2025-05-11T13:06:08Z
author_association: MEMBER
diff_hunk: @@ -339,29 +345,44 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
-	fi
+	}
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $MAIN|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources";;
+			# There should always be a main repo, so fallback to
+			# creating sources.list if the `main.sources` file is missing
+			*) echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list;;
+		esac
+
+		case "${has_repo_x11:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $X11|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/x11.sources";;
+			'legacy') echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list;;
+		esac
+
+		case "${has_repo_root:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $ROOT|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/root.sources";;
+			'legacy') echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list;;
+		esac
+	)
path: scripts/pkg.in
user: login: agnostic-apollo
body: That works for `sed` too actually ;)
created_at: 2025-05-11T13:06:19Z
author_association: MEMBER
diff_hunk: @@ -339,29 +345,44 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
-	fi
+	}
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $MAIN|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources";;
+			# There should always be a main repo, so fallback to
+			# creating sources.list if the `main.sources` file is missing
+			*) echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list;;
+		esac
+
+		case "${has_repo_x11:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $X11|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/x11.sources";;
+			'legacy') echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list;;
+		esac
+
+		case "${has_repo_root:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $ROOT|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/root.sources";;
+			'legacy') echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list;;
+		esac
+	)
path: scripts/pkg.in
user: login: TomJo2000
body: You know what I meant.
created_at: 2025-05-11T13:06:47Z
author_association: MEMBER
diff_hunk: @@ -339,29 +345,44 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	[[ -z "$selected_mirror" ]] && {
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
-	fi
+	}
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $MAIN|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources";;
+			# There should always be a main repo, so fallback to
+			# creating sources.list if the `main.sources` file is missing
+			*) echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list;;
+		esac
+
+		case "${has_repo_x11:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $X11|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/x11.sources";;
+			'legacy') echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list;;
+		esac
+
+		case "${has_repo_root:-}" in
+			'deb822') sed -i -e "s|URIs:.*|URIs: $ROOT|" "@TERMUX_PREFIX@/etc/apt/sources.list.d/root.sources";;
+			'legacy') echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list;;
+		esac
+	)
path: scripts/pkg.in
user: login: agnostic-apollo
body: Yeah. Will review in a bit.
created_at: 2025-05-11T13:08:14Z
author_association: MEMBER
diff_hunk: @@ -44,30 +44,26 @@ updates() {
 }
 
 repo_subscriptions_apt() {
-	local main_sources
-	main_sources=$(grep -E '^[[:space:]]*deb[[:space:]]' "@TERMUX_PREFIX@/etc/apt/sources.list")
-
-	if [ -n "$main_sources" ]; then
-		echo "# sources.list"
-		echo "$main_sources"
-	fi
-
-	if [ -d "@TERMUX_PREFIX@/etc/apt/sources.list.d" ]; then
-		local filename repo_package supl_sources
-		while read -r filename; do
-			repo_package=$(dpkg -S "$filename" 2>/dev/null | cut -d : -f 1)
-			supl_sources=$(grep -E '^[[:space:]]*deb[[:space:]]' "$filename")
-
-			if [ -n "$supl_sources" ]; then
-				if [ -n "$repo_package" ]; then
-					echo "# $repo_package (sources.list.d/$(basename "$filename"))"
-				else
-					echo "# sources.list.d/$(basename "$filename")"
-				fi
-				echo "$supl_sources"
-			fi
-		done < <(find "@TERMUX_PREFIX@/etc/apt/sources.list.d" -maxdepth 1 ! -type d)
-	fi
+	local apt_dir='@TERMUX_PREFIX@/etc/apt'
+
+	local filename source_entry repo_package sources_type
+	for filename in "${apt_dir}"/sources.list{,.d/*}; do
+		[[ -f "$filename" ]] || continue
+		case "$filename" in
+			*.sources) sources_type="deb822";;
+			*.list)    sources_type="legacy";;
+			*) continue;; # Not a valid source type, skip
+		esac
+		source_entry="$(<"$filename")"
+		repo_package=$(dpkg -S "$filename" 2>/dev/null | cut -d : -f 1)
+		is_from_package=""
path: scripts/termux-info.in
user: login: TomJo2000
body: Ah crap I got sidetracked and forgot to finish this.
I was gonna make this a variable to make it less messy in the printf, but I don't think that'll actually help.
created_at: 2025-05-11T13:08:28Z
author_association: MEMBER
diff_hunk: @@ -339,29 +353,53 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	if [ -z "$selected_mirror" ]; then
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
 	fi
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822')
+				sed -i -e "s|URIs:.*|$(sed_escape_replacement "URIs: $MAIN")|" \
+				"@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources"
+			;;
+			# There should always be a main repo, so fallback to
+			# creating sources.list if the `main.sources` file is missing
+			*) echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list;;
+		esac
+
+		case "${has_repo_x11:-}" in
+			'deb822')
+				sed -i -e "s|URIs:.*|$(sed_escape_replacement "URIs: $X11")|" \
+				"@TERMUX_PREFIX@/etc/apt/sources.list.d/x11.sources"
+			;;
+			'legacy') echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list;;
+		esac
+
+		case "${has_repo_root:-}" in
+			'deb822')
+				sed -i -e "s|URIs:.*|$(sed_escape_replacement "URIs: $ROOT")|" \
+				"@TERMUX_PREFIX@/etc/apt/sources.list.d/root.sources"
+			;;
+			'legacy') echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list;;
+		esac
+	)
 }
 
 update_apt_cache() {
 	local current_host
-	current_host=$(head -n 1 <(sed -nE -e 's|^\s*deb\s+https?://(.+)\s+stable\s+main$|\1|p' @TERMUX_PREFIX@/etc/apt/sources.list) || true)
+	if [ -f "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources" ]; then
+		current_host=$(head -n 1 <(sed -nE 's|^\s*URIs:\s+https?://(.+)$|\1|p' @TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources) || :)
+	elif [ -f "@TERMUX_PREFIX@/etc/apt/sources.list" ]; then
+		current_host=$(head -n 1 <(sed -nE 's|^\s*deb\s+https?://(.+)\s+stable\s+main$|\1|p' @TERMUX_PREFIX@/etc/apt/sources.list) || :)
+	fi
path: scripts/pkg.in
user: login: agnostic-apollo
body: Quote `@TERMUX_PREFIX@`
created_at: 2025-05-11T13:33:09Z
author_association: MEMBER
diff_hunk: @@ -178,7 +188,11 @@ get_mirror_weight() {
 
 select_mirror() {
 	local current_mirror
-	current_mirror=$(grep -oE 'https?://[^ ]+' <(grep -m 1 -E '^[[:space:]]*deb[[:space:]]+' @TERMUX_PREFIX@/etc/apt/sources.list) || true)
+	if [ -f "@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources" ]; then
+		current_mirror=$(grep -oE 'https?://[^ ]+' <(grep -m 1 -E '^[[:space:]]*URIs:[[:space:]]+' @TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources) || :)
+	elif [ -f "@TERMUX_PREFIX@/etc/apt/sources.list" ]; then
+		current_mirror=$(grep -oE 'https?://[^ ]+' <(grep -m 1 -E '^[[:space:]]*deb[[:space:]]+' @TERMUX_PREFIX@/etc/apt/sources.list) || :)
+	fi
path: scripts/pkg.in
user: login: agnostic-apollo
body: Quote `@TERMUX_PREFIX@` here too.
created_at: 2025-05-11T13:33:47Z
author_association: MEMBER
diff_hunk: @@ -44,30 +44,25 @@ updates() {
 }
 
 repo_subscriptions_apt() {
-	local main_sources
-	main_sources=$(grep -E '^[[:space:]]*deb[[:space:]]' "@TERMUX_PREFIX@/etc/apt/sources.list")
-
-	if [ -n "$main_sources" ]; then
-		echo "# sources.list"
-		echo "$main_sources"
-	fi
-
-	if [ -d "@TERMUX_PREFIX@/etc/apt/sources.list.d" ]; then
-		local filename repo_package supl_sources
-		while read -r filename; do
-			repo_package=$(dpkg -S "$filename" 2>/dev/null | cut -d : -f 1)
-			supl_sources=$(grep -E '^[[:space:]]*deb[[:space:]]' "$filename")
-
-			if [ -n "$supl_sources" ]; then
-				if [ -n "$repo_package" ]; then
-					echo "# $repo_package (sources.list.d/$(basename "$filename"))"
-				else
-					echo "# sources.list.d/$(basename "$filename")"
-				fi
-				echo "$supl_sources"
-			fi
-		done < <(find "@TERMUX_PREFIX@/etc/apt/sources.list.d" -maxdepth 1 ! -type d)
-	fi
+	local apt_dir='@TERMUX_PREFIX@/etc/apt'
path: scripts/termux-info.in
user: login: agnostic-apollo
body: Should preferably use double quotes like in other places.
created_at: 2025-05-11T13:34:17Z
author_association: MEMBER
diff_hunk: @@ -44,30 +44,25 @@ updates() {
 }
 
 repo_subscriptions_apt() {
-	local main_sources
-	main_sources=$(grep -E '^[[:space:]]*deb[[:space:]]' "@TERMUX_PREFIX@/etc/apt/sources.list")
-
-	if [ -n "$main_sources" ]; then
-		echo "# sources.list"
-		echo "$main_sources"
-	fi
-
-	if [ -d "@TERMUX_PREFIX@/etc/apt/sources.list.d" ]; then
-		local filename repo_package supl_sources
-		while read -r filename; do
-			repo_package=$(dpkg -S "$filename" 2>/dev/null | cut -d : -f 1)
-			supl_sources=$(grep -E '^[[:space:]]*deb[[:space:]]' "$filename")
-
-			if [ -n "$supl_sources" ]; then
-				if [ -n "$repo_package" ]; then
-					echo "# $repo_package (sources.list.d/$(basename "$filename"))"
-				else
-					echo "# sources.list.d/$(basename "$filename")"
-				fi
-				echo "$supl_sources"
-			fi
-		done < <(find "@TERMUX_PREFIX@/etc/apt/sources.list.d" -maxdepth 1 ! -type d)
-	fi
+	local apt_dir='@TERMUX_PREFIX@/etc/apt'
+
+	local filename source_entry repo_package sources_type
+	for filename in "${apt_dir}"/sources.list{,.d/*}; do
+		[[ -f "$filename" ]] || continue
+		case "$filename" in
+			*.sources) sources_type="deb822";;
+			*.list)    sources_type="legacy";;
+			*) continue;; # Not a valid source type, skip
+		esac
+		source_entry="$(<"$filename")"
+		repo_package=$(dpkg -S "$filename" 2>/dev/null | cut -d : -f 1)
+
+		printf "# %s${repo_package:+'('}%s${repo_package:+')'} [%s]\n" \
+			"${repo_package}" \
+			"${filename/${apt_dir}\/}" \
+			"${sources_type}"
path: scripts/termux-info.in
user: login: agnostic-apollo
body: I think just use an `[[ -n "$repo_package" ]] && printf_format="" ||  printf_format=""` beforehand. Add local too.
created_at: 2025-05-11T13:37:56Z
author_association: MEMBER
diff_hunk: @@ -44,30 +44,25 @@ updates() {
 }
 
 repo_subscriptions_apt() {
-	local main_sources
-	main_sources=$(grep -E '^[[:space:]]*deb[[:space:]]' "@TERMUX_PREFIX@/etc/apt/sources.list")
-
-	if [ -n "$main_sources" ]; then
-		echo "# sources.list"
-		echo "$main_sources"
-	fi
-
-	if [ -d "@TERMUX_PREFIX@/etc/apt/sources.list.d" ]; then
-		local filename repo_package supl_sources
-		while read -r filename; do
-			repo_package=$(dpkg -S "$filename" 2>/dev/null | cut -d : -f 1)
-			supl_sources=$(grep -E '^[[:space:]]*deb[[:space:]]' "$filename")
-
-			if [ -n "$supl_sources" ]; then
-				if [ -n "$repo_package" ]; then
-					echo "# $repo_package (sources.list.d/$(basename "$filename"))"
-				else
-					echo "# sources.list.d/$(basename "$filename")"
-				fi
-				echo "$supl_sources"
-			fi
-		done < <(find "@TERMUX_PREFIX@/etc/apt/sources.list.d" -maxdepth 1 ! -type d)
-	fi
+	local apt_dir='@TERMUX_PREFIX@/etc/apt'
+
+	local filename source_entry repo_package sources_type
+	for filename in "${apt_dir}"/sources.list{,.d/*}; do
+		[[ -f "$filename" ]] || continue
+		case "$filename" in
+			*.sources) sources_type="deb822";;
+			*.list)    sources_type="legacy";;
+			*) continue;; # Not a valid source type, skip
+		esac
+		source_entry="$(<"$filename")"
+		repo_package=$(dpkg -S "$filename" 2>/dev/null | cut -d : -f 1)
+
+		printf "# %s${repo_package:+'('}%s${repo_package:+')'} [%s]\n" \
+			"${repo_package}" \
+			"${filename/${apt_dir}\/}" \
+			"${sources_type}"
path: scripts/termux-info.in
user: login: TomJo2000
body: I've used a `if;then;else` instead for readability.
created_at: 2025-05-11T13:46:22Z
author_association: MEMBER
diff_hunk: @@ -44,30 +44,25 @@ updates() {
 }
 
 repo_subscriptions_apt() {
-	local main_sources
-	main_sources=$(grep -E '^[[:space:]]*deb[[:space:]]' "@TERMUX_PREFIX@/etc/apt/sources.list")
-
-	if [ -n "$main_sources" ]; then
-		echo "# sources.list"
-		echo "$main_sources"
-	fi
-
-	if [ -d "@TERMUX_PREFIX@/etc/apt/sources.list.d" ]; then
-		local filename repo_package supl_sources
-		while read -r filename; do
-			repo_package=$(dpkg -S "$filename" 2>/dev/null | cut -d : -f 1)
-			supl_sources=$(grep -E '^[[:space:]]*deb[[:space:]]' "$filename")
-
-			if [ -n "$supl_sources" ]; then
-				if [ -n "$repo_package" ]; then
-					echo "# $repo_package (sources.list.d/$(basename "$filename"))"
-				else
-					echo "# sources.list.d/$(basename "$filename")"
-				fi
-				echo "$supl_sources"
-			fi
-		done < <(find "@TERMUX_PREFIX@/etc/apt/sources.list.d" -maxdepth 1 ! -type d)
-	fi
+	local apt_dir='@TERMUX_PREFIX@/etc/apt'
+
+	local filename source_entry repo_package sources_type
+	for filename in "${apt_dir}"/sources.list{,.d/*}; do
+		[[ -f "$filename" ]] || continue
+		case "$filename" in
+			*.sources) sources_type="deb822";;
+			*.list)    sources_type="legacy";;
+			*) continue;; # Not a valid source type, skip
+		esac
+		source_entry="$(<"$filename")"
+		repo_package=$(dpkg -S "$filename" 2>/dev/null | cut -d : -f 1)
+
+		printf "# %s${repo_package:+'('}%s${repo_package:+')'} [%s]\n" \
+			"${repo_package}" \
+			"${filename/${apt_dir}\/}" \
+			"${sources_type}"
path: scripts/termux-info.in
user: login: agnostic-apollo
body: Then might as well wrap `printf` around `if`.
created_at: 2025-05-11T13:55:38Z
author_association: MEMBER
diff_hunk: @@ -44,30 +44,25 @@ updates() {
 }
 
 repo_subscriptions_apt() {
-	local main_sources
-	main_sources=$(grep -E '^[[:space:]]*deb[[:space:]]' "@TERMUX_PREFIX@/etc/apt/sources.list")
-
-	if [ -n "$main_sources" ]; then
-		echo "# sources.list"
-		echo "$main_sources"
-	fi
-
-	if [ -d "@TERMUX_PREFIX@/etc/apt/sources.list.d" ]; then
-		local filename repo_package supl_sources
-		while read -r filename; do
-			repo_package=$(dpkg -S "$filename" 2>/dev/null | cut -d : -f 1)
-			supl_sources=$(grep -E '^[[:space:]]*deb[[:space:]]' "$filename")
-
-			if [ -n "$supl_sources" ]; then
-				if [ -n "$repo_package" ]; then
-					echo "# $repo_package (sources.list.d/$(basename "$filename"))"
-				else
-					echo "# sources.list.d/$(basename "$filename")"
-				fi
-				echo "$supl_sources"
-			fi
-		done < <(find "@TERMUX_PREFIX@/etc/apt/sources.list.d" -maxdepth 1 ! -type d)
-	fi
+	local apt_dir='@TERMUX_PREFIX@/etc/apt'
+
+	local filename source_entry repo_package sources_type
+	for filename in "${apt_dir}"/sources.list{,.d/*}; do
+		[[ -f "$filename" ]] || continue
+		case "$filename" in
+			*.sources) sources_type="deb822";;
+			*.list)    sources_type="legacy";;
+			*) continue;; # Not a valid source type, skip
+		esac
+		source_entry="$(<"$filename")"
+		repo_package=$(dpkg -S "$filename" 2>/dev/null | cut -d : -f 1)
+
+		printf "# %s${repo_package:+'('}%s${repo_package:+')'} [%s]\n" \
+			"${repo_package}" \
+			"${filename/${apt_dir}\/}" \
+			"${sources_type}"
path: scripts/termux-info.in
user: login: TomJo2000
body: I'd rather not.
created_at: 2025-05-11T13:57:03Z
author_association: MEMBER
diff_hunk: @@ -339,29 +353,53 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	if [ -z "$selected_mirror" ]; then
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
 	fi
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822')
+				sed -i -e "s|URIs:.*|$(sed_escape_replacement "URIs: $MAIN")|" \
+				"@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources"
+			;;
+			# There should always be a main repo, so fallback to
+			# creating sources.list if the `main.sources` file is missing
+			*) echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list;;
+		esac
+
+		case "${has_repo_x11:-}" in
+			'deb822')
+				sed -i -e "s|URIs:.*|$(sed_escape_replacement "URIs: $X11")|" \
+				"@TERMUX_PREFIX@/etc/apt/sources.list.d/x11.sources"
+			;;
+			'legacy') echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list;;
+		esac
+
+		case "${has_repo_root:-}" in
+			'deb822')
+				sed -i -e "s|URIs:.*|$(sed_escape_replacement "URIs: $ROOT")|" \
+				"@TERMUX_PREFIX@/etc/apt/sources.list.d/root.sources"
+			;;
+			'legacy') echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list;;
path: scripts/pkg.in
user: login: agnostic-apollo
body: Quote.
created_at: 2025-05-11T14:30:03Z
author_association: MEMBER
diff_hunk: @@ -44,30 +44,25 @@ updates() {
 }
 
 repo_subscriptions_apt() {
-	local main_sources
-	main_sources=$(grep -E '^[[:space:]]*deb[[:space:]]' "@TERMUX_PREFIX@/etc/apt/sources.list")
-
-	if [ -n "$main_sources" ]; then
-		echo "# sources.list"
-		echo "$main_sources"
-	fi
-
-	if [ -d "@TERMUX_PREFIX@/etc/apt/sources.list.d" ]; then
-		local filename repo_package supl_sources
-		while read -r filename; do
-			repo_package=$(dpkg -S "$filename" 2>/dev/null | cut -d : -f 1)
-			supl_sources=$(grep -E '^[[:space:]]*deb[[:space:]]' "$filename")
-
-			if [ -n "$supl_sources" ]; then
-				if [ -n "$repo_package" ]; then
-					echo "# $repo_package (sources.list.d/$(basename "$filename"))"
-				else
-					echo "# sources.list.d/$(basename "$filename")"
-				fi
-				echo "$supl_sources"
-			fi
-		done < <(find "@TERMUX_PREFIX@/etc/apt/sources.list.d" -maxdepth 1 ! -type d)
-	fi
+	local apt_dir='@TERMUX_PREFIX@/etc/apt'
+
+	local filename source_entry repo_package sources_type
+	for filename in "${apt_dir}"/sources.list{,.d/*}; do
+		[[ -f "$filename" ]] || continue
+		case "$filename" in
+			*.sources) sources_type="deb822";;
+			*.list)    sources_type="legacy";;
+			*) continue;; # Not a valid source type, skip
+		esac
+		source_entry="$(<"$filename")"
+		repo_package=$(dpkg -S "$filename" 2>/dev/null | cut -d : -f 1)
+
+		printf "# %s${repo_package:+'('}%s${repo_package:+')'} [%s]\n" \
+			"${repo_package}" \
+			"${filename/${apt_dir}\/}" \
+			"${sources_type}"
path: scripts/termux-info.in
user: login: agnostic-apollo
body: No problem.
created_at: 2025-05-11T14:30:45Z
author_association: MEMBER
diff_hunk: @@ -339,29 +353,53 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	if [ -z "$selected_mirror" ]; then
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
 	fi
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822')
+				sed -i -e "s|URIs:.*|$(sed_escape_replacement "URIs: $MAIN")|" \
+				"@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources"
+			;;
+			# There should always be a main repo, so fallback to
+			# creating sources.list if the `main.sources` file is missing
+			*) echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list;;
+		esac
+
+		case "${has_repo_x11:-}" in
+			'deb822')
+				sed -i -e "s|URIs:.*|$(sed_escape_replacement "URIs: $X11")|" \
+				"@TERMUX_PREFIX@/etc/apt/sources.list.d/x11.sources"
+			;;
+			'legacy') echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list;;
+		esac
+
+		case "${has_repo_root:-}" in
+			'deb822')
+				sed -i -e "s|URIs:.*|$(sed_escape_replacement "URIs: $ROOT")|" \
+				"@TERMUX_PREFIX@/etc/apt/sources.list.d/root.sources"
+			;;
+			'legacy') echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list;;
path: scripts/pkg.in
user: login: TomJo2000
body: Derp, yeah missed that one.
created_at: 2025-05-11T14:30:56Z
author_association: MEMBER
diff_hunk: @@ -339,29 +353,53 @@ select_mirror() {
 		selected_mirror="${weighted_mirrors[${random_weight}]}"
 	fi
 
-	if [ -n "$selected_mirror" ]; then
-		(
-			unset_mirror_variables
-			source "$selected_mirror"
-			echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
-			if [[ "$has_repo_x11" == "true" ]]; then
-				echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list
-			fi
-			if [[ "$has_repo_root" == "true" ]]; then
-				echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list
-			fi
-		)
-	else
+	if [ -z "$selected_mirror" ]; then
 		# Should not happen unless there is some issue with
 		# the script, or the mirror files
 		echo "Error: None of the mirrors are accessible"
 		exit 1
 	fi
+
+	(
+		unset_mirror_variables
+		# shellcheck source=/dev/null
+		source "$selected_mirror"
+
+		case "$(has_repo main)" in
+			'deb822')
+				sed -i -e "s|URIs:.*|$(sed_escape_replacement "URIs: $MAIN")|" \
+				"@TERMUX_PREFIX@/etc/apt/sources.list.d/main.sources"
+			;;
+			# There should always be a main repo, so fallback to
+			# creating sources.list if the `main.sources` file is missing
+			*) echo "deb $MAIN stable main" > @TERMUX_PREFIX@/etc/apt/sources.list;;
+		esac
+
+		case "${has_repo_x11:-}" in
+			'deb822')
+				sed -i -e "s|URIs:.*|$(sed_escape_replacement "URIs: $X11")|" \
+				"@TERMUX_PREFIX@/etc/apt/sources.list.d/x11.sources"
+			;;
+			'legacy') echo "deb $X11 x11 main" > @TERMUX_PREFIX@/etc/apt/sources.list.d/x11.list;;
+		esac
+
+		case "${has_repo_root:-}" in
+			'deb822')
+				sed -i -e "s|URIs:.*|$(sed_escape_replacement "URIs: $ROOT")|" \
+				"@TERMUX_PREFIX@/etc/apt/sources.list.d/root.sources"
+			;;
+			'legacy') echo "deb $ROOT root stable" > @TERMUX_PREFIX@/etc/apt/sources.list.d/root.list;;
path: scripts/pkg.in
user: login: TomJo2000
body: Checked and quoted all remaining instances.
https://github.com/termux/termux-tools/compare/7db010555f20e228d8e3d3301c2f93f3070d4e77..aa8327da102febe132be2378a1797e7bb723d21a
created_at: 2025-05-11T14:35:19Z
author_association: MEMBER

PR comments:
user: login: robertkirkman
created_at: 2025-04-23T22:45:20Z
body: I tested this, and I think it is working for me. There is something strange-looking about the indentation in some places, but I think you might already have noticed, and will be able to resolve that.
author_association: MEMBER
user: login: TomJo2000
created_at: 2025-04-24T15:32:36Z
body: > There is something strange-looking about the indentation in some places, but I think you might already have noticed, and will be able to resolve that.

Haven't noticed anything of the sort.
There's some over-indenting from conditionals that could be turned into guard clauses, but the indentation as is seems correct to me.
If you have any specific examples please do point them out.
author_association: MEMBER
user: login: robertkirkman
created_at: 2025-04-24T18:12:40Z
body: Oh ok, sure, here is one of them

https://github.com/TomJo2000/termux-tools-prs/blob/924edb6859681eb1b57da7f15d2fab9307a58f18/scripts/pkg.in#L432-L434
author_association: MEMBER
user: login: TomJo2000
created_at: 2025-04-24T18:15:49Z
body: That part was already lined up with the line above, it just used spaces instead of tabs for indentation, which doesn't match the rest of the script.
author_association: MEMBER
user: login: robertkirkman
created_at: 2025-04-24T18:20:04Z
body: yes I understand that, but all I am trying to explain is that it is no longer "lined up" after this. This is how it looks on my screen.

![image](https://github.com/user-attachments/assets/97e9b691-3eca-4528-83fc-32e3f1f74ecb)

author_association: MEMBER
user: login: TomJo2000
created_at: 2025-04-24T18:22:16Z
body: Looks fine on my device.
![image](https://github.com/user-attachments/assets/3ebf9b82-17ba-4380-bd33-cb5334d2ac8d)
Probably an issue with your set tapstop?
author_association: MEMBER
user: login: TomJo2000
created_at: 2025-04-24T18:23:35Z
body: There's snapped it back to 3 tabs.
author_association: MEMBER
user: login: robertkirkman
created_at: 2025-04-24T18:25:13Z
body: The problem is also visible in your screenshot, but then you fixed it in a force push so I guess you are now able to see the problem with how it was before
author_association: MEMBER
user: login: TomJo2000
created_at: 2025-04-24T18:40:24Z
body: > The problem is also visible in your screenshot

Then I'm not seeing what you meant.
author_association: MEMBER
user: login: robertkirkman
created_at: 2025-04-24T18:42:37Z
body: 
![image](https://github.com/user-attachments/assets/73dca91b-10b4-4cd3-9f14-eda2b979a278)


I screenshotted your screenshot and drew a line on it, in my opinion the line should be straight BUT it is ok and does not matter very much because:

- it is purely whitespace-related and does not affect the script operation at all
- you already fixed it by force pushing a commit which makes the line straight
author_association: MEMBER
user: login: TomJo2000
created_at: 2025-04-24T18:44:22Z
body: It was aligned with `un*|rem*|rm|del*`, so I kept it aligned that way when I fixed the indentation from spaces to tabs.
But yeah it should just be straight.
author_association: MEMBER
user: login: robertkirkman
created_at: 2025-04-24T18:47:42Z
body: On my screen, the current change does not change the straight shape of the "line", but this discrepancy can come as a side effect of the different settings of tab width for each user, like you mentioned.

![image](https://github.com/user-attachments/assets/457e8d6f-c9e4-4449-87db-94881e1c4c25)


author_association: MEMBER
user: login: TomJo2000
created_at: 2025-04-27T16:27:17Z
body: Just finished testing the `termux-info` changes.
The new version does away with the previous special casing for the `sources.list` file outside the `sources.list.d` directory by consolidating the `find` into a `for` loop, glob and `case` for classification instead.
author_association: MEMBER
user: login: TomJo2000
created_at: 2025-05-04T21:35:52Z
body: - Seeing as termux/termux-packages#24212 is just about ready,
and as I've mentioned in that PR's reviews,
I think we should keep the Apt 3 transition focused and leave additional structural changes to related packages and tooling for later revisions.
author_association: MEMBER
user: login: Grimler91
created_at: 2025-05-08T07:18:59Z
body: For some reason that I have not figured out termux-change-repo/pkg empties my .sources files:

```
$ cat $PREFIX/etc/apt/sources.list.d/main.sources
# The main termux repository, with cloudflare cache
Types: deb
URIs: https://packages-cf.termux.dev/apt/termux-main/
Suites: stable
Components: main
Signed-By: /data/data/com.termux/files/usr/etc/apt/trusted.gpg.d/grimler.gpg, /data/data/com.termux/files/usr/etc/apt/trusted.gpg.d/termux-autobuilds.gpg

# The main termux repository, without cloudflare cache
#Types: deb
#URIs: https://packages-cf.termux.dev/apt/termux-main/
#Suites: stable
#Components: main
#Signed-By: /data/data/com.termux/files/usr/etc/apt/trusted.gpg.d/grimler.gpg, /data/data/com.termux/files/usr/etc/apt/trusted.gpg.d/termux-autobuilds.gpg
$ termux-change-repo
[ ... ]
[*] Mirror grimler.se selected
[*] pkg --check-mirror update
grep: /data/data/com.termux/files/usr/etc/apt/sources.list: No such file or directory
Testing the available mirrors:
[*] (4) https://grimler.se/termux/termux-main: ok
Picking mirror: (1) /data/data/com.termux/files/usr/etc/termux/mirrors/europe/grimler.se
Hit:1 https://turdl.kcubeterm.com tur-packages InRelease
All packages are up to date.
$ more $PREFIX/etc/apt/sources.list.d/* $PREFIX/etc/apt/sources.list
::::::::::::::
/data/data/com.termux/files/usr/etc/apt/sources.list.d/main.sources
::::::::::::::
::::::::::::::
/data/data/com.termux/files/usr/etc/apt/sources.list.d/root.sources
::::::::::::::
::::::::::::::
/data/data/com.termux/files/usr/etc/apt/sources.list.d/tur.sources
::::::::::::::
# A single and trusted place for all unofficial/less popular termux packages
Types: deb
URIs: https://tur.kcubeterm.com/
Suites: tur-packages
Components: tur tur-on-device tur-continuous
Signed-By: /data/data/com.termux/files/usr/etc/apt/trusted.gpg.d/tur.gpg
::::::::::::::
/data/data/com.termux/files/usr/etc/apt/sources.list.d/x11.sources
::::::::::::::
```
author_association: MEMBER
user: login: Grimler91
created_at: 2025-05-08T17:33:04Z
body: I opened a PR: github.com/tomjo2000/termux-tools-prs/pull/2 with fixes for my comments above, and also fixed so it can find current_mirror in deb822 format as well
author_association: MEMBER
user: login: TomJo2000
created_at: 2025-05-09T15:50:13Z
body: > I opened a PR: github.com/[TomJo2000/pull/2](https://github.com/TomJo2000/termux-tools-prs/pull/2) with fixes for my comments above, and also fixed so it can find current_mirror in deb822 format as well

Those changes have been manually merged into [`4ba21f3`](https://github.com/termux/termux-tools/pull/165/commits/4ba21f3fab01e22cc6443e3daa6a2352c4ac3504).
author_association: MEMBER
user: login: Grimler91
created_at: 2025-05-10T09:53:28Z
body: > Those changes have been manually merged into [4ba21f3](https://github.com/termux/termux-tools/pull/165/commits/4ba21f3fab01e22cc6443e3daa6a2352c4ac3504).

Thanks! Here's another PR which checks if both main.sources and sources.list exists, and deletes sources.list if main.sources seems to be a valid mirror: https://github.com/TomJo2000/termux-tools-prs/pull/3
If we get something like that in then I am happy and will gladly merge this!
author_association: MEMBER
user: login: TomJo2000
created_at: 2025-05-10T10:24:32Z
body: > Thanks! Here's another PR which checks if both main.sources and sources.list exists, and deletes sources.list if main.sources seems to be a valid mirror: [TomJo2000#3](https://github.com/TomJo2000/termux-tools-prs/pull/3) If we get something like that in then I am happy and will gladly merge this!

The way I was gonna handle that is for each `*-repo` package (and `apt`) to move their old `.list` entry to a `.list.dpkg-old` file instead.
That way each repo only modifies its own sources.
author_association: MEMBER
user: login: Grimler91
created_at: 2025-05-10T12:00:44Z
body: @TomJo2000 right, that does sound better!
author_association: MEMBER
user: login: TomJo2000
created_at: 2025-05-10T15:37:59Z
body: > @TomJo2000 right, that does sound better!

- I have now pushed those changes to the postinst scripts to termux/termux-packages#24212
The general structure for the message is:
```bash
[ -f "$TERMUX_PREFIX/etc/apt/sources.list.d/{{repo}}.list" ] && {
	echo "Legacy repo file found for '{{repo}}' repository."
	echo "Migrating sources.list.d/{{repo}}.list to sources.list.d/{{repo}}.sources"
	mv -v "$TERMUX_PREFIX"/etc/apt/sources.list.d/{{repo}}.list{,.dpkg-old}
	echo "A backup copy of the old {{repo}}.list repo file"
	echo "has been saved to $TERMUX_PREFIX/etc/apt/sources.list.d/{{repo}}.list.dpkg-old"
}

```
(`apt`'s does of course check for `$TERMUX_PREFIX/etc/apt/sources.list` instead)
It's currently duplicated for each repo, I don't think it's worth the additional effort to make it DRY.
author_association: MEMBER
user: login: TomJo2000
created_at: 2025-05-10T21:37:08Z
body: > This PR looks good to me, I will aim at merging and releasing it tomorrow evening unless there are more questions/comments by then!

We can merge this PR ahead of merging Apt 3.0.1,
I'd like to run final validation and testing tonight so we're ready to merge everything tomorrow.
author_association: MEMBER
user: login: agnostic-apollo
created_at: 2025-05-11T07:42:34Z
body: Looks good otherwise.
author_association: MEMBER
user: login: Grimler91
created_at: 2025-05-11T19:25:56Z
body: Needs one more update in update_apt_cache, where we run: `sources_modified=$(last_modified "@TERMUX_PREFIX@/etc/apt/sources.list")`, I will fix that and get this in
author_association: MEMBER
user: login: Grimler91
created_at: 2025-05-11T19:41:46Z
body: Thanks!
author_association: MEMBER