aboutsummaryrefslogtreecommitdiff
path: root/pbuilder-apt-config
blob: 74b5a55d7122a48bb87839ab0532000ee3cb65c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/bin/sh
# Outputs APT configuration data

set -e

self="$(basename "$0")"

# TODO
# - support setting --mirror-map (for --profile)
# - Debian security entries?

log() {
    echo "$@" >&2
}

usage() {
    log "$self [<options>] [--archive=<archive>] [--mirror=<mirror>] [--suite=<suite>] [--pockets=<pocket1>,<pocket2>] <action>"
    log "$self [--profile=<profile>] <action>"
    log "  where <action> is one of apt-sources, debootstrap-mirror, or debootstrap-suite"
    log
    log "Common options:"
    log "  --with-sources, --with-sources=[yes|no|disabled]"
    log "  --arch=<arch>"
    log "  --components=<comp1>,<comp2>"
}

die() {
    log "$@"
    exit 1
}

guess_dist_arch_archive() {
    local dist="$1"
    local arch="$2"

    case "$dist" in
      etch|lenny|squeeze|sid|oldstable|stable|testing|unstable|experimental)
        echo "debian"
      ;;
      buzz|rex|bo|hamm|slink|potato|woody|sarge|etch|lenny|squeeze)
        echo "debian-archive"
      ;;
      hardy|intrepid|jaunty|karmic|lucid)
        case "$arch" in
          amd64|i386)
            echo "ubuntu"
          ;;
          armel|hppa|ia64|lpia|powerpc|sparc)
            echo "ubuntu-ports"
          ;;
          *)
            die "Unknown Ubuntu archive for arch=$arch"
          ;;
        esac
      ;;
      *)
        die "Unknown archive for dist=$dist"
      ;;
    esac
}

