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

# Link hook
mkdir -p /usr/local/libexec/mariadb_chpw
ln -sf /var/packages/WordPress/scripts/hooks/mariadb_chpw /usr/local/libexec/mariadb_chpw/WordPress

# Link nginx config
SYNOLOGYADD="/var/packages/WordPress/target/synology_added/etc"
NGINX_CONF="www.WordPress.conf"
NGINX_CONF_DIR="/usr/local/etc/nginx/conf.d/"
ln -sf ${SYNOLOGYADD}/${NGINX_CONF} ${NGINX_CONF_DIR}

WPWEBDIR="/var/services/web/wordpress"
VHOST_CFG="/etc/httpd/sites-enabled-user/httpd-vhost.conf-user"
VHOST_CFG_NEW="/usr/local/etc/httpd/sites-enabled/httpd-vhost.conf-user"

if [ -f ${WPWEBDIR}/wp-config.php ]; then
	sed -i "/if (preg_match('\/ServerName '.\$_SERVER/c\\if (\$_SERVER[\"HOST\"] != \"\") {" ${WPWEBDIR}/wp-config.php
	sed -i '/httpd-vhost\.conf-user/d' ${WPWEBDIR}/wp-config.php
fi

if [ "$SYNOPKG_PKG_STATUS" ==  "UPGRADE" -o -f "/tmp/WordPress.upgrade" ]; then
	exit 0
fi

WPDIR="/var/packages/WordPress/target/src"
MYSQL="/usr/bin/mysql"

# Get DSM Version & Set MYSQL
[ -f "/etc.defaults/VERSION" ] || exit 1
DSM_VERSION=`grep ^majorversion= /etc.defaults/VERSION | cut -d'"' -f2`
[ -z "$DSM_VERSION" ] && exit 1

for KEYNAME in AUTH_KEY SECURE_AUTH_KEY LOGGED_IN_KEY NONCE_KEY AUTH_SALT SECURE_AUTH_SALT LOGGED_IN_SALT NONCE_SALT
do
	KEY=`/bin/openssl rand -base64 48`
	/bin/sed -i /\'${KEYNAME}\'/c\\define\(\'${KEYNAME}\',\'${KEY}\'\)\; ${WPDIR}/wp-config-sample.php
done

if [ -d /var/services/web/wordpress/ ]; then
	/bin/mv ${WPDIR}/* /var/services/web/wordpress/.
else
	/bin/mv ${WPDIR} /var/services/web/wordpress
fi
chown -R 1023:1023 ${WPWEBDIR}

echo $pkgwizard_mysql_password > $$
pkgwizard_mysql_password=`sed 's/"/\\"/gp' $$`
/bin/rm -f $$
$MYSQL -u root --password="${pkgwizard_mysql_password}" -e "create database if not exists wordpressblog";
if [ $? -eq "0" ]; then
	/bin/cp -a ${WPWEBDIR}/wp-config-sample.php ${WPWEBDIR}/wp-config.php
	/bin/sed -i /\'DB_NAME\'/c\\define\(\'DB_NAME\',\'wordpressblog\'\)\; ${WPWEBDIR}/wp-config.php
	/bin/sed -i /\'DB_USER\'/c\\define\(\'DB_USER\',\'wordpress\'\)\; ${WPWEBDIR}/wp-config.php
	/bin/sed -i /\'DB_PASSWORD\'/c\\define\(\'DB_PASSWORD\',\'\'\)\; ${WPWEBDIR}/wp-config.php
fi

#Restore upload files, themes, plugins
WP_BAK=/$(readlink /var/packages/WordPress/target|cut -d/ -f 2)/@appstore/.wordpress
if [ -d ${WP_BAK} ]; then
	/bin/cp -af ${WP_BAK}/wp-content/ /var/services/web/wordpress/
	if [ $? -ne "0" ]; then
		logger -p 0 "[WordPress] Fail to restore wp-content"
		echo "WordPress user data restore failed." > \$SYNOPKG_TEMP_LOGFILE
		exit 1
	fi

	# Backup config for web crawler and apache
	WP_WEB_ROOT="/var/services/web/wordpress"
	WP_BKP_LIST="robot.txt .htaccess"
	for f in ${WP_BKP_LIST}; do
		if [ -f ${WP_BAK}/$f ]; then
			/bin/cp -af ${WP_BAK}/$f ${WP_WEB_ROOT}/
			if [ $? -ne "0" ]; then
				logger -p 0 "[WordPress] Fail to restore wp-content ($f)"
				echo "WordPress user data restore failed." > \$SYNOPKG_TEMP_LOGFILE
				exit
			fi
		fi
	done

	# Remove html files which cause DOM XSS if existed
	/bin/rm -f ${WPWEBDIR}/wp-content/themes/twentythirteen/genericons/example.html
	/bin/rm -f ${WPWEBDIR}/wp-content/themes/twentyfourteen/genericons/example.html
	/bin/rm -f ${WPWEBDIR}/wp-content/themes/twentyfifteen/genericons/example.html

	#Correct user/group setting due to WordPress#101
	chown http:http -R /var/services/web/wordpress/wp-content
	/bin/rm -rf ${WP_BAK}
fi
exit 0
