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