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

WPDIR="/var/packages/WordPress/target/src"
WPWEBDIR="/var/services/web/wordpress"
MYSQL="/usr/bin/mysql"

tmp_path=/$(readlink /var/packages/WordPress/target|cut -d/ -f 2)/@tmp

# Official upgrade procedure of WordPress
# Please refer to
# https://codex.wordpress.org/Updating_WordPress

# Clean old source code
/bin/mv  ${WPWEBDIR}/index.php /tmp/wp_temp
/bin/rm -rf ${WPWEBDIR}/wp-includes/
/bin/rm -rf ${WPWEBDIR}/wp-admin/

/bin/cp -af ${WPDIR}/* ${WPWEBDIR}
/bin/mv /tmp/wp_temp ${WPWEBDIR}/index.php

# 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

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"; 

rm -f /tmp/WordPress.upgrade
exit 0
