summaryrefslogtreecommitdiff
blob: 7b5f2771e6d41b9843eaaed07ebf4454e87f6f34 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/bin/bash
#
# /usr/sbin/php-select
#		Tool for managing PHP symlinks on Gentoo Linux
#
# Author	Stuart Herbert
#		(stuart@gentoo.org)
#
# Copyright	(c) 2005 Gentoo Foundation, Inc.
#		Released under v2 of the GNU General Public License
#
# ========================================================================
#
# USAGE
# -----
#
# php-select <component> [<php-version>]
#
# where
#    <component> is one of
#      php|php-cgi|php-devel|apache|apache2
#
#    <php-version> is the directory under /usr/lib where PHP is installed
#
# php-select with no arguments will list all components for each installed
# php-version
#
# php-select <component> will list the current php-version in use for that
# component
#
# php-select <component> <php-version> will reconfigure Gentoo to use 
# <php-version> of <component>.  This normally involves replacing symlinks
# on the system

# ========================================================================

G_ACTION=
G_COMPONENT=
G_COPYRIGHT[0]="Copyright (c) 2005 Gentoo Foundation, Inc."
G_COPYRIGHT[1]="Released under v2 of the GNU General Public License"
G_EXITSTATUS=0
G_LIBDIR[0]="/usr/lib64"
G_LIBDIR[1]="/usr/lib32"
G_LIBDIR[2]="/usr/lib"
G_MODULE_PATH="/usr/share/php-select"
G_NEED_COMPONENT=0
G_NEED_PHPVERSION=0
G_PN="$0"
G_PN_BASE="`basename $G_PN`"
G_PVR="1.0.1"
G_PHPVERSION=

G_STATUS_OK=0
G_STATUS_USAGE=1
G_STATUS_NOTINSTALLED=2
G_STATUS_NOTCOMPATIBLE=3
G_STATUS_USINGOTHER=4
G_STATUS_USINGNONE=5

# ========================================================================
# The Main Actions
# ========================================================================

# ------------------------------------------------------------------------
# doCopyright() - output the copyright info
#
# No arguments

