File: mk-sbuild

package info (click to toggle)
ubuntu-dev-tools 0.206
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,172 kB
  • sloc: python: 9,120; sh: 1,304; perl: 135; makefile: 10
file content (1129 lines) | stat: -rwxr-xr-x 36,502 bytes parent folder | download
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
#!/bin/bash
#
# Copyright 2006-2013 (C) Canonical Ltd.
# Authors:
#  Kees Cook <kees@ubuntu.com>
#  Emmet Hikory <persia@ubuntu.com>
#  Scott Moser <smoser@ubuntu.com>
#  Stefano Rivera <stefanor@ubuntu.com>
#  Steve Langasek <steve.langasek@ubuntu.com>
#  Marc Deslauriers <marc.deslauriers@ubuntu.com>
#
# ##################################################################
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# See file /usr/share/common-licenses/GPL for more details.
#
# ##################################################################
#
# This script creates chroots designed to be used in a snapshot mode
# (with LVM, btrfs, zfs, overlay, overlay or aufs) with schroot and sbuild.
# Much love to "man sbuild-setup", https://wiki.ubuntu.com/PbuilderHowto,
# and https://help.ubuntu.com/community/SbuildLVMHowto.
#
# It will deal with sbuild having not be installed and configured before.
set -e

# Set up configurable defaults (loaded after option processing)
LV_SIZE="5G"
SNAPSHOT_SIZE="4G"
SOURCE_CHROOTS_DIR="/var/lib/schroot/chroots"
SOURCE_CHROOTS_TGZ="/var/lib/schroot/tarballs"
CHROOT_SNAPSHOT_DIR="/var/lib/schroot/snapshots"
SCHROOT_PROFILE="sbuild"
CCACHE_DIR="/var/cache/ccache-sbuild"
CCACHE_SIZE="4G"

function usage()
{
    echo "Usage: $0 [OPTIONS] Release"
    echo "Options:"
    echo "  --arch=ARCH                What architecture to select"
    echo "  --name=NAME                Base name for the schroot (arch is appended)"
    echo "  --personality=PERSONALITY  What personality to use (defaults to match --arch)"
    echo "  --vg=VG                    use LVM snapshots, with group VG"
    echo "  --zfs-dataset=DATASET      use ZFS snapshots, with parent dataset DATASET"
    echo "  --debug                    Turn on script debugging"
    echo "  --skip-updates             Do not include -updates pocket in sources.list"
    echo "  --skip-security            Do not include -security pocket in sources.list"
    echo "  --skip-proposed            Do not include -proposed pocket in sources.list"
    echo "  --source-template=FILE     Use FILE as the sources.list template"
    echo "  --debootstrap-mirror=URL   Use URL as the debootstrap source"
    echo "  --debootstrap-include=list Comma separated list of packages to include"
    echo "  --debootstrap-exclude=list Comma separated list of packages to exclude"
    echo "  --debootstrap-opts=OPTS    Extra options passed to debootstrap"
    echo "  --debootstrap-proxy=URL    Use PROXY as apt proxy"
    echo "  --debootstrap-keyring=KEYRING"
    echo "                             Use KEYRING to check signatures of retrieved Release files"
    echo "  --debootstrap-no-check-gpg Disables checking gpg signatures of retrieved Release files"
    echo "  --skip-eatmydata           Don't install and use eatmydata"
    echo "  --eatmydata                Install and use eatmydata (default)"
    echo "  --ccache                   Install configure and use ccache as default"
    echo "  --ccache-dir=PATH          Sets the CCACHE_DIR to PATH"
    echo "                                 (can be shared between all schroots, defaults to ${CCACHE_DIR})"
    echo "  --ccache-size=SIZE         Sets the ccache max-size to SIZE"
    echo "                                 (shared by each CCACHE_DIR, defaults to ${CCACHE_SIZE})"
    echo "  --distro=DISTRO            Install specific distro:"
    echo "                                 'ubuntu' or 'debian' "
    echo "                                 (defaults to determining from release name)"
    echo "  --target=ARCH              Target architecture for cross-building"
    echo "  --type=SCHROOT_TYPE        Define the schroot type:"
    echo "                                 'directory' (default), 'file', or 'btrfs-snapshot'."
    echo "                                 'lvm-snapshot' is selected via --vg"
    echo "                                 'zfs-snapshot' is selected via --zfs-dataset"
    echo ""
    echo "Configuration (via ~/.mk-sbuild.rc)"
    echo "  LV_SIZE                    Size of source LVs (default ${LV_SIZE})"
    echo "  SNAPSHOT_SIZE              Size of snapshot LVs (default ${SNAPSHOT_SIZE})"
    echo "  SOURCE_CHROOTS_DIR         Directory to store directory source chroots"
    echo "  SOURCE_CHROOTS_TGZ         Directory to store file source chroots"
    echo "  CHROOT_SNAPSHOT_DIR        Directory to mount open btrfs snaphshot chroots (default ${CHROOT_SNAPSHOT_DIR})"
    echo "  SCHROOT_CONF_SUFFIX        Lines to append to schroot.conf entries"
    echo "  SCHROOT_PROFILE            Profile to use with schroot (default ${SCHROOT_PROFILE})"
    echo "  SKIP_UPDATES               Enable --skip-updates"
    echo "  SKIP_PROPOSED              Enable --skip-proposed"
    echo "  SKIP_SECURITY              Enable --skip-security"
    echo "  DEBOOTSTRAP_MIRROR         Mirror location (same as --debootstrap-mirror)"
    echo "  DEBOOTSTRAP_INCLUDE        Included packages (same as --debootstrap-include)"
    echo "  DEBOOTSTRAP_EXCLUDE        Excluded packages (same as --debootstrap-exclude)"
    echo "  DEBOOTSTRAP_OPTS           Extra options passed to debootstrap (same as --debootstrap-opts)"
    echo "  DEBOOTSTRAP_PROXY          Apt proxy (same as --debootstrap-proxy)"
    echo "  DEBOOTSTRAP_KEYRING        GPG keyring (same as --debootstrap-keyring)"
    echo "  DEBOOTSTRAP_NO_CHECK_GPG   Disable GPG verification (same as --debootstrap-no-check-gpg)"
    echo "  EATMYDATA                  Enable or disable eatmydata usage, see --eatmydata and --skip-eatmydata"
    echo "  CCACHE                     Enable --ccache"
    echo "  CCACHE_DIR                 Path for ccache (can be shared between all schroots, "
    echo "                                 same as --ccache-dir, default ${CCACHE_DIR})"
    echo "  CCACHE_SIZE                Sets the ccache max-size (shared by each CCACHE_DIR, "
    echo "                                 same as --ccache-size, default ${CCACHE_SIZE})"
    echo "  TEMPLATE_SOURCES           A template for sources.list"
    echo "  TEMPLATE_SCHROOTCONF       A template for schroot.conf stanza"
    if [ -z "$1" ]; then
        exit 1
    fi
    exit $1
}


