|
1 | | -#!/bin/bash |
2 | | - |
3 | | -# Color definitions |
4 | | -VERSION="1.1.0" |
5 | | -RED='\033[0;31m' |
6 | | -GREEN='\033[0;32m' |
7 | | -YELLOW='\033[0;33m' |
8 | | -BLUE='\033[0;34m' |
9 | | -NC='\033[0m' # No Color |
10 | | - |
11 | | -# Function to print colored messages with a touch of humor |
12 | | -print_error() { |
13 | | - echo -e "${RED}Oops... $1${NC}" |
14 | | -} |
15 | | - |
16 | | -print_warning() { |
17 | | - echo -e "${YELLOW}Hold on... $1${NC}" |
18 | | -} |
19 | | - |
20 | | -print_success() { |
21 | | - echo -e "${GREEN}Ta-da! $1${NC}" |
22 | | -} |
23 | | - |
24 | | -print_info() { |
25 | | - echo -e "${BLUE}FYI: $1${NC}" |
26 | | -} |
27 | | - |
28 | | -# Function to clone or update PHP version from AUR |
29 | | -clone_or_update_php() { |
30 | | - local version="$1" |
31 | | - local aur_repo="https://aur.archlinux.org/php${version}.git" |
32 | | - local install_dir="$HOME/src/php${version}" |
33 | | - |
34 | | - # Create source directory if it doesn't exist |
35 | | - if [ ! -d "$HOME/src" ]; then |
36 | | - mkdir -p "$HOME/src" || { |
37 | | - print_error "Failed to create directory $HOME/src. Did the gnomes run off?" |
38 | | - exit 1 |
39 | | - } |
40 | | - fi |
41 | | - |
42 | | - # Navigate to the source directory |
43 | | - cd "$HOME/src" || { |
44 | | - print_error "Could not find the path to $HOME/src. Are we lost in the enchanted forest?" |
45 | | - exit 1 |
46 | | - } |
47 | | - |
48 | | - # Check if directory exists and is not empty |
49 | | - if [ -d "$install_dir" ] && [ -n "$(ls -A $install_dir)" ]; then |
50 | | - print_info "Stirring up some magic to update PHP version $version..." |
51 | | - cd "$install_dir" || { |
52 | | - print_error "Lost the path to $install_dir. Maybe a map spell will help?" |
53 | | - exit 1 |
54 | | - } |
55 | | - # Attempt to pull changes from 'master' branch first |
56 | | - if ! git pull origin master; then |
57 | | - # If 'master' branch fails, try 'main' branch |
58 | | - print_info "Trying 'main' branch... Abracadabra!" |
59 | | - if ! git pull origin main; then |
60 | | - print_error "Failed to pull latest changes from $aur_repo. Perhaps a stronger potion is needed?" |
61 | | - exit 1 |
62 | | - fi |
63 | | - fi |
64 | | - else |
65 | | - print_info "Conjuring PHP version $version from $aur_repo... *poof!*" |
66 | | - git clone "$aur_repo" "$install_dir" || { |
67 | | - print_error "Oops, the magic circle was broken! Failed to clone PHP version ${version} from AUR." |
68 | | - exit 1 |
69 | | - } |
70 | | - cd "$install_dir" || { |
71 | | - print_error "Teleportation spell malfunction! Can't find $install_dir." |
72 | | - exit 1 |
73 | | - } |
74 | | - fi |
75 | | -} |
76 | | - |
77 | | -# Function to check if package exists in repositories and install it |
78 | | -install_via_pacman() { |
79 | | - local package="$1" |
80 | | - if pacman -Si "$package" &>/dev/null; then |
81 | | - print_info "Found prebuilt binary for $package! Installing via pacman..." |
82 | | - sudo pacman -S --noconfirm "$package" || return 1 |
83 | | - return 0 |
84 | | - else |
85 | | - return 1 |
86 | | - fi |
87 | | -} |
88 | | - |
89 | | -# Function to install c-client if missing |
90 | | -install_c_client() { |
91 | | - if pacman -Qi c-client &>/dev/null; then |
92 | | - return 0 |
93 | | - fi |
94 | | - |
95 | | - print_info "c-client dependency is missing (required for PHP build)." |
96 | | - print_info "Downloading prebuilt c-client from official repository..." |
97 | | - |
98 | | - local c_client_url="https://github.com/Its-Satyajit/phpv/raw/main/c-client/c-client-2007f-20-x86_64.pkg.tar.zst" |
99 | | - local c_client_file="/tmp/c-client-2007f-20-x86_64.pkg.tar.zst" |
100 | | - |
101 | | - if curl -L -o "$c_client_file" "$c_client_url"; then |
102 | | - print_info "Installing c-client..." |
103 | | - if sudo pacman -U --noconfirm "$c_client_file"; then |
104 | | - print_success "c-client installed successfully!" |
105 | | - return 0 |
106 | | - else |
107 | | - print_error "Failed to install c-client via pacman." |
108 | | - return 1 |
109 | | - fi |
110 | | - else |
111 | | - print_error "Failed to download c-client from GitHub." |
112 | | - return 1 |
113 | | - fi |
114 | | -} |
115 | | - |
116 | | -# Function to build and install PHP version using makepkg |
117 | | -build_and_install_php() { |
118 | | - local version="$1" |
119 | | - |
120 | | - # Try to install via pacman first if --build is not set |
121 | | - if [ "$FORCE_BUILD" != "true" ]; then |
122 | | - if install_via_pacman "php${version}"; then |
123 | | - print_success "Installed PHP ${version} via pacman!" |
124 | | - return 0 |
125 | | - fi |
126 | | - print_info "Prebuilt binary not found. Falling back to AUR build..." |
127 | | - fi |
128 | | - |
129 | | - # Ensure c-client is installed |
130 | | - install_c_client || { |
131 | | - print_error "Failed to handle c-client dependency. Build might fail." |
132 | | - # We don't exit here, let makepkg try/fail so user sees the native error too |
133 | | - } |
134 | | - |
135 | | - print_info "Mixing potions to build and install PHP version $version..." |
136 | | - print_info "This may take a while... Gather 'round, wizards!" |
137 | | - |
138 | | - # Explicitly skipping PGP check can sometimes help with old AUR keys, but standard is secure. |
139 | | - # Using --noconfirm to avoid interactive prompts which break automation. |
140 | | - # Using --cleanbuild to remove the build directory ($srcdir/) before starting (or after packaging with -c), |
141 | | - # ensuring a fresh build environment and preventing errors on re-runs (like prepare() failing). |
142 | | - local makepkg_args="-si --noconfirm --cleanbuild" |
143 | | - if [ "$NO_CHECK" == "true" ]; then |
144 | | - makepkg_args="$makepkg_args --nocheck" |
145 | | - print_warning "Skipping tests as requested..." |
146 | | - fi |
147 | | - |
148 | | - makepkg $makepkg_args || { |
149 | | - print_error "Failed the incantation to build or install PHP version $version. Did someone mispronounce 'makepkg'?" |
150 | | - exit 1 |
151 | | - } |
152 | | -} |
153 | | - |
154 | | - |
155 | | -# Function to create symbolic links for PHP binaries |
156 | | -create_symlinks() { |
157 | | - local version="$1" |
158 | | - |
159 | | - # List of binaries to link |
160 | | - # local binaries=(php php-cgi php-config phpize php-fpm phpdbg pear pecl) |
161 | | - local binaries=(php php-cgi php-config phpize php-fpm phpdbg) |
162 | | - |
163 | | - # Remove existing symbolic links |
164 | | - for bin in "${binaries[@]}"; do |
165 | | - rm -f "${HOME}/bin/${bin}" |
166 | | - done |
167 | | - |
168 | | - # Create new symbolic links for the installed PHP version |
169 | | - for bin in "${binaries[@]}"; do |
170 | | - local bin_path |
171 | | - bin_path=$(command -v "${bin}${version}") |
172 | | - if [ -n "$bin_path" ]; then |
173 | | - ln -s "$bin_path" "${HOME}/bin/${bin}" || { |
174 | | - print_warning "Failed to weave a symbolic link for $bin. Are the threads tangled?" |
175 | | - } |
176 | | - else |
177 | | - print_warning "The rune stone for ${bin}${version} is missing. Check the ancient archives." |
178 | | - fi |
179 | | - done |
180 | | - |
181 | | - # Verify symbolic links creation |
182 | | - if [ $? -eq 0 ]; then |
183 | | - print_success "Hocus pocus! Switched to PHP version ${version}." |
184 | | - else |
185 | | - print_error "The mystical threads snapped! Failed to create symbolic links for PHP version ${version}." |
186 | | - exit 1 |
187 | | - fi |
188 | | -} |
189 | | - |
190 | | -# Function to install PHP extension |
191 | | -install_extension() { |
192 | | - local extension="$1" |
193 | | - local version="$2" |
194 | | - local package_name="php${version}-${extension}" |
195 | | - local aur_repo="https://aur.archlinux.org/${package_name}.git" |
196 | | - local install_dir="$HOME/src/${package_name}" |
197 | | - |
198 | | - print_info "Preparing to install extension: ${extension} for PHP ${version}..." |
199 | | - |
200 | | - # Try to install via pacman first if --build is not set |
201 | | - if [ "$FORCE_BUILD" != "true" ]; then |
202 | | - if install_via_pacman "${package_name}"; then |
203 | | - print_success "Extension ${extension} for PHP ${version} installed via pacman!" |
204 | | - return 0 |
205 | | - fi |
206 | | - print_info "Prebuilt binary not found. Falling back to AUR build..." |
207 | | - fi |
208 | | - |
209 | | - # Create source directory if it doesn't exist |
210 | | - if [ ! -d "$HOME/src" ]; then |
211 | | - mkdir -p "$HOME/src" || { |
212 | | - print_error "Failed to create directory $HOME/src." |
213 | | - exit 1 |
214 | | - } |
215 | | - fi |
216 | | - |
217 | | - # Check if directory exists and is not empty |
218 | | - if [ -d "$install_dir" ] && [ -n "$(ls -A $install_dir)" ]; then |
219 | | - print_info "Updating existing extension source..." |
220 | | - cd "$install_dir" || exit 1 |
221 | | - git pull origin master || git pull origin main || { |
222 | | - print_error "Failed to update extension source." |
223 | | - exit 1 |
224 | | - } |
225 | | - else |
226 | | - print_info "Cloning extension from ${aur_repo}..." |
227 | | - git clone "$aur_repo" "$install_dir" || { |
228 | | - print_error "Failed to clone extension repository. Does it exist in AUR?" |
229 | | - exit 1 |
230 | | - } |
231 | | - cd "$install_dir" || exit 1 |
232 | | - fi |
233 | | - |
234 | | - print_info "Building and installing usage makepkg..." |
235 | | - local makepkg_args="-si --noconfirm --cleanbuild" |
236 | | - if [ "$NO_CHECK" == "true" ]; then |
237 | | - makepkg_args="$makepkg_args --nocheck" |
238 | | - print_warning "Skipping tests as requested..." |
239 | | - fi |
240 | | - |
241 | | - # Manual cleanup to be absolutely sure |
242 | | - print_info "Sweeping away old build artifacts (src/ pkg/)..." |
243 | | - rm -rf src pkg |
244 | | - |
245 | | - makepkg $makepkg_args || { |
246 | | - print_error "Failed to build/install extension ${package_name}." |
247 | | - exit 1 |
248 | | - } |
249 | | - |
250 | | - print_success "Extension ${extension} for PHP ${version} installed successfully!" |
251 | | -} |
252 | | - |
253 | | -# Function to switch PHP version |
254 | | -switch_php_version() { |
255 | | - local version="$1" |
256 | | - |
257 | | - # Check if PHP binary exists for the specified version |
258 | | - if ! command -v "php${version}" &>/dev/null; then |
259 | | - print_error "The crystal ball reveals that PHP version ${version} is not installed." |
260 | | - exit 1 |
261 | | - fi |
262 | | - |
263 | | - # Create symbolic links for the PHP version |
264 | | - create_symlinks "$version" |
265 | | - |
266 | | - # Verify PHP version after switching |
267 | | - print_info "Conjuring spirits to verify PHP version..." |
268 | | - php -v |
269 | | -} |
270 | | - |
271 | | -# Main script logic |
272 | | - |
273 | | -# Main script logic |
274 | | - |
275 | | -FORCE_BUILD="false" |
276 | | -NO_CHECK="false" |
277 | | - |
278 | | -# Check arguments |
279 | | -while [[ $# -gt 0 ]]; do |
280 | | - case $1 in |
281 | | - -i) |
282 | | - action="install_or_update" |
283 | | - version="$2" |
284 | | - shift 2 |
285 | | - ;; |
286 | | - -e) |
287 | | - action="install_extension" |
288 | | - extension="$2" |
289 | | - version="$3" |
290 | | - shift 3 |
291 | | - ;; |
292 | | - -v|--version) |
293 | | - echo "PHPV Version: $VERSION" |
294 | | - exit 0 |
295 | | - ;; |
296 | | - -b|--build) |
297 | | - FORCE_BUILD="true" |
298 | | - shift |
299 | | - ;; |
300 | | - --nocheck) |
301 | | - NO_CHECK="true" |
302 | | - shift |
303 | | - ;; |
304 | | - -*) |
305 | | - print_error "Unknown magic spell: $1" |
306 | | - exit 1 |
307 | | - ;; |
308 | | - *) |
309 | | - if [ -z "$action" ]; then |
310 | | - action="switch" |
311 | | - version="$1" |
312 | | - shift |
313 | | - else |
314 | | - print_error "Too many ingredients in the cauldron!" |
315 | | - exit 1 |
316 | | - fi |
317 | | - ;; |
318 | | - esac |
319 | | -done |
320 | | - |
321 | | -if [ -z "$action" ]; then |
322 | | - print_info "Magic instructions:" |
323 | | - print_info " $0 -i <version> : Install/Update PHP version" |
324 | | - print_info " $0 -i <version> --build : Force build from source (ignore binary)" |
325 | | - print_info " $0 -i <version> --nocheck : Skip tests during build" |
326 | | - print_info " $0 -e <extension> <version> : Install PHP extension (e.g., 'imagick 81')" |
327 | | - print_info " $0 <version> : Switch to PHP version" |
328 | | - print_info " $0 -v : Show version" |
329 | | - exit 1 |
330 | | -fi |
331 | | - |
332 | | - |
333 | | -case "$action" in |
334 | | -install_or_update) |
335 | | - clone_or_update_php "$version" |
336 | | - build_and_install_php "$version" |
337 | | - create_symlinks "$version" |
338 | | - ;; |
339 | | -install_extension) |
340 | | - install_extension "$extension" "$version" |
341 | | - ;; |
342 | | -switch) |
343 | | - switch_php_version "$version" |
344 | | - ;; |
345 | | -*) |
346 | | - print_error "The cauldron is bubbling with unknown commands: $action" |
347 | | - exit 1 |
348 | | - ;; |
349 | | -esac |
| 1 | +1.2.0 |
0 commit comments