get_archive_url() {
    local archive="$1"
    local people_ppa

    case "$archive" in
      backports.org)
        echo "http://www.backports.org/backports.org/"
      ;;
      debian)
        echo "http://ftp.us.debian.org/debian/"
      ;;
      debian-archive)
        echo "http://archive.debian.org/debian/"
      ;;
      ppa:*)
        people_ppa="${archive#ppa:}"
        case "$people_ppa" in
          */*)
            :
          ;;
          *)
            people_ppa="$people_ppa/ppa"
        esac
        echo "http://ppa.launchpad.net/$people_ppa/ubuntu/"
      ;;
      ubuntu)
        echo "http://archive.ubuntu.com/ubuntu/"
      ;;
      ubuntu-ports)
        echo "http://ports.ubuntu.com/ubuntu-ports/"
      ;;
      debian-volatile)
        echo "http://volatile.debian.org/debian-volatile/"
      ;;
      *)
        die "Unknown mirror for archive=$archive"
      ;;
    esac
}

output_sources() {
    local with_sources="$1"
    local mirror="$2"
    local dist="$3"
    local components="$4"

    case "$with_sources" in
      yes)
        echo "deb     $mirror $dist $components"
        echo "deb-src $mirror $dist $components"
      ;;
      disabled)
        echo "deb     $mirror $dist $components"
        echo "#deb-src $mirror $dist $components"
      ;;
      no)
        echo "deb $mirror $dist $components"
      ;;
      *)
        die 'with_sources must be either "yes", "disabled", or "no"'
      ;;
    esac
}

sources=""
debootstrap_suite=""
debootstrap_mirror=""

add_output_sources() {
    local output="$(output_sources "$@")"

    sources="$sources${sources:+
}$output"
}

set_debootstrap_suite() {
    local archive="$1"
    local suite="$2"

    case "$archive" in
      debian)
        case "$suite" in
          unstable)
            debootstrap_suite="sid"
          ;;
          testing)
            debootstrap_suite="squeeze"
          ;;
          stable)
            debootstrap_suite="lenny"
          ;;
          oldstable)
            debootstrap_suite="etch"
          ;;
          *)
            debootstrap_suite="$suite"
          ;;
        esac
      ;;
      *)
        debootstrap_suite="$suite"
      ;;
    esac
}

getopt_output="`getopt -o "" -l help,with-sources::,arch:,components:,archive:,mirror:,suite:,pockets:,profile: -n "$self" -s sh -- "$@"`"

eval set -- "$getopt_output"

with_sources="disabled"
arch="`dpkg --print-architecture`"
components="main"
archive=""
mirror=""
suite=""
pockets=""
archive=""
profile=""

while :; do
    case "$1" in
      --help)
        usage
        exit 0
      ;;
      --with-sources)
        case "$2" in
          "")
            with_sources="yes"
          ;;
          yes|no|disabled)
            with_sources="$2"
          ;;
          *)
            die '--with-sources must be either "yes", "disabled", or "no"'
          ;;
        esac
        shift 2
      ;;
      --arch)
        arch="$2"
        shift 2
      ;;
      --components)
        components="$2"
        shift 2
      ;;
      --archive)
        archive="$2"
        shift 2
      ;;
      --mirror)
        mirror="$2"
        shift 2
      ;;
      --suite)
        if [ -z "$2" ]; then
            die "Need a suite for --suite"
        fi
        if [ -n "$profile" ]; then
            die "Can't set --suite after --profile"
        fi
        suite="$2"
        shift 2
      ;;
      --pockets)
        pockets="$2"
        shift 2
      ;;
      --profile)
        if [ -z "$2" ]; then
            die "Need a profile for --profile"
        fi
        if [ -n "$suite" ]; then
            die "Can't set --profile after --suite"
        fi
        profile="$2"
        shift 2
      ;;
      --)
        shift
        break
      ;;
      *)
        die "Unhandled arg=$1"
      ;;
    esac
done

if [ -z "$suite" ] && [ -z "$profile" ]; then
    die "Please pass --suite or --profile"
fi

components="$(echo "$components" | tr , " ")"
pockets="$(echo "$pockets" | tr , " ")"

if [ -n "$profile" ]; then
    base_dist="${profile%%/*}"
    base_dist="${base_dist%%-*}"
    base_archive="`guess_dist_arch_archive "$base_dist" "$arch"`"
    base_mirror="`get_archive_url "$base_archive"`"
    case "$base_archive" in
      debian|debian-archive)
        case "$profile" in
          experimental)
            base_dist="unstable"
            add_output_sources "$with_sources" "$base_mirror" "$base_dist" "$components"
            add_output_sources "$with_sources" "$base_mirror" "$profile" "$components"
          ;;
          *-proposed-updates/volatile)
            archive="debian-volatile"
            mirror="`get_archive_url "$archive"`"
            volatile_dist="$base_dist/volatile"
            add_output_sources "$with_sources" "$base_mirror" "$base_dist" "$components"
            add_output_sources "$with_sources" "$mirror" "$volatile_dist" "$components"
            add_output_sources "$with_sources" "$mirror" "$profile" "$components"
          ;;
          */volatile|*/volatile-sloppy)
            if [ "$base_dist" != "${profile%%/*}" ]; then
                die "Unknown Debian Volatile based profile=$profile"
            fi
            archive="debian-volatile"
            mirror="`get_archive_url "$archive"`"
            volatile_dist=${profile%-sloppy}
            add_output_sources "$with_sources" "$base_mirror" "$base_dist" "$components"
            add_output_sources "$with_sources" "$mirror" "$volatile_dist" "$components"
            if [ "$volatile_dist" != "$profile" ]; then
                add_output_sources "$with_sources" "$mirror" "$profile" "$components"
            fi
          ;;
          *-backports)
            if [ "$base_dist" != "${profile%%-*}" ]; then
                die "Unknown Backports.org based profile=$profile"
            fi
            archive="backports.org"
            mirror="`get_archive_url "$archive"`"
            add_output_sources "$with_sources" "$base_mirror" "$base_dist" "$components"
            add_output_sources "$with_sources" "$mirror" "$profile" "$components"
          ;;
          *-proposed-updates)
            if [ "$base_dist" != "${profile%%-*}" ]; then
                die "Unknown Debian updates based profile=$profile"
            fi
            add_output_sources "$with_sources" "$base_mirror" "$base_dist" "$components"
            add_output_sources "$with_sources" "$base_mirror" "$profile" "$components"
          ;;
          *)
            if [ "$base_dist" != "$profile" ]; then
                die "Unknown Debian based profile=$profile"
            fi
            add_output_sources "$with_sources" "$base_mirror" "$base_dist" "$components"
          ;;
        esac
      ;;
      ubuntu|ubuntu-ports)
        if [ "$base_dist" != "${profile%%-*}" ]; then
            die "Unknown Ubuntu based profile=$profile"
        fi
        pockets=""
        case "$profile" in
          *-security)
            pockets="security"
          ;;
          *-updates)
            pockets="security updates"
          ;;
          *-backports)
            pockets="security updates backports"
          ;;
          *-proposed)
            pockets="security updates proposed"
          ;;
          *)
            if [ "$base_dist" != "$profile" ]; then
                die "Unknown Ubuntu pocket based profile=$profile"
            fi
          ;;
        esac
        add_output_sources "$with_sources" "$base_mirror" "$base_dist" "$components"
        for pocket in $pockets; do
            add_output_sources "$with_sources" "$base_mirror" "$base_dist-$pocket" "$components"
        done
      ;;
      *)
        die "Unknown profile for base_archive=$base_archive"
      ;;
    esac
    set_debootstrap_suite "$base_archive" "$base_dist"
    debootstrap_mirror="$base_mirror"
fi

if [ -n "$suite" ]; then
    if [ -z "$mirror" ]; then
        if [ -z "$archive" ]; then
            archive="`guess_dist_arch_archive "$suite" "$arch"`"
        fi
        mirror="`get_archive_url "$archive"`"
    fi
    add_output_sources "$with_sources" "$mirror" "$suite" "$components"
    for pocket in $pockets; do
        add_output_sources "$with_sources" "$mirror" "$suite-$pocket" "$components"
    done
    # NB: archive might be empty; best effort to try to guess it
    if [ -z "$archive" ]; then
        archive="`guess_dist_arch_archive "$suite" "$arch"`" 2>/dev/null || true
    fi
    set_debootstrap_suite "$archive" "$suite"
    debootstrap_mirror="$mirror"
fi

command="$1"
case $1 in
  apt-sources)
    echo "$sources"
  ;;
  debootstrap-mirror)
    echo "$debootstrap_mirror"
  ;;
  debootstrap-suite)
    echo "$debootstrap_suite"
  ;;
  *)
    usage
    exit 1
  ;;
esac