if [ -z "$1" ]; then
    usage
fi
supported_options=(
    help
    debug
    skip-updates
    skip-security
    skip-proposed
    skip-eatmydata
    ccache
    arch:
    name:
    source-template:
    debootstrap-mirror:
    debootstrap-include:
    debootstrap-exclude:
    debootstrap-opts:
    debootstrap-proxy:
    debootstrap-no-check-gpg
    debootstrap-keyring:
    personality:
    distro:
    vg:
    zfs-dataset:
    type:
    target:
    ccache-dir:
    ccache-size:
)
OPTS=$(getopt -o 'h' --long "$(IFS=, && echo "${supported_options[*]}")" -- "$@")
eval set -- "$OPTS"

VG=""
DISTRO=""
COMMAND_PREFIX=""
name=""
proxy="_unset_"
DEBOOTSTRAP_NO_CHECK_GPG=0
EATMYDATA=1
CCACHE=0
USE_PKGBINARYMANGLER=0

while :; do
    case "$1" in
        --debug)
            DEBUG=1
            set -x
            shift
            ;;
        --arch)
            CHROOT_ARCH="$2"
            case $2 in
                armhf|i386)
                    if [ -z "$personality" ]; then
                        personality="linux32"
                    fi
                    ;;
            esac
            shift 2
            ;;
        --personality)
            personality="$2"
            shift 2
            ;;
        --skip-updates)
            SKIP_UPDATES="1"
            shift
            ;;
        --skip-proposed)
            SKIP_PROPOSED="1"
            shift
            ;;
        --skip-security)
            SKIP_SECURITY="1"
            shift
            ;;
        --name)
            name="$2"
            shift 2
            ;;
        --source-template)
            TEMPLATE_SOURCES="$2"
            shift 2
            if [ ! -r $TEMPLATE_SOURCES ]; then
                echo "W: Template file $TEMPLATE_SOURCES is not readable"
                echo "W: Continuing with default sources!"
            fi
            ;;
        --debootstrap-mirror)
            DEBOOTSTRAP_MIRROR="$2"
            shift 2
            ;;
        --debootstrap-include)
            DEBOOTSTRAP_INCLUDE="$2"
            shift 2
            ;;
        --debootstrap-exclude)
            DEBOOTSTRAP_EXCLUDE="$2"
            shift 2
            ;;
        --debootstrap-opts)
            DEBOOTSTRAP_OPTS="$2"
            shift 2
            ;;
        --debootstrap-proxy)
            proxy="$2"
            shift 2
            ;;
        --debootstrap-keyring)
            # Store the absolute path because we cd to the root directory later.
            DEBOOTSTRAP_KEYRING=$(readlink -f "$2")
            shift 2
            ;;
        --debootstrap-no-check-gpg)
            DEBOOTSTRAP_NO_CHECK_GPG=1
            shift
            ;;
        --skip-eatmydata)
            EATMYDATA=0
            shift
            ;;
        --ccache)
            CCACHE=1
            shift
            ;;
        --distro)
            DISTRO="$2"
            shift 2
            ;;
        --vg)
            VG="$2"
            shift 2
            ;;
        --zfs-dataset)
            ZFS_PARENT_DATASET="$2"
            shift 2
            ;;
        --type)
            SCHROOT_TYPE="$2"
            shift 2
            ;;
        --target)
            TARGET_ARCH="$2"
            shift 2
            ;;
        --ccache-dir)
            CCACHE_DIR="$2"
            shift 2
            ;;
        --ccache-size)
            CCACHE_SIZE="$2"
            shift 2
            ;;
        --)
            shift
            break
            ;;
        -h|--help|*)
            usage 0
            ;;
     esac
done

# For when schroot enters the chroot, we cannot be in a directory that
# will not exist in the chroot.
cd /

if [ -w /etc/passwd -a ! -e ~/.sbuildrc -a ! -e ~/.mk-sbuild.rc ]; then
    cat >&2 <<EOF
It's recommended to run this script as a regular user, not root, so that it
uses the configuration files in your home directory.
It will use sudo to escalate to root as necessary.

If you really do want to use it as root, create a .sbuildrc or .mk-sbuild.rc
in root's home.
EOF
    exit 1
fi

# Perform once-only things to initially set up for using sbuild+schroot
if [ ! -w /var/lib/sbuild ]; then
    # Load all the packages you'll need to do work
    sudo apt-get install sbuild schroot debootstrap
    # Add self to the sbuild group
    sudo adduser "$USER" sbuild

    # Prepare a usable default .sbuildrc
    if [ ! -e ~/.sbuildrc ]; then
        cat > ~/.sbuildrc <<EOM
