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