Emulate autoconf a bit more
[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 [ -e /etc/arch-release -a -d /etc/rc.d ]; then
142         echo "Overriding service status check for Arch Linux"
143         RCDIR=/etc/rc.d
144         RESTARTCMD="[ -e /var/run/daemons/\1 ] \&\& /etc/rc.d/\1 restart"
145         echo "yes"
146 fi
147
148 if [ -z "$RESTARTCMD" ]; then
149         printf "Checking for OpenRC ... "
150         if [ -x /sbin/rc-service ]; then
151                 RESTARTCMD="if /sbin/rc-service -e \1; then /sbin/rc-service \1 -- -Ds restart; fi"
152                 echo "yes"
153         else
154                 echo "no"
155         fi
156 fi
157 if [ -z "$RESTARTCMD" ]; then
158         printf "Checking for invoke-rc.d ... "
159         if [ -x /usr/sbin/invoke-rc.d ]; then
160                 RCDIR=/etc/init.d
161                 RESTARTCMD="if /usr/sbin/invoke-rc.d --quiet \1 status >/dev/null 2>\&1; then /usr/sbin/invoke-rc.d \1 restart; fi"
162                 echo "yes"
163         else
164                 echo "no"
165         fi
166 fi
167 if [ -z "$RESTARTCMD" ]; then
168         printf "Checking for service ... "
169         if [ -x /sbin/service ]; then
170                 RCDIR=/etc/init.d
171                 RESTARTCMD="if /sbin/service \1; then /sbin/service \1 restart; fi"
172                 echo "yes"
173         else
174                 echo "no"
175         fi
176 fi
177 if [ -z "$RESTARTCMD" ]; then
178         for x in /etc/init.d/rc.d /etc/rc.d /etc/init.d; do
179                 printf "Checking for $x ... "
180                 if [ -d $x ]; then
181                         RCDIR=$x
182                         RESTARTCMD="if $x/\1 status >/dev/null 2>\&1; then $x/\1 restart; fi"
183                         echo "yes"
184                         break
185                 else
186                         echo "no"
187                 fi
188         done
189 fi
190
191 if [ -z "$RESTARTCMD" ]; then
192         echo "$0: WARNING: No means of interacting with system services detected!"
193         exit 1
194 fi
195
196 echo "RCDIR=            $RCDIR" >>$CONFIG_MK
197 # Work around bug in the dash shell as "echo 'foo \1'" does bad things
198 printf "%s\n" "RESTARTCMD=      $RESTARTCMD" >>$CONFIG_MK
199
200 echo
201 echo "   SYSCONFDIR =           $SYSCONFDIR"
202 echo "   SBINDIR =              $SBINDIR"
203 echo "   LIBEXECDIR =           $LIBEXECDIR"
204 echo "   VARDIR =               $RUNDIR"
205 echo "   MANDIR =               $MANDIR"
206 echo