# *** THIS COMMAND IS DEPRECATED ***
#
# In sbuild 0.87.0 and later, the unshare backend is available. This is
# expected to become the default in a future release.
#
# This is the new preferred way of building Debian packages, making the manual
# creation of schroots no longer necessary. To retain the default behavior,
# you may remove this comment block and continue.
#
# To test the unshare backend while retaining the default settings, run sbuild
# with --chroot-mode=unshare like this:
# $ sbuild --chroot-mode=unshare --dist=unstable hello
#
# To switch to the unshare backend by default (recommended), uncomment the
# following lines and delete the rest of the file (with the exception of the
# last two lines):
#\$chroot_mode = 'unshare';
#\$unshare_mmdebstrap_keep_tarball = 1;

# *** VERIFY AND UPDATE \$mailto and \$maintainer_name BELOW ***

# Name to use as override in .changes files for the Maintainer: field
#\$maintainer_name='$USER <$USER@localhost>';

# Directory for chroot symlinks and sbuild logs.  Defaults to the
# current directory if unspecified.
#\$build_dir='$HOME/ubuntu/build';

# Directory for writing build logs to
\$log_dir="$HOME/ubuntu/logs";

# don't remove this, Perl needs it:
1;
EOM
        sensible-editor ~/.sbuildrc
        # Create target directories, if needed
        eval $(egrep '^\$(build|log)_dir[ 	]*=' ~/.sbuildrc | cut -c2-)
        if [ -n "$log_dir" ]; then
            mkdir -p "$log_dir"
        fi
        if [ -n "$build_dir" ]; then
            mkdir -p "$build_dir"
        fi
    else
        echo "Your ~/.sbuildrc already exists -- leaving it as-is."
    fi

    echo '***********************************************'
    echo '* Before continuing, you MUST restart your    *'
    echo '* session to gain "sbuild" group permissions! *'
    echo '***********************************************'
    exit 0
fi

if ! id | fgrep -q '(sbuild)'; then
    echo "You must be a member of the 'sbuild' group." >&2
    exit 1
fi

# To build the chroot, we need to know which release of Ubuntu to debootstrap
RELEASE="$1"
if [ -z "$RELEASE" ]; then
    usage
fi

# Determine distribution and possible synonyms
synonym=""
EXPERIMENTAL=0
if [ "$RELEASE" = "experimental" ]; then
    DISTRO="${DISTRO:-debian}"
    EXPERIMENTAL=1
    name="${name:-experimental}"
    RELEASE=$(debian-distro-info --devel)
elif debian-distro-info --all | grep -Fqx "$RELEASE"; then
    DISTRO="${DISTRO:-debian}"
    if [ "$RELEASE" = $(debian-distro-info --devel) ]; then
        synonym=unstable
    elif [ "$RELEASE" = $(debian-distro-info --testing) ]; then
        synonym=testing
    elif [ "$RELEASE" = $(debian-distro-info --stable) ]; then
        synonym=stable
    elif [ "$RELEASE" = $(debian-distro-info --old) ]; then
        synonym=oldstable
    fi
elif ubuntu-distro-info --all | grep -Fqx "$RELEASE"; then
    DISTRO="${DISTRO:-ubuntu}"
elif [ "$RELEASE" = "unstable" ]; then
    DISTRO="${DISTRO:-debian}"
    synonym="$RELEASE"
    RELEASE=$(debian-distro-info --devel)
elif [ "$RELEASE" = "testing" ]; then
    DISTRO="${DISTRO:-debian}"
    synonym="$RELEASE"
    RELEASE=$(debian-distro-info --testing)
elif [ "$RELEASE" = "stable" ]; then
    DISTRO="${DISTRO:-debian}"
    synonym="$RELEASE"
    RELEASE=$(debian-distro-info --stable)
elif [ "$RELEASE" = "oldstable" ]; then
    DISTRO="${DISTRO:-debian}"
    synonym="$RELEASE"
    RELEASE=$(debian-distro-info --old)
elif [ -z "$DISTRO" ]; then
    echo "Unable to determine distribution, please provide --distro" >&2
    exit 1
fi

# By default DEBOOTSTRAP_SCRIPT must match RELEASE
DEBOOTSTRAP_SCRIPT="$RELEASE"

dist_ge() {
    local releases="$($3-distro-info --all)"
    local left=999
    local right=0
    local seq=1

    for i in $releases; do
        if [ $1 = $i ]; then
            local left=$seq
            break
        fi
        seq=$((seq+1))
    done

    seq=1
    for i in $releases; do
        if [ $2 = $i ]; then
            local right=$seq
            break
        fi
        seq=$((seq+1))
    done

    [ $left -ge $right ] && return 0 || return 1
}

ubuntu_dist_ge () {
    dist_ge $1 $2 ubuntu
}

debian_dist_ge () {
    dist_ge $1 $2 debian
}

if [ "$DISTRO" = "ubuntu" ]; then
    # On Ubuntu, set DEBOOTSTRAP_SCRIPT to gutsy to allow building new RELEASES without new debootstrap
    DEBOOTSTRAP_SCRIPT=gutsy
fi

# By default, name the schroot the same as the release
if [ -z "$name" ]; then
    name="$RELEASE"
else
    # Disable synonym when a custom name is used:
    synonym=""
fi

# By default, use the native architecture.
HOST_ARCH=$(dpkg --print-architecture)
if [ -z "$CHROOT_ARCH" ]; then
    CHROOT_ARCH="$HOST_ARCH"
fi

if [ -z "$TARGET_ARCH" ]; then
    CHROOT_NAME="${name}-${CHROOT_ARCH}"
else
    CHROOT_NAME="${name}-${CHROOT_ARCH}-${TARGET_ARCH}"
fi

if [ -z "$synonym" ]; then
    CHROOT_SYNONYM=""
else
    CHROOT_SYNONYM="${synonym}-${CHROOT_ARCH}"
fi

# Load customizations
if [ -r ~/.mk-sbuild.rc ]; then
    . ~/.mk-sbuild.rc
fi

