-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphpswitcher
executable file
·59 lines (51 loc) · 1.66 KB
/
phpswitcher
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
#!/bin/sh
# @author Thomas Baier <thomas.baier.halle@gmail.com>
# @license MIT
. "${0%/*}"/.lib
# check for available PHP binaries
if ! PHP_ALTERNATIVES="$(sudo update-alternatives --list php --quiet)"; then
error ${ERROR_NO_BINARIES}
fi
# bind current version info
current_php_version_name=$(php -r 'echo PHP_VERSION;')
current_php_version_id=$(php -r 'echo PHP_VERSION_ID;')
# display current version
info ${INFO_CURRENT_VERSION} ${current_php_version_name}
# check for alternative binaries
case ${PHP_ALTERNATIVES} in
*[[:space:]]*)
break ;;
*) error ${ERROR_NO_ALTERNATIVE_BINARIES} ;;
esac
CHOICES_COUNT=$(echo "$PHP_ALTERNATIVES" | wc -w)
if [ "$CHOICES_COUNT" -lt 2 ]; then
error ${ERROR_NO_ALTERNATIVE_BINARIES}
fi
# check for current version in selection
for available_phpversion in ${PHP_ALTERNATIVES}; do
available_phpversion_id=$(${available_phpversion} -r 'echo PHP_VERSION_ID;')
if [ "$available_phpversion_id" = "$current_php_version_id" ]; then
current_match=${available_phpversion}
CHOICES_COUNT=$((CHOICES_COUNT-1))
fi
done
# remove current from selection if matched
if [ -n "$current_match" ]; then
PHP_ALTERNATIVES=$(echo ${PHP_ALTERNATIVES} | sed "s#$current_match##g")
fi
# begin interaction
selection=
until [ "$selection" = "0" ]; do
label_counter=1
# print choices
for choice in ${PHP_ALTERNATIVES}; do
echo "${label_counter}) ${choice}";
label_counter=$((label_counter+1))
done
# start interaction
printf "${COLOR_ORANGE}${INFO_CHOICE_LABEL}${COLOR_END}\n"
read selection
case ${selection} in
* ) validate_choice ${selection}; break ;;
esac
done