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

. `dirname $0`/common

SetMySQLPort()
{
	port="$1"
cat << EOF > "$MY_PORT_CNF_FILE"
# DO NOT EDIT THIS FILE !!!
# You can change the port on user interface of MariaDB10.
# Please add other custom configuration to /var/packages/MariaDB10/etc/my.cnf
[mysqld]
port=$port
[client]
port=$port
EOF
	"$BIN_SET_MYSQL_PORT" "$port"
	/usr/syno/sbin/synopkghelper update "$PACKAGE_NAME" port-config
}

DO_LINK
/bin/mkdir -p /usr/local/libexec/mariadb10_chpw

if [ "$pkgwizard_create" = true ]; then 
	ChkExecEnv
	mysqlPath=$(readlink "${DATADIR}")
	rm -rf "${mysqlPath}"
fi

if [ ! -z "$pkgwizard_port" ]; then 
#all new install
	port="$pkgwizard_port"
elif [ -f "$MY_PORT_CNF_FILE" ]; then
# upgrade, restore from old version > 1001, > 52
	port=$(/usr/syno/bin/get_section_key_value "$MY_PORT_CNF_FILE" mysqld port)
else
# upgrade, restore from old version <= 1001, <= 52
	port=3307
fi

SetMySQLPort "$port"

# Modify logrotate setting to correct log file
sed -i "s/mysqld.log/$(hostname).err/g" "${SYNOPKG_PKGDEST}/logrotate.d/mysql"

RebuildDatabase

exit 0