# Will eatmydata be available?
if [ $EATMYDATA -eq 1 ]; then
    case "$RELEASE" in
        hardy|lucid|maverick|lenny|squeeze)
            echo "eatmydata is known not to be available in $RELEASE, ignoring --eatmydata"
            EATMYDATA=0
            ;;
        *)
            DEBOOTSTRAP_INCLUDE="${DEBOOTSTRAP_INCLUDE:+$DEBOOTSTRAP_INCLUDE,}eatmydata"
            ;;
    esac
fi

if [ $CCACHE -eq 1 ]; then
    if [ -z "$CCACHE_DIR" ] || [[ "$(dirname "$CCACHE_DIR")" == '/' ]]; then
        echo "Invalid ccache dir: ${CCACHE_DIR}" >&2
        exit 1
    fi

    # We can safely use a global cache path, in such case changing size applies
    # to all the schroots
    setup_script="$CCACHE_DIR"/mk-sbuild-setup
    if [ -d "$CCACHE_DIR" ]; then
        echo "Reusing $CCACHE_DIR as CCACHE_DIR, will be configured to use max-size=${CCACHE_SIZE}"
        rm -f "$setup_script"
    else
        echo "Configuring $CCACHE_DIR as CCACHE_DIR with max-size=${CCACHE_SIZE}"
        sudo install --group=sbuild --mode=2775 -d "$CCACHE_DIR"
    fi

    if [ ! -x "$setup_script" ]; then
        cat <<END | sudo tee "$setup_script" 1>/dev/null
#!/bin/sh
export CCACHE_DIR="$CCACHE_DIR"
export CCACHE_MAXSIZE="${CCACHE_SIZE}"
export CCACHE_UMASK=002
export CCACHE_COMPRESS=1
unset CCACHE_HARDLINK
export CCACHE_NOHARDLINK=1
export PATH="/usr/lib/ccache:\$PATH"
exec "\$@"
END
        sudo chmod a+rx "$setup_script"
    fi

    if ! sudo grep -qs "$CCACHE_DIR" /etc/schroot/sbuild/fstab; then
        # This acts on host configuration, but there is no other way to handle
        # this, however it won't affect anything
        cat <<END | sudo tee -a /etc/schroot/sbuild/fstab 1>/dev/null
${CCACHE_DIR} ${CCACHE_DIR} none rw,bind 0 0
END
    fi

    DEBOOTSTRAP_INCLUDE="${DEBOOTSTRAP_INCLUDE:+$DEBOOTSTRAP_INCLUDE,}ccache"
    BUILD_PKGS="$BUILD_PKGS ccache"
    COMMAND_PREFIX="${COMMAND_PREFIX:+$COMMAND_PREFIX,}$setup_script"
fi

if [ -z "$SCHROOT_TYPE" ]; then
    # To build the LV, we need to know which volume group to use
    if [ -n "$VG" ]; then
        SCHROOT_TYPE="lvm-snapshot"
    # To build the ZFS dataset, we need to know which parent to use
    elif [ -n "$ZFS_PARENT_DATASET" ]; then
        SCHROOT_TYPE="zfs-snapshot"
    else
        SCHROOT_TYPE="directory"
    fi
fi

case "$SCHROOT_TYPE" in
"lvm-snapshot")
    # Make sure LVM tools that operate on the snapshots have needed module
    if ! sudo dmsetup targets | grep -q ^snapshot; then
        sudo modprobe dm_snapshot
        echo dm_snapshot | sudo tee -a /etc/modules >/dev/null
    fi

    # Set up some variables for use in the paths and names
    if [ -z "$TARGET_ARCH" ]; then
        CHROOT_LV="${name}_${CHROOT_ARCH}_chroot"
    else
        CHROOT_LV="${name}_${CHROOT_ARCH}_${TARGET_ARCH}_chroot"
    fi
    CHROOT_PATH="/dev/$VG/$CHROOT_LV"

    # Install lvm2 if missing
    if ! dpkg -l lvm2 >/dev/null 2>&1; then
        sudo apt-get install lvm2
    fi

    # Does the specified VG exist?  (vgdisplay doesn't set error codes...)
    if [ `sudo vgdisplay -c "$VG" | wc -l` -eq 0 ]; then
        echo "Volume group '${VG}' does not appear to exist" >&2
        exit 1
    fi
    ;;
"directory")
    if [ ! -d "${SOURCE_CHROOTS_DIR}" ]; then
        sudo mkdir -p "${SOURCE_CHROOTS_DIR}"
    fi
    # Set up some variables for use in the paths and names
    CHROOT_PATH="${SOURCE_CHROOTS_DIR}/${CHROOT_NAME}"
    ;;
"file")
    if [ ! -d "$SOURCE_CHROOTS_TGZ" ]; then
        sudo mkdir -p "$SOURCE_CHROOTS_TGZ"
    fi
    # Set up some variables for use in the paths and names
    CHROOT_PATH="${SOURCE_CHROOTS_TGZ}/${CHROOT_NAME}.tgz"
    ;;
"btrfs-snapshot" | "zfs-snapshot")
    if [ ! -d "${SOURCE_CHROOTS_DIR}" ]; then
        sudo mkdir -p "${SOURCE_CHROOTS_DIR}"
    fi
    if [ ! -d "${CHROOT_SNAPSHOT_DIR}" ]; then
        sudo mkdir -p "${CHROOT_SNAPSHOT_DIR}"
    fi
    CHROOT_PATH="${SOURCE_CHROOTS_DIR}/${CHROOT_NAME}"
    ;;
*)
    echo 'unknown source type!?' >&2
    exit 1
    ;;
esac

# Is the specified release known to debootstrap?
variant_opt="--variant=buildd"
if [ ! -r "/usr/share/debootstrap/scripts/$DEBOOTSTRAP_SCRIPT" ]; then
    echo "Specified release ($DEBOOTSTRAP_SCRIPT) not known to debootstrap" >&2
    exit 1
fi

