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

. `dirname $0`/common

# Link hook
mkdir -p /usr/local/libexec/mariadb_chpw
ln -s $PKG_BASE_PATH/$PKG_NAME/scripts/hooks/mariadb_chpw /usr/local/libexec/mariadb_chpw/$PKG_NAME

if [ "$SYNOPKG_PKG_STATUS" = "INSTALL" -a ! -f "$BACKUP_TMP_PATH" ]; then
	/bin/sed -i "s/__MAGENTO_DATABASE_NAME__/$pkgwizard_db_name/g" $PKG_TARGET_PATH/magento/$CONFIG_FILE
fi

# Set website root
mv $PKG_TARGET_PATH/magento $WEBSITE_ROOT

# Enabled php mcrypt module if exists
sed -i "s/^;extension *= *mcrypt\.so.*$/extension = mcrypt.so/" $EXTENSION_INI

# DSM 4 use different config
if [ $SYNOPKG_DSM_VERSION_MAJOR -eq 4 ]; then
	/usr/syno/etc/rc.d/S97apache-user.sh restart > /dev/null 2>&1
else
	initctl restart php-fpm > /dev/null 2>&1
fi

# Create conf folder and write conf/PKG_DEPS for MariaDB
mkdir -p $PKG_CONF
cat > $PKG_CONF/PKG_DEPS << EOF1
[MariaDB]
dsm_min_ver=5.0-4300
EOF1

tmpPath=""
tmpDB=""
if [ -s "$BACKUP_TMP_PATH" ]; then
	tmpPath=`/bin/get_key_value $BACKUP_TMP_PATH $KEY_PATH`
	tmpDB=`/bin/get_key_value $BACKUP_TMP_PATH $KEY_DB`
fi

# if UPGRADE or has backup file, just restore file level data, no need to create database
# if new install, just create database
if [ -n "$tmpPath" -a -n "$tmpDB" ]; then
	if [ "$PKG_DIR_OLD" = "`/usr/bin/basename $tmpPath`" ]; then
		while read FILE; do
			[ "./$CONFIG_FILE" = $FILE ] && continue
			[ -f "$tmpPath/$FILE" ] && rm -f "$tmpPath/$FILE"
		done < $FILE_LIST_OLD
	fi
	/bin/cp -rf $tmpPath/media $WEBSITE_ROOT
	/bin/cp -rf $tmpPath/app/design/frontend $WEBSITE_ROOT/app/design
	/bin/cp -rf $tmpPath/skin/frontend $WEBSITE_ROOT/skin
	/bin/cp -rf $tmpPath/app/code/local $WEBSITE_ROOT/app/code
	/bin/cp -rf $tmpPath/app/code/community $WEBSITE_ROOT/app/code
	/bin/cp -f $tmpPath/$CONFIG_FILE $WEBSITE_ROOT/$CONFIG_FILE
	/bin/cp -f $tmpPath/$POST_CONFIG_FILE $WEBSITE_ROOT/$POST_CONFIG_FILE
	if [ "$SYNOPKG_PKG_STATUS" = "INSTALL" -a -f "$WEBSITE_ROOT/$POST_CONFIG_FILE" ]; then
		/bin/sed -i "s/<password>.*<\/password>/<password><\!\[CDATA\[$pkgwizard_mysql_password\]\]><\/password>/" $WEBSITE_ROOT/$POST_CONFIG_FILE
	fi
else
	#for new install
	pkgwizard_mysql_password=`echo $pkgwizard_mysql_password | sed 's/"/\\"/gp'`
	$MYSQL -u root --password="$pkgwizard_mysql_password" -e "CREATE DATABASE IF NOT EXISTS \`$pkgwizard_db_name\` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci"
	if [ $? -ne 0 ]; then
		echo ${wizard_db_create_error} >> ${SYNOPKG_TEMP_LOGFILE}
		exit 1
	fi
fi

chown -R 1023:1023 $WEBSITE_ROOT
#Setting File and Directory Ownership and Privileges
cd $WEBSITE_ROOT
find . -type d -exec chmod 700 {} \;
find . -type f -exec chmod 600 {} \;

if [ -n "$tmpPath" -a -n "$tmpDB" ]; then
	if [ -d "$tmpPath/var/$BKP_FOLDER" ]; then
		postfix="`/bin/stat $tmpPath | /bin/grep "Change" | /usr/bin/awk '{ print $2 "_" $3 }'`"
		/bin/mv "$tmpPath/var/$BKP_FOLDER" "$tmpPath/var/$BKP_FOLDER""_$postfix"
	fi
	/bin/mv $tmpPath/var/$BKP_FOLDER* $MAG_VAR

	/bin/cat $BACKUP_CONF_PATH | /bin/grep -v "$KEY_PATH=\"$tmpPath\"" > $BACKUP_CONF_PATH.$$
	if [ -s "$BACKUP_CONF_PATH.$$" ]; then
		/bin/mv $BACKUP_CONF_PATH.$$ $BACKUP_CONF_PATH
	else
		/bin/rm -f $BACKUP_CONF_PATH*
	fi

	rm -rf $tmpPath $BACKUP_TMP_PATH
	volume="/`/bin/echo $tmpPath | /usr/bin/cut -d'/' -f2`"
	currBkpBase="$volume/@appstore/.magento"
	count=`/bin/ls -1 $currBkpBase | /usr/bin/wc -l`
	if [ 0 -eq $count ]; then
		rm -rf $currBkpBase
	fi
fi

exit 0
