#!/bin/bash
### Copyright 1999-2017. Parallels IP Holdings GmbH. All Rights Reserved.
#

#
# Plesk script
#


### Copyright 1999-2017. Parallels IP Holdings GmbH. All Rights Reserved.


# vim:ft=sh:
#!/bin/bash
### Copyright 1999-2017. Parallels IP Holdings GmbH. All Rights Reserved.

extension_installed()
{
	test -f "/opt/psa/admin/plib/modules/$1/meta.xml"
}

register_extension()
{
	local meta=$(mktemp)
	local ext="$1"

	cat <<-EOT > $meta
	<?xml version="1.0" encoding="utf-8"?>
	<module>
		<id>$ext</id>
		<name>$ext stub module</name>
		<description>This is stub for installation of $ext on Virtuozzo</description>
		<version>0.0.0</version>
		<release>0</release>
		<vendor>Plesk</vendor>
		<url>http://plesk.com</url>
		<plesk_min_version>17.0.16</plesk_min_version>
	</module>
	EOT
	mkdir -p "/opt/psa/admin/plib/modules/$ext/"
	mv -n "$meta" "/opt/psa/admin/plib/modules/$ext/meta.xml"
	chmod 0644 "/opt/psa/admin/plib/modules/$ext/meta.xml"
	/opt/psa/bin/extension --register "$ext"
}

install_extensions()
{
	ext_file="/var/install-plesk-vz-ext"
	[ -f "$ext_file" ] || return 0

	sort -u < "$ext_file" | while read e;do
		if extension_installed "$e"; then
			echo "Extension $e is already installed"
			continue
		fi
		register_extension "$e"
		/opt/psa/bin/extension --upgrade "$e"
	done

	rm -f "$ext_file"
}

install_extensions
