#!/bin/sh
# Copyright (c) 2000-2014 Synology Inc. All rights reserved.

PKG_DIR="Joomla"
PKG_VERSION=`/bin/get_key_value /var/packages/Joomla/INFO version | cut -d- -f2`
tmp_path=/$(readlink /var/services/web|cut -d/ -f 2)/@tmp

if [ "$PKG_VERSION" -le 10 ]; then
	rm -rf ${tmp_path}/joomla
	mkdir -p ${tmp_path}/joomla

	/bin/cp -rf /var/services/web/joomla ${tmp_path}/joomla/$PKG_DIR

	while read joomlafile
	do
		# Package center uses preupgarde in new package but joomla_file.all is
		# from old package, thus remove Joomla_X.X.X in $joomlafile first
		joomlafile=${joomlafile#*\/}
		if [ -f "${tmp_path}/joomla/$PKG_DIR/$joomlafile" ]; then
			rm "${tmp_path}/joomla/$PKG_DIR/$joomlafile"
		fi
		# FIXME How about file not removed happens to be newly-added file in new
		# package? The file in new package will be overwritten when postupgrade
	done < /var/services/web/joomla/etc/joomla_file.all
	# Fix old bug
	rm "${tmp_path}/joomla/$PKG_DIR/etc/joomla_file.all"
	echo "path=\"${tmp_path}/joomla\"" > /var/packages/.joomla.conf
	echo "version=\"$PKG_VERSION\"" >> /var/packages/.joomla.conf
fi

exit 0
