next attempt, plus --keepmydata as FIRST(!) arg
[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 # workaround for supporting old and new eatmydata; cf. #765579
33 eatmydata='LD_LIBRARY_PATH=/usr/lib/libeatmydata LD_PRELOAD=libeatmydata.so'
34 # disable for some distros without either
35 case ${DIST%/*} {
36 (sarge|sarge-*|etch|etch-*)
37         eatmydata=LD_PRELOAD=
38         ;;
39 }
40 if [[ $1 = --keepmydata ]]; then
41         eatmydata=LD_PRELOAD=
42         shift
43 fi
44
45 # construct command line
46 set -A cmd env "DIST=$DIST" LANG=C LC_CTYPE=C LC_NUMERIC=C \
47     LC_TIME=C LC_COLLATE=C LC_MONETARY=C LC_MESSAGES=C LC_PAPER=C \
48     LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=C \
49     LC_IDENTIFICATION=C LC_ALL=C $eatmydata
50 shift
51 while [[ $1 = *=* ]]; do
52         cmd[${#cmd[@]}]=$1
53         shift
54 done
55 setarch_seen=0
56 case $1 {
57 (--)
58         shift
59         ;;
60 (linux32|linux64)
61         cmd[${#cmd[@]}]=$1
62         shift
63         if [[ $1 = --uname-2.6 ]]; then
64                 cmd[${#cmd[@]}]=$1
65                 shift
66         fi
67         setarch_seen=1
68         ;;
69 }
70 #XXX add more architectures
71 (( setarch_seen )) || case $DIST {
72 (*/amd64) cmd[${#cmd[@]}]=linux64 ;;
73 (*/i386) cmd[${#cmd[@]}]=linux32 ;;
74 (*/x32) cmd[${#cmd[@]}]=linux64 ;;
75 (*) setarch_seen=2 ;;
76 }
77 (( setarch_seen )) || case ${DIST%/*} {
78 (sarge|sarge-*) cmd[${#cmd[@]}]=--uname-2.6 ;;
79 (etch|etch-*) cmd[${#cmd[@]}]=--uname-2.6 ;;
80 (lenny|lenny-*) cmd[${#cmd[@]}]=--uname-2.6 ;;
81 (squeeze|squeeze-*) cmd[${#cmd[@]}]=--uname-2.6 ;;
82 (dapper|dapper-*) cmd[${#cmd[@]}]=--uname-2.6 ;;
83 (hardy|hardy-*) cmd[${#cmd[@]}]=--uname-2.6 ;;
84 (jaunty|jaunty-*) cmd[${#cmd[@]}]=--uname-2.6 ;;
85 (karmic|karmic-*) cmd[${#cmd[@]}]=--uname-2.6 ;;
86 (lucid|lucid-*) cmd[${#cmd[@]}]=--uname-2.6 ;;
87 (maverick|maverick-*) cmd[${#cmd[@]}]=--uname-2.6 ;;
88 (natty|natty-*) cmd[${#cmd[@]}]=--uname-2.6 ;;
89 }
90
91 set -x
92 exec sudo "${cmd[@]}" cowbuilder "$@"