BUILD_PKGS="build-essential fakeroot apt-utils"
# Handle distro-specific logic, unknown to debootstrap
case "$DISTRO" in
ubuntu)
    if [ -z "$DEBOOTSTRAP_MIRROR" ]; then
        case "$CHROOT_ARCH" in
        amd64 | i386)
            DEBOOTSTRAP_MIRROR="http://archive.ubuntu.com/ubuntu"
            ;;
        *)
            DEBOOTSTRAP_MIRROR="http://ports.ubuntu.com/ubuntu-ports"
            ;;
        esac
    fi
    if [ -z "$COMPONENTS" ]; then
        COMPONENTS="main restricted universe multiverse"
    fi
    if [ -z "$SOURCES_PROPOSED_SUITE" ]; then
        SOURCES_PROPOSED_SUITE="RELEASE-proposed"
    fi
    if [ -z "$SOURCES_SECURITY_SUITE" ]; then
        SOURCES_SECURITY_SUITE="RELEASE-security"
    fi
    if [ -z "$SOURCES_SECURITY_URL" ]; then
        case "$CHROOT_ARCH" in
        amd64 | i386)
            SOURCES_SECURITY_URL="http://security.ubuntu.com/ubuntu"
            ;;
        *)
            SOURCES_SECURITY_URL="http://ports.ubuntu.com/ubuntu-ports"
            ;;
        esac
    fi
    if [ -n "$TARGET_ARCH" ]; then
        # target chroots only supported in precise and later, so ignore
        # the fact that powerpc was once not on ports.
        case "$TARGET_ARCH" in
        amd64 | i386)
            TARGET_MIRROR="http://archive.ubuntu.com/ubuntu"
            TARGET_SOURCES_SECURITY_URL="http://security.ubuntu.com/ubuntu"
            ;;
        *)
            TARGET_MIRROR="http://ports.ubuntu.com/ubuntu-ports"
            TARGET_SOURCES_SECURITY_URL="http://ports.ubuntu.com/ubuntu-ports"
            ;;
        esac
    fi
    # Add edgy+ buildd tools
    if ubuntu_dist_ge "$RELEASE" "edgy"; then
        # Add pkgbinarymangler (edgy and later)
        BUILD_PKGS="$BUILD_PKGS pkgbinarymangler"
        USE_PKGBINARYMANGLER=1
        # Disable recommends for a smaller chroot (gutsy and later only)
        if ubuntu_dist_ge "$RELEASE" "gutsy"; then
            BUILD_PKGS="--no-install-recommends $BUILD_PKGS"
            SKIP_RECOMMENDS=1
        fi
        # Add pkg-create-dbgsym (edgy through zesty)
        if ! ubuntu_dist_ge "$RELEASE" "artful"; then
            BUILD_PKGS="$BUILD_PKGS pkg-create-dbgsym"
        fi
    fi
    ;;
debian)
    if [ -z "$DEBOOTSTRAP_MIRROR" ]; then
        DEBOOTSTRAP_MIRROR="http://deb.debian.org/debian"
    fi
    if [ -z "$COMPONENTS" ]; then
        COMPONENTS="main non-free non-free-firmware contrib"
    fi
    if [ -z "$SOURCES_PROPOSED_SUITE" ]; then
        SOURCES_PROPOSED_SUITE="RELEASE-proposed-updates"
    fi
    # Debian only performs security updates
    SKIP_UPDATES=1
    if [ -z "$SOURCES_SECURITY_SUITE" ]; then
        if debian_dist_ge "$RELEASE" "bullseye"; then
            SOURCES_SECURITY_SUITE="RELEASE-security"
        else
            SOURCES_SECURITY_SUITE="RELEASE/updates"
        fi
    fi
    if [ -z "$SOURCES_SECURITY_URL" ]; then
        SOURCES_SECURITY_URL="http://security.debian.org/"
    fi
    if [ -n "$TARGET_ARCH" ]; then
        TARGET_MIRROR="$DEBOOTSTRAP_MIRROR"
        TARGET_SOURCES_SECURITY_URL="$SOURCES_SECURITY_URL"
    fi
    # Unstable and Experimental do not have security or proposed repositories
    if [ "$RELEASE" = 'unstable' ] || [ "$RELEASE" = 'sid' ] || [ "$RELEASE" = 'experimental' ]; then
        SKIP_SECURITY=1
        SKIP_PROPOSED=1
    fi
    # Keep the chroot as minimal as possible
    BUILD_PKGS="--no-install-recommends $BUILD_PKGS"
    SKIP_RECOMMENDS=1
    ;;
*)
    echo "Unknown --distro '$DISTRO': aborting" >&2
    exit 1
    ;;
esac

if [ -n "$TARGET_ARCH" ]; then
    # Ultimately we would like there to be a "cross-build-essential-$arch"
    # package.  In practice, the cross-g++ package is sufficient to pull in
    # everything we need.
    if ! target_tuple=$(dpkg-architecture -a"$TARGET_ARCH" -qDEB_HOST_GNU_TYPE 2>/dev/null)
    then
        echo "Unknown target architecture $TARGET_ARCH" >&2
        exit 1
    fi
    BUILD_PKGS="$BUILD_PKGS g++-$target_tuple pkg-config dpkg-cross libc-dev:$TARGET_ARCH"
fi

debootstrap_opts="--components=$(echo $COMPONENTS | tr ' ' ,)"
if [ -n "$DEBOOTSTRAP_INCLUDE" ] ; then
    debootstrap_opts="$debootstrap_opts --include=$DEBOOTSTRAP_INCLUDE"
fi

if [ -n "$DEBOOTSTRAP_EXCLUDE" ] ; then
    debootstrap_opts="$debootstrap_opts --exclude=$DEBOOTSTRAP_EXCLUDE"
fi

if [ $DEBOOTSTRAP_NO_CHECK_GPG -eq 1 ]; then
    debootstrap_opts="$debootstrap_opts --no-check-gpg"
elif [ -n "$DEBOOTSTRAP_KEYRING" ]; then
    debootstrap_opts="$debootstrap_opts --keyring=$DEBOOTSTRAP_KEYRING"
