abcc13d8c8e3c7102a8a8448858f0f82a2854cf7
[shellsnippets/shellsnippets.git] / mksh / debian-dev / c
1 #!/bin/mksh
2 #-
3 # Copyright © 2013, 2014
4 #       Thorsten “mirabilos” Glaser <t.glaser@tarent.de>
5 #
6 # Provided that these terms and disclaimer and all copyright notices
7 # are retained or reproduced in an accompanying document, permission
8 # is granted to deal in this work without restriction, including un‐
9 # limited rights to use, publicly perform, distribute, sell, modify,
10 # merge, give away, or sublicence.
11 #
12 # This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
13 # the utmost extent permitted by applicable law, neither express nor
14 # implied; without malicious intent or gross negligence. In no event
15 # may a licensor, author or contributor be held liable for indirect,
16 # direct, other damage, loss, or other issues arising in any way out
17 # of dealing in the work, even if advised of the possibility of such
18 # damage or existence of a defect, except proven that it results out
19 # of said person’s immediate fault when using the work as intended.
20 #-
21 # Simple-to-use wrapper around eatmydata plus cowbuilder to use with
22 # https://www.mirbsd.org/cvs.cgi/contrib/hosted/tg/deb/pbuilderrc to
23 # select the proper build chroot.
24 #
25 # Example:
26 # c lenny/i386 DEB_BUILD_OPTIONS=parallel=4 linux32 --build foo*.dsc
27
28 unset LANGUAGE
29 export LC_ALL=C
30 DIST=$1
31
32 # attempt to divine which eatmydata method to use; cf. #765579
33 # parts taken from MirBSD-contrib pbuilderrc
34 BASEPATH=/var/cache/pbuilder/base.cow-${DIST%/*}
35 ARCHSUFFIX=
36 if [[ $DIST = */* ]]; then
37         build_ARCHITECTURE=$(dpkg --print-architecture)
38         [[ ${DIST##*/} = "$build_ARCHITECTURE" ]] || ARCHSUFFIX=-${DIST##*/}
39 fi
40 BASEPATH+=$ARCHSUFFIX${CUSTOM:+-$CUSTOM}/
41 # look into the chroot
42 if [[ -e ${BASEPATH}usr/lib/libeatmydata/libeatmydata.so ]]; then
43         # know old method (26-2)
44         eatmydata=LD_PRELOAD=/usr/lib/libeatmydata/libeatmydata.so
45 else
46         # assume new method (82-3)
47         eatmydata=LD_PRELOAD=libeatmydata.so
48 fi
49 # disable for some distros without either
50 case ${DIST%/*} {
51 (sarge|sarge-*|etch|etch-*)
52         eatmydata=LD_PRELOAD=
53         ;;
54 }
55
56 # construct command line
57 set -A cmd env "DIST=$DIST" LANG=C LC_CTYPE=C LC_NUMERIC=C \
58     LC_TIME=C LC_COLLATE=C LC_MONETARY=C LC_MESSAGES=C LC_PAPER=C \
59     LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=C \
60     LC_IDENTIFICATION=C LC_ALL=C $eatmydata
61 shift
62 while [[ $1 = *=* ]]; do
63         cmd[${#cmd[@]}]=$1
64         shift
65 done
66 setarch_seen=0
67 case $1 {
68 (--)
69         shift
70         ;;
71 (linux32|linux64)
72         cmd[${#cmd[@]}]=$1
73         shift
74         if [[ $1 = --uname-2.6 ]]; then
75                 cmd[${#cmd[@]}]=$1
76                 shift
77         fi
78         setarch_seen=1
79         ;;
80 }
81 #XXX add more architectures
82 (( setarch_seen )) || case $DIST {
83 (*/amd64) cmd[${#cmd[@]}]=linux64 ;;
84 (*/i386) cmd[${#cmd[@]}]=linux32 ;;
85 (*/x32) cmd[${#cmd[@]}]=linux64 ;;
86 (*) setarch_seen=2 ;;
87 }
88 (( setarch_seen )) || case ${DIST%/*} {
89 (sarge|sarge-*) cmd[${#cmd[@]}]=--uname-2.6 ;;
90 (etch|etch-*) cmd[${#cmd[@]}]=--uname-2.6 ;;
91 (lenny|lenny-*) cmd[${#cmd[@]}]=--uname-2.6 ;;
92 (squeeze|squeeze-*) cmd[${#cmd[@]}]=--uname-2.6 ;;
93 (dapper|dapper-*) cmd[${#cmd[@]}]=--uname-2.6 ;;
94 (hardy|hardy-*) cmd[${#cmd[@]}]=--uname-2.6 ;;
95 (jaunty|jaunty-*) cmd[${#cmd[@]}]=--uname-2.6 ;;
96 (karmic|karmic-*) cmd[${#cmd[@]}]=--uname-2.6 ;;
97 (lucid|lucid-*) cmd[${#cmd[@]}]=--uname-2.6 ;;
98 (maverick|maverick-*) cmd[${#cmd[@]}]=--uname-2.6 ;;
99 (natty|natty-*) cmd[${#cmd[@]}]=--uname-2.6 ;;
100 }
101
102 set -x
103 exec sudo "${cmd[@]}" cowbuilder "$@"