| 1 | #!/usr/bin/env bash |
| 2 | set -Eeuo pipefail |
| 3 | |
| 4 | thisDir="$(dirname "$(readlink -vf "$BASH_SOURCE")")" |
| 5 | source "$thisDir/.constants.sh" \ |
| 6 | --flags 'eol,ports,snapshot' \ |
| 7 | --flags 'deb-src' \ |
| 8 | --flags 'deb822,no-deb822' \ |
| 9 | -- \ |
| 10 | '[--deb-src] [--deb822/--no-deb822] [--eol] [--ports] [--snapshot] <target-dir> <suite>' \ |
| 11 | '--snapshot rootfs stretch |
| 12 | --eol rootfs wheezy' |
| 13 | |
| 14 | eval "$dgetopt" |
| 15 | eol= |
| 16 | ports= |
| 17 | snapshot= |
| 18 | debSrc= |
| 19 | deb822='auto' |
| 20 | while true; do |
| 21 | flag="$1"; shift |
| 22 | dgetopt-case "$flag" |
| 23 | case "$flag" in |
| 24 | --eol) eol=1 ;; |
| 25 | --ports) ports=1 ;; |
| 26 | --snapshot) snapshot=1 ;; |
| 27 | --deb-src) debSrc=1 ;; |
| 28 | --deb822) deb822=1 ;; |
| 29 | --no-deb822) deb822= ;; |
| 30 | --) break ;; |
| 31 | *) eusage "unknown flag '$flag'" ;; |
| 32 | esac |
| 33 | done |
| 34 | |
| 35 | targetDir="${1:-}"; shift || eusage 'missing target-dir' |
| 36 | suite="${1:-}"; shift || eusage 'missing suite' |
| 37 | [ -n "$targetDir" ] |
| 38 | |
| 39 | epoch="$(< "$targetDir/debuerreotype-epoch")" |
| 40 | |
| 41 | comp='main' |
| 42 | arch="$("$thisDir/.dpkg-arch.sh" "$targetDir")" |
| 43 | |
| 44 | if [ "$deb822" = 'auto' ]; then |
| 45 | aptVersion="$("$thisDir/.apt-version.sh" "$targetDir")" |
| 46 | if dpkg --compare-versions "$aptVersion" '>=' '2.3~'; then # this is a minimally-supported version for the files we generate (could go lower, but this is a safe choice) - this exists to support "debuerreotype-init" needing to generate (after "debootstrap" but before the script finishes), and is then overridden by "examples/debian.sh" later with more specific opinions on which releases should be deb822 |
| 47 | deb822=1 |
| 48 | else |
| 49 | deb822= |
| 50 | fi |
| 51 | fi |
| 52 | |
| 53 | if [ -n "$deb822" ]; then |
| 54 | if [ -n "$ports" ]; then |
| 55 | keyring='debian-ports-archive-keyring' |
| 56 | else |
| 57 | keyring='debian-archive-keyring' |
| 58 | fi |
| 59 | keyring="/usr/share/keyrings/$keyring" |
| 60 | # https://salsa.debian.org/release-team/debian-archive-keyring/-/commit/17c653ad964a3e81519f83e1d3a0704be737e4f6 |
| 61 | if [ -s "$targetDir$keyring.pgp" ]; then |
| 62 | keyring+='.pgp' |
| 63 | else |
| 64 | keyring+='.gpg' |
| 65 | fi |
| 66 | if [ ! -s "$targetDir$keyring" ]; then |
| 67 | echo >&2 "warning: it appears that '$targetDir' is missing '$keyring' (the expected keyring for APT); skipping 'Signed-By:' in generated sources" |
| 68 | keyring= |
| 69 | fi |
| 70 | fi |
| 71 | |
| 72 | deb() { |
| 73 | local mirrorArgs=() |
| 74 | if [ -n "$ports" ]; then |
| 75 | mirrorArgs+=( --ports ) |
| 76 | fi |
| 77 | if [ -n "$eol" ]; then |
| 78 | mirrorArgs+=( --eol ) |
| 79 | fi |
| 80 | mirrorArgs+=( "@$epoch" ) |
| 81 | |
| 82 | local suite deb822Mirrors=() |
| 83 | local -A deb822Suites=() deb822Snapshots=() |
| 84 | for suite; do |
| 85 | local mirrors |
| 86 | if ! mirrors="$("$thisDir/.debian-mirror.sh" "${mirrorArgs[@]}" "$suite" "$arch" "$comp")"; then |
| 87 | echo >&2 "skipping '$suite/$comp' ..." |
| 88 | continue |
| 89 | fi |
| 90 | local mirror snapshotMirror foundSuite |
| 91 | eval "$mirrors" |
| 92 | [ -n "$mirror" ] |
| 93 | [ -n "$snapshotMirror" ] |
| 94 | [ -n "$foundSuite" ] |
| 95 | suite="$foundSuite" |
| 96 | |
| 97 | if [ -n "$snapshot" ]; then |
| 98 | mirror="$snapshotMirror" |
| 99 | fi |
| 100 | |
| 101 | if [ -n "$deb822" ]; then |
| 102 | if [ -z "${deb822Suites["$mirror"]:-}" ]; then |
| 103 | # haven't seen this mirror yet! |
| 104 | deb822Mirrors+=( "$mirror" ) |
| 105 | deb822Snapshots["$mirror"]="$snapshotMirror" |
| 106 | fi |
| 107 | deb822Suites["$mirror"]+="${deb822Suites["$mirror"]:+ }$suite" |
| 108 | else |
| 109 | if [ -z "$snapshot" ]; then |
| 110 | echo "# deb $snapshotMirror $suite $comp" |
| 111 | fi |
| 112 | echo "deb $mirror $suite $comp" |
| 113 | if [ -n "$debSrc" ]; then |
| 114 | echo "deb-src $mirror $suite $comp" |
| 115 | fi |
| 116 | fi |
| 117 | done |
| 118 | |
| 119 | if [ -n "$deb822" ]; then |
| 120 | local first=1 |
| 121 | local mirror |
| 122 | for mirror in "${deb822Mirrors[@]}"; do |
| 123 | if [ -n "$first" ]; then |
| 124 | first= |
| 125 | else |
| 126 | echo |
| 127 | fi |
| 128 | if [ -n "$debSrc" ]; then |
| 129 | echo 'Types: deb deb-src' |
| 130 | else |
| 131 | echo 'Types: deb' |
| 132 | fi |
| 133 | if [ -z "$snapshot" ]; then |
| 134 | echo "# ${deb822Snapshots["$mirror"]}" |
| 135 | fi |
| 136 | echo "URIs: $mirror" |
| 137 | echo "Suites: ${deb822Suites["$mirror"]}" |
| 138 | echo "Components: $comp" |
| 139 | if [ -n "$keyring" ]; then |
| 140 | echo "Signed-By: $keyring" |
| 141 | fi |
| 142 | done |
| 143 | fi |
| 144 | } |
| 145 | |
| 146 | targetFileLine='/etc/apt/sources.list' |
| 147 | targetFile822='/etc/apt/sources.list.d/debian.sources' |
| 148 | if [ -n "$deb822" ]; then |
| 149 | targetFile="$targetFile822" |
| 150 | rm -f "$targetDir$targetFileLine" |
| 151 | else |
| 152 | targetFile="$targetFileLine" |
| 153 | rm -f "$targetDir$targetFile822" |
| 154 | fi |
| 155 | unset targetFileLine targetFile822 |
| 156 | |
| 157 | # https://github.com/tianon/go-aptsources/blob/e066ed9cd8cd9eef7198765bd00ec99679e6d0be/target.go#L16-L58 |
| 158 | { |
| 159 | suites=( "$suite" ) |
| 160 | case "$suite" in |
| 161 | sid | unstable) |
| 162 | if [ -n "$ports" ]; then |
| 163 | # https://www.ports.debian.org/archive |
| 164 | suites+=( 'unreleased' ) |
| 165 | fi |
| 166 | ;; |
| 167 | |
| 168 | *) |
| 169 | # https://salsa.debian.org/apt-team/apt/-/blob/23fe896858dfc7857f2d59c5fd7627332f11c1ff/vendor/debian/apt-vendor.ent#L9-20 |
| 170 | # https://salsa.debian.org/installer-team/apt-setup/tree/d7a642fb5fc76e4f0b684db53984bdb9123f8360/generators |
| 171 | # https://github.com/debuerreotype/debuerreotype/pull/128#issuecomment-1164046315 |
| 172 | # https://wiki.debian.org/SourcesList#Example_sources.list |
| 173 | suites+=( |
| 174 | "$suite-security" |
| 175 | "$suite-updates" |
| 176 | ) |
| 177 | if [ "$suite" = 'squeeze' ]; then |
| 178 | # https://wiki.debian.org/DebianSqueeze#FAQ |
| 179 | suites+=( "$suite-lts" ) |
| 180 | fi |
| 181 | ;; |
| 182 | esac |
| 183 | deb "${suites[@]}" |
| 184 | } > "$targetDir$targetFile" |
| 185 | chmod 0644 "$targetDir$targetFile" |
| 186 | |
| 187 | if [ ! -s "$targetDir$targetFile" ]; then |
| 188 | echo >&2 "error: '$targetFile' ended up empty -- something is definitely wrong" |
| 189 | exit 1 |
| 190 | fi |