doCopyright ()
{
	local i

	for (( i = 0 ; i < ${#G_COPYRIGHT[@]} ; i = i + 1 )) ; do
		echo ${G_COPYRIGHT[$i]}
	done
}

# ------------------------------------------------------------------------
# doHelp() - show full help
#
# No arguments

doHelp ()
{
	doVerboseVersion
	echo
	
	doUsageShort
	echo
	echo "    $G_PN_BASE <component>"
	echo "        Show which version of PHP is used for <component>"
	echo
	echo "    $G_PN_BASE -t <component> <php-version>"
	echo "        Test if <component> uses <php-version> or not"
	echo
	echo "    $G_PN_BASE <component> <php-version>"
	echo "        Set which version of PHP is used for <component>"
	echo
	echo "    $G_PN_BASE -v"
	echo "        Show version number"
	echo
	echo "    $G_PN_BASE -h"
	echo "        Show this help"
	echo
	echo "where <component> is one of:"
	echo
	echo "    apache1   - Apache webserver v1.x"
	echo "    apache2   - Apache webserver v2.x"
	echo "    php       - /usr/bin/php (the CLI SAPI)"
	echo "    php-cgi   - /usr/bin/php-cgi (the CGI SAPI)"
	echo "    php-devel - the scripts used to build PECL extensions"
	echo
	echo "and where <php-version> is one of:"
	echo
	echo "    php5      - php 5.x"
	echo
	echo "NOTE: none of these settings affect how Portage installs PHP or PEAR"
	echo "      packages or PECL extensions."
	echo
	echo "Exit status is one of:"
	echo
	echo "  0 == ok"
	echo "  1 == usage error"
	echo "  2 == no compatible PHP version found"
	echo "  3 == incompatible PHP version specified"
	echo "  4 == component uses a different PHP version"
	echo "  5 == component not configured to use PHP"

	echo
	# doManPageDetails
}

# ------------------------------------------------------------------------
# doManPageDetails() - show man page details
#
# No arguments

doManPageDetails ()
{
	echo "See 'man $G_PN_BASE' for full details"
}

# ------------------------------------------------------------------------
# doSetComponent() - set a component to use a particular php version
#
# $1: component to set
# $2: php version to set the component to

doSetComponent ()
{
	loadComponent $1 || exit 1
	actionSet $2
}

# ------------------------------------------------------------------------
# doShowComponent() - show which version of php a particular component is
# currently using
#
# $1: component to show

doShowComponent ()
{
	loadComponent $1 || exit 1
	actionShow
}

# ------------------------------------------------------------------------
# doTest() - check to see if a particular component is using a specified
# version or not
#
# $1: component to test

doTest ()
{
	loadComponent $1 || exit 1
	actionTest
}

# ------------------------------------------------------------------------
# doUsage() - tell the user to stop being a muppet :)
#
# No arguments

doUsage ()
{
	doUsageShort
	echo "use $G_PN_BASE --help for full details"
	G_EXITSTATUS=1
}

# ------------------------------------------------------------------------
# doUsageShort() - print out just the line to tell the user what to do
#
# No arguments

doUsageShort ()
{
	echo "usage: $G_PN_BASE [-htv] [<component> [<php-version>] ]"
}

# ------------------------------------------------------------------------
# doVerboseVersion() - show more version info
#
# No arguments

doVerboseVersion ()
{
	doVersion
	doCopyright
}

# ------------------------------------------------------------------------
# doVersion() - show version info
#
# No arguments

doVersion ()
{
	echo "$G_PN_BASE v$G_PVR"
}

# ========================================================================
# Helping Functions
# ========================================================================

# ------------------------------------------------------------------------
# loadComponent() - load a component's code into the script
#
# $1: component to load

loadComponent ()
{
	# only load one component, and only do it once

	if [[ -n $G_COMPONENT_LOADED ]] ; then
		return
	fi

	local l_module="$G_MODULE_PATH/$1.sh"

	if [[ ! -f $l_module ]] ; then
		echo "Error: module $l_module not found"
		return 1
	fi

	if [[ ! -r $l_module ]] ; then
		echo "Error: module $l_module not readable"
		return 1
	fi

	. "$l_module" && G_COMPONENT_LOADED=1
	[[ $? != 0 ]] && echo "Error: component $l_module not loadable"

	return 0
}

# ------------------------------------------------------------------------
# missingComponent() - check to see if the user did not specify which
# component to work on
#
# No arguments

missingComponent ()
{
	if [[ -z $G_COMPONENT && $G_NEED_COMPONENT == 1 ]] ; then
		echo "*** error: missing <component> argument"
		return 0
	fi

	return 1
}

# ------------------------------------------------------------------------
# missingPhpVersion() - check to see if the user did not specify which
# PHP version to use
#
# No arguments

missingPhpVersion ()
{
	if [[ -z $G_PHPVERSION && $G_NEED_PHPVERSION == 1 ]] ; then
		echo "*** error: missing <php-version> argument"
		return 0
	fi

	return 1
}

# ------------------------------------------------------------------------
# parseArgs() - work out what the user has asked us to do
#
# $1: a php component (optional)
# $2: a php version (optional)

parseArgs ()
{
	# do we have any parameters at all?

	if [[ -z $1 ]]; then
		G_ACTION=doUsage
		return
	fi

	# handle switches

	while [[ -n $1 && ${1:0:1} == '-' ]]; do
		case "$1" in
			-v|--version)
				G_ACTION=doVersion
				return
				;;
			-V)
				G_ACTION=doVerboseVersion
				return
				;;
			-h|-\?|--help)
				G_ACTION=doHelp
				return
				;;
			-t|--test)
				G_ACTION=doTest
				G_NEED_COMPONENT=1
				G_NEED_PHPVERSION=1
				;;
			*)
				G_ACTION=doUsage
				return
				;;
		esac

		shift
	done

	# if we get here, we have a non-switch to examine, or we
	# don't have anything at all to look at

	if [[ -z $1 ]]; then
		if missingComponent || missingPhpVersion ; then
			G_ACTION=doUsage
		fi
		return
	fi
	
	# we have a component - make a note of it

	G_COMPONENT="$1"
	shift

	# do we have a php version too?

	if [[ -z $1 ]]; then
		if missingComponent || missingPhpVersion ; then
			G_ACTION=doUsage
		fi

		[[ -z $G_ACTION ]] && G_ACTION=doShowComponent
		return
	fi

	# yes we do - make a note of it too

	G_PHPVERSION="$1"
	[[ -z $G_ACTION ]] && G_ACTION=doSetComponent
	shift

	# we're done for now
}

# ------------------------------------------------------------------------
# main() - it all happens here

main ()
{
	parseArgs "$@"
	$G_ACTION $G_COMPONENT $G_PHPVERSION

	exit $G_EXITSTATUS
}

main "$@"