fi

if [ -n "$DEBOOTSTRAP_OPTS" ] ; then
    debootstrap_opts="$debootstrap_opts $DEBOOTSTRAP_OPTS"
fi

# if http_proxy is set in the environment (even empty) set 'proxy' to it
[ "$proxy" = "_unset_" -a "${DEBOOTSTRAP_PROXY-xx}" != "xx" ] &&
    proxy=${DEBOOTSTRAP_PROXY}
[ "$proxy" = "_unset_" -a "${http_proxy-xx}" != "xx" ] && proxy=${http_proxy}
if [ "$proxy" = "_unset_" ]; then
    _out=$(apt-config shell x Acquire::HTTP::Proxy) &&
        _out=$(sh -c 'eval $1 && echo $x' -- "$_out") && [ -n "$_out" ] &&
        proxy="$_out"
fi
[ "$proxy" = "_unset_" ] && proxy=""

DEBOOTSTRAP_COMMAND=debootstrap
# Use qemu-kvm-extras-static for foreign chroots
if [ "$CHROOT_ARCH" != "$HOST_ARCH" ] ; then
    case "$CHROOT_ARCH-$HOST_ARCH" in
    # Sometimes we don't need qemu
    amd64-i386|arm64-armhf|armhf-arm64|i386-amd64|powerpc-ppc64|ppc64-powerpc)
        ;;
    # Sometimes we do
    *)
        DEBOOTSTRAP_COMMAND=debootstrap
        if ! which "qemu-x86_64-static"; then
            sudo apt-get install qemu-user-static
        fi
        ;;
    esac
fi

case "$SCHROOT_TYPE" in
"lvm-snapshot")
    # Allocate the "golden" chroot LV
    sudo lvcreate -n "$CHROOT_LV" -L "$LV_SIZE" "$VG"
    sudo mkfs -t ext4 "$CHROOT_PATH"

    # Mount
    MNT=`mktemp -d -t schroot-XXXXXX`
    sudo mount "$CHROOT_PATH" "$MNT"
    ;;
"directory")
    MNT="${CHROOT_PATH}"
    if [ -d "${MNT}" ]; then
        echo "E: ${MNT} already exists; aborting" >&2
        exit 1
    fi
    sudo mkdir -p "${MNT}"
    ;;
"btrfs-snapshot")
    MNT="${CHROOT_PATH}"
    if sudo btrfs subvolume list "${MNT}" >/dev/null 2>&1; then
        echo "E: Subvolume ${MNT} already exists; aborting" >&2
        exit 1
    fi
    sudo btrfs subvolume create "${MNT}"
    ;;

"zfs-snapshot")
    ZFS_DATASET="${ZFS_PARENT_DATASET}/${CHROOT_NAME}"
    if sudo zfs list "${ZFS_DATASET}" >/dev/null 2>&1; then
        echo "E: ZFS dataset ${ZFS_DATASET} already exists; aborting" >&2
        exit 1
    fi
    sudo zfs create -p -o mountpoint=legacy "${ZFS_DATASET}"

    # Mount
    MNT=`mktemp -d -t schroot-XXXXXX`
    sudo mount -t zfs "${ZFS_DATASET}" "${MNT}"
    ;;
"file")
    MNT=`mktemp -d -t schroot-XXXXXX`
esac

case "$SCHROOT_TYPE" in
    directory|file)
        if grep -q '\soverlay$' /proc/filesystems \
                || /sbin/modprobe -q --dry-run overlay; then
            OVERLAY_FS=overlay
        elif grep -q '\soverlayfs$' /proc/filesystems \
                || /sbin/modprobe -q --dry-run overlayfs; then
            OVERLAY_FS=overlayfs
        else
            OVERLAY_FS=aufs
        fi
esac

# work around apt's GPG invocation that fails without root's .gnupg directory
sudo mkdir -p -m 0700 "$MNT"/root/.gnupg

# debootstrap the chroot
sudo ${proxy:+"http_proxy=${proxy}"} "$DEBOOTSTRAP_COMMAND" --arch="$CHROOT_ARCH" $variant_opt $debootstrap_opts "$RELEASE" "$MNT" "${DEBOOTSTRAP_MIRROR:-http://archive.ubuntu.com/ubuntu}" "$DEBOOTSTRAP_SCRIPT"

if [ $EATMYDATA -eq 1 ]; then
    sudo mkdir -p "${MNT}/usr/local/libexec/mk-sbuild"
    sudo ln -s /usr/bin/eatmydata "${MNT}/usr/local/libexec/mk-sbuild/dpkg"
    echo 'Dir::Bin::dpkg "/usr/local/libexec/mk-sbuild/dpkg";' \
        | sudo tee "${MNT}/etc/apt/apt.conf.d/00mk-sbuild-eatmydata" > /dev/null
fi

# Update the package sources
TEMP_SOURCES=`mktemp -t sources-XXXXXX`
if [ -z "$TEMPLATE_SOURCES" ]; then
    TEMPLATE_SOURCES=~/.mk-sbuild.sources
fi
if [ -n "$TARGET_ARCH" ]; then
    MIRROR_ARCHS="[arch=$CHROOT_ARCH] "
fi
if [ -r "$TEMPLATE_SOURCES" ]; then
    cat "$TEMPLATE_SOURCES" > "$TEMP_SOURCES"
else
    cat > "$TEMP_SOURCES" <<EOM
deb ${MIRROR_ARCHS}${DEBOOTSTRAP_MIRROR} RELEASE ${COMPONENTS}
deb-src ${DEBOOTSTRAP_MIRROR} RELEASE ${COMPONENTS}
EOM
    if [ -n "$TARGET_ARCH" ]; then
        cat >> "$TEMP_SOURCES" <<EOM
deb [arch=$TARGET_ARCH] $TARGET_MIRROR RELEASE $COMPONENTS
EOM
    fi
    if [ "$EXPERIMENTAL" -eq 1 ]; then
        cat >> "$TEMP_SOURCES" <<EOM
