#!/bin/bash
versions=( 56 71 72 73 74 80 81)

stop_fpm() {
		for v in "${versions[@]}"
		do
			msg="${v:-default}"
			[[ "$(systemctl --user is-active "php@${v}.service")" == active ]] && systemctl --user stop "php@${v}" && echo "Stop php fpm ($msg)"
		done
}

start() {
	[[ "$(systemctl --user is-active nginx.service)" != active ]] && echo Start nginx && systemctl --user start nginx.service
	msg="${1:-default}"
	systemctl --user restart "php@$1.service" && echo "Start php fpm ($msg)"
}

restart() {
	stop_fpm
	start "${1:-74}"
}

case $1 in
	start|default|80|php80)
		restart 80
		;;
	stop)
		systemctl --user stop nginx && echo Stop nginx
		stop_fpm
		;;
	php71|71)
		restart 71
		;;
	php72|72)
		restart 72
		;;
	php73|73)
		restart 73
		;;
	php74|74)
		restart 74
		;;
	php81|81)
		restart 81
		;;
	php56|56)
		restart 56
		;;
	dev|snapshot|-snapshot)
		restart -snapshot
		;;
	*)
		for v in "${versions[@]}"
		do
			msg="${v:-default}"
			[[ "$(systemctl --user is-active "php@${v}")" == active ]] && echo "($msg)" && exit
		done

		restart
	
		;;
esac

