summaryrefslogtreecommitdiff
blob: f3704d5d2f52fd844ea266b617dbfc3b8459ddd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/bin/sh
# Copyright 1999-2011 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/sys-devel/automake-wrapper/files/am-wrapper-6.sh,v 1.1 2011/10/02 19:51:17 vapier Exp $

# Based on the am-wrapper.pl script provided by MandrakeSoft
# Rewritten in bash by Gregorio Guidi
#
# Executes the correct automake version.
#
# - defaults to newest version available (hopefully automake-1.10)
# - runs automake-1.9 if:
#   - envvar WANT_AUTOMAKE is set to `1.9'
#     -or-
#   - `Makefile.in' was generated by automake-1.9
#     -or-
#   - 'aclocal.m4' contain AM_AUTOMAKE_VERSION, specifying the use of 1.9
# - runs automake-1.8 if:
#   - envvar WANT_AUTOMAKE is set to `1.8'
#     -or-
#   - `Makefile.in' was generated by automake-1.8
#     -or-
#   - 'aclocal.m4' contain AM_AUTOMAKE_VERSION, specifying the use of 1.8
# - runs automake-1.7 if:
#   - envvar WANT_AUTOMAKE is set to `1.7'
#     -or-
#   - `Makefile.in' was generated by automake-1.7
#     -or-
#   - 'aclocal.m4' contain AM_AUTOMAKE_VERSION, specifying the use of 1.7
# - runs automake-1.6 if:
#   - envvar WANT_AUTOMAKE is set to `1.6'
#     -or-
#   - `Makefile.in'
#     -or-
#   - 'aclocal.m4' contain AM_AUTOMAKE_VERSION, specifying the use of 1.6
# - runs automake-1.5 if:
#   - envvar WANT_AUTOMAKE is set to `1.5'
#     -or-
#   - `Makefile.in' was generated by automake-1.5
#     -or-
#   - 'aclocal.m4' contain AM_AUTOMAKE_VERSION, specifying the use of 1.5
# - runs automake-1.4 if:
#   - envvar WANT_AUTOMAKE is set to `1.4'
#     -or-
#   - `Makefile.in' was generated by automake-1.4
#     -or-
#   - 'aclocal.m4' contain AM_AUTOMAKE_VERSION, specifying the use of 1.4

warn() { printf 'am-wrapper: %s: %b\n' "${argv0}" "$*" 1>&2; }
err() { warn "$@"; exit 1; }
unset IFS
which() {
	local p
	IFS=: # we don't use IFS anywhere, so don't bother saving/restoring
	for p in ${PATH} ; do
		p="${p}/$1"
		[ -e "${p}" ] && echo "${p}" && return 0
	done
	unset IFS
	return 1
}

#
# Sanitize argv[0] since it isn't always a full path #385201
#
argv0=${0##*/}
case ${0} in
	${argv0})
		# find it in PATH
		if ! full_argv0=$(which "${argv0}") ; then
			err "could not locate ${argv0}; file a bug"
		fi
		;;
	*)
		# re-use full/relative paths
		full_argv0=$0
		;;
esac

if [ "${argv0}" = "am-wrapper.sh" ] ; then
	err "Don't call this script directly"
fi

vers="1.11 1.10 1.9 1.8 1.7 1.6 1.5 1.4"

#
# Export the proper variable/versions and try to locate a usuable
# default (newer versions are preferred)
#
binary=""
for v in ${vers} ; do
	if [ -z "${binary}" ] && [ -x "${full_argv0}-${v}" ] ; then
		binary="${full_argv0}-${v}"
		break
	fi
done
if [ -z "${binary}" ] ; then
	err "Unable to locate any usuable version of automake.\n" \
	    "\tI tried these versions: ${vers}\n" \
	    "\tWith a base name of '${full_argv0}'."
fi

#
# Check the WANT_AUTOMAKE setting.  We accept a whitespace delimited
# list of automake versions.
#
if [ -n "${WANT_AUTOMAKE}" ] ; then
	for v in ${vers} x ; do
		if [ "${v}" = "x" ] ; then
			warn "warning: invalid WANT_AUTOMAKE '${WANT_AUTOMAKE}'; ignoring."
			unset WANT_AUTOMAKE
			break
		fi

		for wx in ${WANT_AUTOMAKE} ; do
			if [ "${wx}" = "${v}" ] ; then
				binary="${full_argv0}-${v}"
				v="x"
			fi
		done
		[ "${v}" = "x" ] && break
	done
fi

#
# autodetect helpers
#
do_awk() {
	local file=$1 ; shift
	local arg=$1 ; shift
	echo $(gawk "{ if (match(\$0, \"$*\", res)) { print res[${arg}]; exit } }" ${file})
}

#
# autodetect routine
#
if [ -z "${WANT_AUTOMAKE}" ] ; then
	if [ -r "Makefile.in" ] ; then
		confversion_mf=$(do_awk Makefile.in 2 "^# Makefile.in generated (automatically )?by automake ([0-9].[0-9]+)")
	fi
	if [ -r "aclocal.m4" ] ; then
		confversion_ac=$(do_awk aclocal.m4 1 'generated automatically by aclocal ([0-9].[0-9]+)')
		confversion_am=$(do_awk aclocal.m4 1 '[[:space:]]*\\[?AM_AUTOMAKE_VERSION\\(\\[?([0-9].[0-9]+)[^)]*\\]?\\)')
	fi

	for v in ${vers} ; do
		if [ "${confversion_mf}" = "${v}" ] || \
		   [ "${confversion_ac}" = "${v}" ] || \
		   [ "${confversion_am}" = "${v}" ]
		then
			binary="${full_argv0}-${v}"
			break
		fi
	done
fi

if [ "${WANT_AMWRAPPER_DEBUG}" ] ; then
	if [ "${WANT_AUTOMAKE}" ] ; then
		warn "DEBUG: WANT_AUTOMAKE is set to ${WANT_AUTOMAKE}"
	fi
	warn "DEBUG: will execute <$binary>"
fi

#
# for further consistency
#
for v in ${vers} ; do
	if [ "${binary}" = "${full_argv0}-${v}" ] ; then
		export WANT_AUTOMAKE="${v}"
		break
	fi
done

#
# Now try to run the binary
#
if [ ! -x "${binary}" ] ; then
	err "$binary is missing or not executable.\n" \
	    "\tPlease try emerging the correct version of automake."
fi

exec "$binary" "$@"

err "was unable to exec $binary !?"