deb ${MIRROR_ARCHS}${DEBOOTSTRAP_MIRROR} experimental ${COMPONENTS}
deb-src ${DEBOOTSTRAP_MIRROR} experimental ${COMPONENTS}
EOM
        if [ -n "$TARGET_ARCH" ]; then
            cat >> "$TEMP_SOURCES" <<EOM
deb [arch=$TARGET_ARCH] $TARGET_MIRROR experimental $COMPONENTS
EOM
        fi
    fi
    if [ -z "$SKIP_UPDATES" ]; then
        cat >> "$TEMP_SOURCES" <<EOM
deb ${MIRROR_ARCHS}${DEBOOTSTRAP_MIRROR} RELEASE-updates ${COMPONENTS}
deb-src ${DEBOOTSTRAP_MIRROR} RELEASE-updates ${COMPONENTS}
EOM
        if [ -n "$TARGET_ARCH" ]; then
            cat >> "$TEMP_SOURCES" <<EOM
deb [arch=$TARGET_ARCH] $TARGET_MIRROR RELEASE-updates $COMPONENTS
EOM
        fi
    fi
    if [ -z "$SKIP_PROPOSED" ]; then
	TEMP_PREFERENCES=`mktemp -t preferences-XXXXXX`
        cat >> "$TEMP_PREFERENCES" <<EOM
# override for NotAutomatic: yes
Package: *
Pin: release a=*-proposed
Pin-Priority: 500
EOM
        cat >> "$TEMP_SOURCES" <<EOM
deb ${MIRROR_ARCHS}${DEBOOTSTRAP_MIRROR} $SOURCES_PROPOSED_SUITE ${COMPONENTS}
deb-src ${DEBOOTSTRAP_MIRROR} $SOURCES_PROPOSED_SUITE ${COMPONENTS}
EOM
        if [ -n "$TARGET_ARCH" ]; then
            cat >> "$TEMP_SOURCES" <<EOM
deb [arch=$TARGET_ARCH] $TARGET_MIRROR $SOURCES_PROPOSED_SUITE $COMPONENTS
EOM
        fi
    fi
    if [ -z "$SKIP_SECURITY" ]; then
        cat >> "$TEMP_SOURCES" <<EOM
deb ${MIRROR_ARCHS}${SOURCES_SECURITY_URL} ${SOURCES_SECURITY_SUITE} ${COMPONENTS}
deb-src ${SOURCES_SECURITY_URL} ${SOURCES_SECURITY_SUITE} ${COMPONENTS}
EOM
        if [ -n "$TARGET_ARCH" ]; then
            cat >> "$TEMP_SOURCES" <<EOM
deb [arch=$TARGET_ARCH] $TARGET_SOURCES_SECURITY_URL $SOURCES_SECURITY_SUITE $COMPONENTS
EOM
        fi
    fi
fi
cat "$TEMP_SOURCES" | sed -e "s|RELEASE|$RELEASE|g" | \
    sudo bash -c "cat > $MNT/etc/apt/sources.list"
rm -f "$TEMP_SOURCES"
if [ -n "$TEMP_PREFERENCES" ]; then
    sudo mv "$TEMP_PREFERENCES" $MNT/etc/apt/preferences.d/proposed.pref
fi

# Copy the timezone (uncomment this if you want to use your local time zone)
#sudo cp -P --remove-destination /etc/localtime /etc/timezone "$MNT"/etc/
# Create a schroot entry for this chroot
TEMP_SCHROOTCONF=`mktemp -t schrootconf-XXXXXX`
TEMPLATE_SCHROOTCONF=~/.mk-sbuild.schroot.conf
TYPED_TEMPLATE_SCHROOTCONF="${TEMPLATE_SCHROOTCONF}.${SCHROOT_TYPE}"

if [ -r "${TYPED_TEMPLATE_SCHROOTCONF}" ]; then
    cat "${TYPED_TEMPLATE_SCHROOTCONF}" > "$TEMP_SCHROOTCONF"
elif [ -r "${TEMPLATE_SCHROOTCONF}" ]; then
    cat "$TEMPLATE_SCHROOTCONF" > "$TEMP_SCHROOTCONF"
else
    ADMIN_GROUPS="sbuild,root"
    if getent group admin > /dev/null; then
        ADMIN_GROUPS+=",admin"
    fi
    cat > "$TEMP_SCHROOTCONF" <<EOM
[CHROOT_NAME]
description=CHROOT_NAME
groups=$ADMIN_GROUPS
root-groups=$ADMIN_GROUPS
# Uncomment these lines to allow members of these groups to access
# the -source chroots directly (useful for automated updates, etc).
#source-root-users=$ADMIN_GROUPS
#source-root-groups=$ADMIN_GROUPS
type=SCHROOT_TYPE
profile=$SCHROOT_PROFILE
EOM
    if [ -n "$COMMAND_PREFIX" ]; then
    cat >> "$TEMP_SCHROOTCONF" <<EOM
command-prefix=${COMMAND_PREFIX}
EOM
    fi
    case "$SCHROOT_TYPE" in
    "lvm-snapshot")
        cat >> "$TEMP_SCHROOTCONF" <<EOM
device=CHROOT_PATH
mount-options=-o noatime
lvm-snapshot-options=--size SNAPSHOT_SIZE
EOM
    ;;
    directory|file)
        cat >> "${TEMP_SCHROOTCONF}" <<EOM
union-type=$OVERLAY_FS
${SCHROOT_TYPE}=CHROOT_PATH
EOM
    ;;
    btrfs-snapshot)
        cat >> "${TEMP_SCHROOTCONF}" <<EOM
btrfs-source-subvolume=CHROOT_PATH
btrfs-snapshot-directory=CHROOT_SNAPSHOT_DIR
EOM
    ;;
zfs-snapshot)
        cat >> "${TEMP_SCHROOTCONF}" <<EOM
