#!/bin/sh

PKG_VOL_TMP="/`/usr/bin/readlink /var/services/homes | /usr/bin/cut -d'/' -f2`/@tmp"
SYS_VOL="/`/usr/bin/readlink /var/services/homes | /usr/bin/cut -d'/' -f2`"

mailstation=/usr/syno/mailstation
WebMailDir="/var/packages/MailStation/target"
WebMailExt=${WebMailDir}/roundcubemail/ext
WebMailConf=${WebMailDir}/roundcubemail/config/main.inc.php

OLDVERSION=`cat /tmp/MailStation.upgrade`
NEWVERSION=`grep ^version /var/packages/MailStation/INFO | cut -d'"' -f2 | cut -d'-' -f2`

update_username()
{
	/usr/syno/pgsql/bin/psql -U roundcube roundcubemail -c "select user_id from users_type where account_type='ldap';" | sed '1,2d' | while read user_id
	do
		if [ x"(" = x"${user_id:0:1}" ]; then
			break;
		fi
		domain_name=`/usr/syno/pgsql/bin/psql -U roundcube roundcubemail -c "select domain_name from users_type where user_id=${user_id};" | sed '1,2d;4d' | tr -d ' '`
		username=`/usr/syno/pgsql/bin/psql -U roundcube roundcubemail -c "select username from users where user_id=${user_id};" | sed '1,2d;4d' | tr -d ' '`
		username=${username}'@'${domain_name}
		`/usr/syno/pgsql/bin/psql -U roundcube roundcubemail -c "BEGIN;UPDATE users SET username='${username}' where user_id=${user_id};END;"`
	done

	/usr/syno/pgsql/bin/psql -U roundcube roundcubemail -c "select user_id from users_type where account_type='win';" | sed '1,2d' | while read user_id
	do
		if [ x"(" = x"${user_id:0:1}" ]; then
			break;
		fi

		domain_name=`/usr/syno/pgsql/bin/psql -U roundcube roundcubemail -c "select domain_name from users_type where user_id=${user_id};" | sed '1,2d;4d' | tr -d ' '`
		username=`/usr/syno/pgsql/bin/psql -U roundcube roundcubemail -c "select username from users where user_id=${user_id};" | sed '1,2d;4d' | tr -d ' '`
		username=${domain_name}'\\'${username}
		`/usr/syno/pgsql/bin/psql -U roundcube roundcubemail -c "BEGIN;UPDATE users SET username='${username}' where user_id=${user_id};END;"`
	done

}

echo Upgrade from version ${OLDVERSION} to version ${NEWVERSION} >> ${WebMailDir}/roundcubemail/logs/upgrade
if [ $OLDVERSION -le 16 -a $NEWVERSION -ge 17 ]; then
	UPGRADE_SQL="/var/packages/MailStation/scripts/postgres.update.sql"
	if [ -f ${UPGRADE_SQL} ]; then
		/usr/syno/pgsql/bin/psql -U roundcube roundcubemail < ${UPGRADE_SQL} >> ${WebMailDir}/roundcubemail/logs/upgrade 2>&1
	fi
fi

if [ $OLDVERSION -le 18 -a $NEWVERSION -ge 19 ]; then
	UPGRADE_SQL="/var/packages/MailStation/scripts/postgres.syno.update.sql"
	if [ -f ${UPGRADE_SQL} ]; then
		/usr/syno/pgsql/bin/psql -U roundcube roundcubemail < ${UPGRADE_SQL} >> ${WebMailDir}/roundcubemail/logs/upgrade 2>&1
	fi
fi
if [ $OLDVERSION -le 46 -a $NEWVERSION -ge 47 ]; then
	UPGRADE_SQL="/var/packages/MailStation/scripts/postgres.syno.domain.update.sql"
	if [ -f ${UPGRADE_SQL} ]; then
		/usr/syno/pgsql/bin/psql -U roundcube roundcubemail < ${UPGRADE_SQL} >> ${WebMailDir}/roundcubemail/logs/upgrade 2>&1
	fi
fi
if [ $OLDVERSION -eq 47 -a $NEWVERSION -ge 48 ]; then
	/usr/syno/pgsql/bin/psql -U roundcube roundcubemail -c "BEGIN;UPDATE users_type SET account_type='local' WHERE account_type='';END;" 2>&1
fi

if [ $OLDVERSION -eq 62 -a $NEWVERSION -ge 63 ] || [ $OLDVERSION -eq 61 -a $NEWVERSION -ge 63 ]; then
	update_username
	local UPDATE_SCRIPT_FOLDER="/var/packages/MailStation/scripts/SQL/postgres"
	#updates=("2010100600.sql" "2011011200.sql" "2011092800.sql" "2011111600.sql" "2011121400.sql" "2012080700.sql" "2013011000.sql" )
	/usr/syno/pgsql/bin/psql -U roundcube roundcubemail < ${UPDATE_SCRIPT_FOLDER}/2010100600.sql >> ${WebMailDir}/roundcubemail/logs/upgrade 2>&1
	/usr/syno/pgsql/bin/psql -U roundcube roundcubemail < ${UPDATE_SCRIPT_FOLDER}/2011011200.sql >> ${WebMailDir}/roundcubemail/logs/upgrade 2>&1
	/usr/syno/pgsql/bin/psql -U roundcube roundcubemail < ${UPDATE_SCRIPT_FOLDER}/2011092800.sql >> ${WebMailDir}/roundcubemail/logs/upgrade 2>&1
	/usr/syno/pgsql/bin/psql -U roundcube roundcubemail < ${UPDATE_SCRIPT_FOLDER}/2011111600.sql >> ${WebMailDir}/roundcubemail/logs/upgrade 2>&1
	/usr/syno/pgsql/bin/psql -U roundcube roundcubemail < ${UPDATE_SCRIPT_FOLDER}/2011121400.sql >> ${WebMailDir}/roundcubemail/logs/upgrade 2>&1
	/usr/syno/pgsql/bin/psql -U roundcube roundcubemail < ${UPDATE_SCRIPT_FOLDER}/2012080700.sql >> ${WebMailDir}/roundcubemail/logs/upgrade 2>&1
	/usr/syno/pgsql/bin/psql -U roundcube roundcubemail < ${UPDATE_SCRIPT_FOLDER}/2013011000.sql >> ${WebMailDir}/roundcubemail/logs/upgrade 2>&1
fi

/bin/grep -Ev "rcmail_config.*extmailallow|rcmail_config.*extmailperiod" ${WebMailConf} > /tmp/MailStation.$$
/bin/cat /tmp/MailStation.restore >> /tmp/MailStation.$$
/bin/mv /tmp/MailStation.$$ ${WebMailConf}


rm -f /tmp/MailStation.upgrade
rm -f /tmp/MailStation.restore

if [ -d /tmp/MailStation/ext ]; then
	/bin/cp -a /tmp/MailStation/ext ${WebMailDir}/roundcubemail
	/bin/rm -rf /tmp/MailStation/ext > /dev/null 2>&1
fi

sed -i 's/\/usr\/syno\/mailstation\/sbin\/procmail/\/var\/packages\/MailStation\/target\/bin\/procmail/g' ${WebMailDir}/roundcubemail/ext/*_fetch

exit 0


