#!/bin/bash
NEW_VERSION=$1
PHP_VERSIONS=("php@5.6" "php@7.0" "php@7.1" "php@7.2" "php@7.3" "php@7.4")

# Make sure we have a new version to be set.
if [ -z "${NEW_VERSION}" ]; then
	echo "Please provide the new php version as the first argument"
	exit 128
fi

# Make sure the version is formatted like "php@7.4"
NEW_VERSION=$( echo $NEW_VERSION | awk '{print tolower($0)}')
NEW_VERSION=${NEW_VERSION#"php@"}
NEW_VERSION=php@${NEW_VERSION}

LAST_VERSION=($(printf '%s\n' "${PHP_VERSIONS[@]}" | tail -r))

# Return an error message if an unsupported version has been chosen
if [[ ! " ${PHP_VERSIONS[@]} " =~ " ${NEW_VERSION} " ]]; then
	echo "Only the following php versions are supported by Valet: "$(printf "%s " "${PHP_VERSIONS[@]}")
	exit 128
fi

# When the last version has been chosen use "php" as version since this is the latest version in brew
if [[  " ${NEW_VERSION} " =~ " ${LAST_VERSION} " ]]; then
	NEW_VERSION="php"
fi

PHP_VERSIONS=("${PHP_VERSIONS[@]}" "php")

# Stop and unlink all php versions
echo "Stopping all php services"
for PHP_VERSION in "${PHP_VERSIONS[@]}"
do :
	sudo brew services stop $PHP_VERSION 2>/dev/null
	brew unlink --force $PHP_VERSION &>/dev/null
done

# Install new version if not installed or link new version.
if ! brew ls --versions $NEW_VERSION > /dev/null; then
	brew install $NEW_VERSION
else
	# Link new version and install valet.
	brew link --force --overwrite $NEW_VERSION
fi

# Reload valet with new php version
valet install