zfs-dataset=ZFS_DATASET
EOM
    ;;

    esac
fi
if [ ! -z "$personality" ]; then
    echo "personality=$personality" >> "$TEMP_SCHROOTCONF"
fi
if [ ! -z "$CHROOT_SYNONYM" ]; then
    echo "aliases=$CHROOT_SYNONYM" >> "$TEMP_SCHROOTCONF"
fi
if [ ! -z "$SCHROOT_CONF_SUFFIX" ]; then
    echo "$SCHROOT_CONF_SUFFIX" >> "$TEMP_SCHROOTCONF"
fi
sed -e "s|CHROOT_NAME|$CHROOT_NAME|g" \
    -e "s|CHROOT_PATH|$CHROOT_PATH|g" \
    -e "s|SNAPSHOT_SIZE|$SNAPSHOT_SIZE|g" \
    -e "s|SCHROOT_TYPE|$SCHROOT_TYPE|g" \
    -e "s|CHROOT_SNAPSHOT_DIR|$CHROOT_SNAPSHOT_DIR|g" \
    -e "s|ZFS_DATASET|$ZFS_DATASET|g" \
    "$TEMP_SCHROOTCONF" \
    | sudo tee "/etc/schroot/chroot.d/sbuild-$CHROOT_NAME" > /dev/null
rm -f "$TEMP_SCHROOTCONF"

# Disable daemons in chroot:
sudo bash -c "cat >> $MNT/usr/sbin/policy-rc.d" <<EOM
#!/bin/sh
while true; do
    case "\$1" in
      -*) shift ;;
      makedev) exit 0;;
      x11-common) exit 0;;
      *) exit 101;;
    esac
done
EOM
sudo chmod a+x "$MNT"/usr/sbin/policy-rc.d

# Create image finalization script
sudo bash -c "cat >> $MNT/finish.sh" <<EOM
#!/bin/bash
if [ "$DEBUG" = 1 ]; then
    set -x
fi
set -e
if [ -n "$proxy" ]; then
   mkdir -p /etc/apt/apt.conf.d/
   cat > /etc/apt/apt.conf.d/99mk-sbuild-proxy <<EOF
// proxy settings copied from mk-sbuild
Acquire { HTTP { Proxy "$proxy"; }; };
EOF
fi
EOM

if [ -n "$SKIP_RECOMMENDS" ]; then
    sudo bash -c "cat >> $MNT/finish.sh" <<EOM
   mkdir -p /etc/apt/apt.conf.d/
   cat > /etc/apt/apt.conf.d/99mk-sbuild-no-recommends <<EOF
// disable install recommends
APT::Install-Recommends "0";
EOF
EOM
fi

if [ "$USE_PKGBINARYMANGLER" = 1 ]; then
    sudo bash -c "cat >> $MNT/finish.sh" <<EOM
   mkdir -p /etc/pkgbinarymangler/
   cat > /etc/pkgbinarymangler/maintainermangler.conf <<EOF
# pkgmaintainermangler configuration file

# pkgmaintainermangler will do nothing unless enable is set to "true"
enable: true

# Configure what happens if /CurrentlyBuilding is present, but invalid
# (i. e. it does not contain a Package: field). If "ignore" (default),
# the file is ignored (i. e. the Maintainer field is mangled) and a
# warning is printed. If "fail" (or any other value), pkgmaintainermangler
# exits with an error, which causes a package build to fail.
invalid_currentlybuilding: ignore
EOF
EOM
fi

if [ -n "$TARGET_ARCH" ]; then
    sudo bash -c "cat >> $MNT/finish.sh" <<EOM
# Configure target architecture
dpkg --add-architecture "$TARGET_ARCH"
EOM
fi

sudo bash -c "cat >> $MNT/finish.sh" <<EOM
# Reload package lists
apt-get update || true
# Pull down signature requirements
apt-get -y --force-yes install gnupg ${DISTRO}-keyring
# Reload package lists
apt-get update || true
# Disable debconf questions so that automated builds won't prompt
echo set debconf/frontend Noninteractive | debconf-communicate
echo set debconf/priority critical | debconf-communicate
# Install basic build tool set, trying to match buildd
apt-get -y --force-yes -o Dpkg::Options::="--force-confold" install $BUILD_PKGS
# Set up expected /dev entries
if [ ! -r /dev/stdin ];  then ln -s /proc/self/fd/0 /dev/stdin;  fi
if [ ! -r /dev/stdout ]; then ln -s /proc/self/fd/1 /dev/stdout; fi
if [ ! -r /dev/stderr ]; then ln -s /proc/self/fd/2 /dev/stderr; fi
# Clean up
rm /finish.sh
apt-get clean
EOM
sudo chmod a+x "$MNT"/finish.sh

case "$SCHROOT_TYPE" in
"lvm-snapshot"|"zfs-snapshot")
    sudo umount "$MNT"
    rmdir "$MNT"
    ;;
"directory"|"btrfs-snapshot")
    ;;
"file")
    cd "$MNT"
    sudo tar czf "$CHROOT_PATH" .
    cd /
    sudo rm -r "$MNT"
    ;;
esac

# Run finalization script on the "golden" copy via schroot.
sudo schroot -c source:$CHROOT_NAME -u root /finish.sh

# Finished
echo ""
echo "Done building $CHROOT_NAME."
echo ""
echo " To CHANGE the golden image: sudo schroot -c source:${CHROOT_NAME} -u root"
echo " To ENTER an image snapshot: schroot -c ${CHROOT_NAME}"
echo " To BUILD within a snapshot: sbuild -A -d ${CHROOT_NAME} PACKAGE*.dsc"
if [ -n "$TARGET_ARCH" ] && [ "$CHROOT_ARCH" != "$TARGET_ARCH" ] ; then
    echo " To BUILD for ${TARGET_ARCH}: sbuild -A -d ${CHROOT_NAME} --host ${TARGET_ARCH} PACKAGE*.dsc"
fi
echo ""

# vi: set et: