Ensure that creating interface resolv.conf files is an atomic operation.
[openresolv] / configure
1 #!/bin/sh
2 # Try and be like autotools configure, but without autotools
3
4 # Ensure that we do not inherit these from env
5 OS=
6 BUILD=
7 HOST=
8 TARGET=
9 RESTARTCMD=
10 RCDIR=
11
12 for x do
13         opt=${x%%=*}
14         var=${x#*=}
15         case "$opt" in
16         --os|OS) OS=$var;;
17         --with-cc|CC) CC=$var;;
18         --debug) DEBUG=$var;;
19         --disable-debug) DEBUG=no;;
20         --enable-debug) DEBUG=yes;;
21         --prefix) PREFIX=$var;;
22         --sysconfdir) SYSCONFDIR=$var;;
23         --bindir|--sbindir) SBINDIR=$var;;
24         --libexecdir) LIBEXECDIR=$var;;
25         --statedir|--localstatedir) STATEDIR=$var;;
26         --dbdir) DBDIR=$var;;
27         --rundir) RUNDIR=$var;;
28         --mandir) MANDIR=$var;;
29         --with-ccopts|CFLAGS) CFLAGS=$var;;
30         CPPFLAGS) CPPFLAGS=$var;;
31         --build) BUILD=$var;;
32         --host) HOST=$var;;
33         --target) TARGET=$var;;
34         --libdir) LIBDIR=$var;;
35         --restartcmd) RESTARTCMD=$var;;
36         --includedir) eval INCLUDEDIR="$INCLUDEDIR${INCLUDEDIR:+ }$var";;
37         --datadir|--infodir) ;; # ignore autotools
38         --disable-maintainer-mode|--disable-dependency-tracking) ;;
39         --help) echo "See the README file for available options"; exit 0;;
40         *) echo "$0: WARNING: unknown option $opt" >&2;;
41         esac
42 done
43
44 if [ -z "$LIBEXECDIR" ]; then
45         printf "Checking for directory /libexec ... "
46         if [ -d /libexec ]; then
47                 echo "yes"
48                 LIBEXECDIR=$PREFIX/libexec/resolvconf
49         else
50                 echo "no"
51                 LIBEXECDIR=$PREFIX/lib/resolvconf
52         fi
53 fi
54 if [ -z "$RUNDIR" ]; then
55         printf "Checking for directory /run ... "
56         if [ -d /run ]; then
57                 echo "yes"
58                 RUNDIR=/run
59         else
60                 echo "no"
61                 RUNDIR=/var/run
62         fi
63 fi      
64
65 : ${SED:=sed}
66
67 : ${SYSCONFDIR:=$PREFIX/etc}
68 : ${SBINDIR:=$PREFIX/sbin}
69 : ${LIBEXECDIR:=$PREFIX/libexec/resolvconf}
70 : ${STATEDIR:=/var}
71 : ${RUNDIR:=$STATEDIR/run}
72 : ${MANDIR:=${PREFIX:-/usr}/share/man}
73
74 eval SYSCONFDIR="$SYSCONFDIR"
75 eval SBINDIR="$SBINDIR"
76 eval LIBEXECDIR="$LIBEXECDIR"
77 eval VARDIR="$RUNDIR/resolvconf"
78 eval MANDIR="$MANDIR"
79
80 CONFIG_MK=config.mk
81
82 if [ -z "$BUILD" ]; then
83         BUILD=`uname -m`-`uname -s | tr '[:upper:]' '[:lower:]'`
84 fi
85 if [ -z "$HOST" ]; then
86         [ -z "$TARGET" ] && TARGET=$BUILD
87         HOST=$TARGET
88 fi
89 if [ -z "$TARGET" ]; then
90         [ -z "$HOST" ] && HOST=$BUILD
91         TARGET=$HOST
92 fi
93
94 # Debian and Slackware have linux in different places when dealing with
95 # autoconf, so we deal with that here.
96 if [ -z "$OS" ]; then
97         case "$TARGET" in
98         *-linux-*|linux-*|*-linux|linux) OS=linux;;
99         esac
100 fi
101
102 if [ -z "$OS" ]; then
103         # Derive OS from cpu-manufacturer-os-kernel
104         CPU=${TARGET%%-*}
105         REST=${TARGET#*-}
106         if [ "$CPU" != "$REST" ]; then
107                 MANU=${REST%%-*}
108                 REST=${REST#*-}
109                 if [ "$MANU" != "$REST" ]; then
110                         OS=${REST%%-*}
111                         REST=${REST#*-}
112                         if [ "$OS" != "$REST" ]; then
113                                 KERNEL=${REST%%-*}
114                         else
115                                 # 3 tupple
116                                 KERNEL=$OS
117                                 OS=$MANU
118                                 MANU=
119                         fi
120                 else
121                         # 2 tupple
122                         OS=$MANU
123                         MANU=
124                 fi
125         fi
126 fi
127
128 echo "Configuring openresolv for ... $OS"
129 rm -rf $CONFIG_MK
130 echo "# $OS" >$CONFIG_MK
131
132 for x in SYSCONFDIR SBINDIR LIBEXECDIR VARDIR MANDIR; do
133         eval v=\$$x
134         # Make files look nice for import
135         l=$((10 - ${#x}))
136         unset t
137         [ $l -gt 3 ] && t="     "
138         echo "$x=$t     $v" >>$CONFIG_MK
139 done
140
141 if [ -z "$RESTARTCMD" ]; then
142         printf "Checking for systemd ... "
143         if [ -x /bin/systemctl ]; then
144                 RESTARTCMD="/bin/systemctl try-restart \1"
145                 echo "yes"
146         elif [ -x /usr/bin/systemctl ]; then
147                 RESTARTCMD="/usr/bin/systemctl try-restart \1"
148                 echo "yes"
149         else
150                 echo "no"
151         fi
152 fi
153
154 # Arch upgraded to systemd, so this check has to be just after systemd
155 # but higher than the others
156 if [ -z "$RESTARTCMD" ]; then
157         printf "Checking for Arch ... "
158         if [ -e /etc/arch-release -a -d /etc/rc.d ]; then
159                 RCDIR=/etc/rc.d
160                 RESTARTCMD="[ -e /var/run/daemons/\1 ] \&\& /etc/rc.d/\1 restart"
161                 echo "yes"
162         else
163                 echo "no"
164         fi
165 fi
166
167 if [ -z "$RESTARTCMD" ]; then
168         printf "Checking for OpenRC ... "
169         if [ -x /sbin/rc-service ]; then
170                 RESTARTCMD="if /sbin/rc-service -e \1; then /sbin/rc-service \1 -- -Ds restart; fi"
171                 echo "yes"
172         else
173                 echo "no"
174         fi
175 fi
176 if [ -z "$RESTARTCMD" ]; then
177         printf "Checking for invoke-rc.d ... "
178         if [ -x /usr/sbin/invoke-rc.d ]; then
179                 RCDIR=/etc/init.d
180                 RESTARTCMD="if /usr/sbin/invoke-rc.d --quiet \1 status >/dev/null 2>\&1; then /usr/sbin/invoke-rc.d \1 restart; fi"
181                 echo "yes"
182         else
183                 echo "no"
184         fi
185 fi
186 if [ -z "$RESTARTCMD" ]; then
187         printf "Checking for service ... "
188         if [ -x /sbin/service ]; then
189                 RCDIR=/etc/init.d
190                 RESTARTCMD="if /sbin/service \1; then /sbin/service \1 restart; fi"
191                 echo "yes"
192         else
193                 echo "no"
194         fi
195 fi
196 if [ -z "$RESTARTCMD" ]; then
197         for x in /etc/init.d/rc.d /etc/rc.d /etc/init.d; do
198                 printf "Checking for $x ... "
199                 if [ -d $x ]; then
200                         RCDIR=$x
201                         RESTARTCMD="if $x/\1 status >/dev/null 2>\&1; then $x/\1 restart; fi"
202                         echo "yes"
203                         break
204                 else
205                         echo "no"
206                 fi
207         done
208 fi
209
210 if [ -z "$RESTARTCMD" ]; then
211         echo "$0: WARNING: No means of interacting with system services detected!"
212         exit 1
213 fi
214
215 echo "RCDIR=            $RCDIR" >>$CONFIG_MK
216 # Work around bug in the dash shell as "echo 'foo \1'" does bad things
217 printf "%s\n" "RESTARTCMD=      $RESTARTCMD" >>$CONFIG_MK
218
219 echo
220 echo "   SYSCONFDIR =           $SYSCONFDIR"
221 echo "   SBINDIR =              $SBINDIR"
222 echo "   LIBEXECDIR =           $LIBEXECDIR"
223 echo "   VARDIR =               $RUNDIR"
224 echo "   MANDIR =               $MANDIR"
225 echo