update from latest MirBSD CVS
[shellsnippets/shellsnippets.git] / mksh / debian-dev / c
1 #!/bin/mksh
2 #-
3 # Copyright © 2013, 2014, 2018
4 #       mirabilos <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 # c bp:stretch/i386 --debbuildopts -v1.0-1 foo*.dsc
28
29 unset LANGUAGE
30 export LC_ALL=C
31
32 if [[ -z $1 ]]; then
33         print -u2 E: No operation specified
34         exit 1
35 fi
36 REALDIST=$1
37 DIST=${REALDIST#*:}
38 shift
39
40 # workaround for supporting old and new eatmydata; cf. #765579
41 eatmydata='LD_LIBRARY_PATH=/usr/lib/libeatmydata LD_PRELOAD=libeatmydata.so'
42 # disable for some distros without either
43 case ${DIST%/*} {
44 (sarge|sarge-*|etch|etch-*)
45         eatmydata=LD_PRELOAD=
46         ;;
47 }
48 if [[ $1 = --keepmydata ]]; then
49         eatmydata=LD_PRELOAD=
50         shift
51 fi
52
53 # construct command line
54 set -A cmd env "DIST=$REALDIST" LANG=C LC_CTYPE=C LC_NUMERIC=C \
55     LC_TIME=C LC_COLLATE=C LC_MONETARY=C LC_MESSAGES=C LC_PAPER=C \
56     LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=C \
57     LC_IDENTIFICATION=C LC_ALL=C $eatmydata
58 while [[ $1 = *=* ]]; do
59         cmd[${#cmd[@]}]=$1
60         shift
61 done
62 setarch_seen=0
63 case $1 {
64 (--)
65         shift
66         ;;
67 (linux32|linux64)
68         cmd[${#cmd[@]}]=$1
69         shift
70         if [[ $1 = --uname-2.6 ]]; then
71                 cmd[${#cmd[@]}]=$1
72                 shift
73         fi
74         setarch_seen=1
75         ;;
76 }
77 #XXX add more architectures
78 case $DIST {
79 (*/amd64)       setarch_cmd=linux64 ;;
80 (*/i386)        setarch_cmd=linux32 ;;
81 (*/x32)         setarch_cmd=linux64 ;;
82 (*/*)           setarch_cmd= ;;
83 (*)             setarch_cmd=- ;;
84 }
85 case ${DIST%/*} {
86 (sarge|sarge-*)         setarch_26=1 ;;
87 (etch|etch-*)           setarch_26=1 ;;
88 (lenny|lenny-*)         setarch_26=1 ;;
89 (squeeze|squeeze-*)     setarch_26=1 ;;
90 (dapper|dapper-*)       setarch_26=1 ;;
91 (hardy|hardy-*)         setarch_26=1 ;;
92 (jaunty|jaunty-*)       setarch_26=1 ;;
93 (karmic|karmic-*)       setarch_26=1 ;;
94 (lucid|lucid-*)         setarch_26=1 ;;
95 (maverick|maverick-*)   setarch_26=1 ;;
96 (natty|natty-*)         setarch_26=1 ;;
97 (*)                     setarch_26=0 ;;
98 }
99 (( setarch_seen )) || case x$setarch_cmd {
100 (x)
101         print -ru2 "W: no setarch for architecture part of '$REALDIST'!"
102         ;;
103 (x-)
104         if (( setarch_26 )); then
105                 cmd[${#cmd[@]}]=setarch
106                 cmd[${#cmd[@]}]=uname26
107                 cmd[${#cmd[@]}]=--
108         fi
109         ;;
110 (*)
111         cmd[${#cmd[@]}]=$setarch_cmd
112         (( setarch_26 )) && cmd[${#cmd[@]}]=--uname-2.6
113         cmd[${#cmd[@]}]=--
114         ;;
115 }
116
117 unset TMPDIR
118 set -x
119 exec sudo "${cmd[@]}" cowbuilder "$@"