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

. `dirname $0`/string_loader
. `dirname $0`/common

# Moodle dir name is changed by moodle version (ex moodle-2.4.1), we need to know dir name of that version
OLD_MDDIR="`grep "^MDPACKAGE=" /var/packages/Moodle/scripts/start-stop-status | cut -d'"' -f2`"

# Moodle version > 0028 will not store MDPACKAGE in start-stop-status
[ -z "$OLD_MDDIR" ] && exit 0
OldConfig=$(FindConfInMoodleSrc $OLD_MDDIR)
[ -z "$OldConfig" ] && exit 0

# get Moodle data root path in old moodle dir
MDDATA_PATH=`/bin/get_key_value $OldConfig x_dataroot_x`
DB_NAME=`/bin/get_key_value $OldConfig x_database_x`
rm -f $OldConfig
# Storte data_root and old_dir into tmp file, and postinst will use those
echo "x_dataroot_x=$MDDATA_PATH" > $MD_CONFIG

# for the old version <= 0006 we need to backup them before run old preuninst
if [ -f $OLD_MDDIR/config.php ]; then
	/bin/cp -f $OLD_MDDIR/config.php $MDDATA_PATH/$CONFIG_BAK
fi

# check user's database password
pkgwizard_mysql_password=$(ParsePassword $pkgwizard_mysql_password)

$MYSQL -u root --password="$pkgwizard_mysql_password"
if [ $? -eq "1" ]; then
	echo $md_mysql_password_error > $SYNOPKG_TEMP_LOGFILE
	exit 1;
fi

echo "x_database_x=$DB_NAME" >> $MD_CONFIG
exit 0

