set PLESK_ROOT_D=%~1
set DBUSER=%~2
set DBPASSWD=%~3
set DBNAME=%~4
set horde=%~5
set PHP5_INSTALLDIR=%~6

set RETURN=0

rem Create horde 3.1.x database schema
"%PLESK_ROOT_D%MySQL\bin\mysql.exe" -u%DBUSER% -p%DBPASSWD% %DBNAME% < "%horde%scripts\upgrades\create_horde_3.1.0.sql"
if x%ERRORLEVEL%x NEQ x0x set RETURN=%ERRORLEVEL%
rem Fix horde 3.1.x database schema
"%PLESK_ROOT_D%MySQL\bin\mysql.exe" -u%DBUSER% -p%DBPASSWD% %DBNAME% < "%horde%scripts\upgrades\create_horde_3.1.0_fix.sql"
if x%ERRORLEVEL%x NEQ x0x set RETURN=%ERRORLEVEL%
rem Create sequence's tables
cmd /c ""%horde%scripts\upgrades\upgrade_horde_sequence.cmd" "%PLESK_ROOT_D%" "%DBUSER%" "%DBPASSWD%" "%DBNAME%" "%horde%""
if x%ERRORLEVEL%x NEQ x0x set RETURN=%ERRORLEVEL%

rem Update horde 3.1.7 -> 3.3.6 (partly)
"%PLESK_ROOT_D%MySQL\bin\mysql.exe" -u%DBUSER% -p%DBPASSWD% %DBNAME% < "%horde%scripts\upgrades\3.1_to_3.2.mysql.sql"
if x%ERRORLEVEL%x NEQ x0x set RETURN=%ERRORLEVEL%
"%PLESK_ROOT_D%MySQL\bin\mysql.exe" -u%DBUSER% -p%DBPASSWD% %DBNAME% < "%horde%mnemo\scripts\upgrades\2.1_to_2.2.sql"
if x%ERRORLEVEL%x NEQ x0x set RETURN=%ERRORLEVEL%

rem Insert horde 3.1.6 data
"%PLESK_ROOT_D%MySQL\bin\mysql.exe" -u%DBUSER% -p%DBPASSWD% %DBNAME% < "%horde%scripts\upgrades\dump_horde_db.sql"
if x%ERRORLEVEL%x NEQ x0x set RETURN=%ERRORLEVEL%

rem Update horde 3.3.6 -> 3.3.8
"%PLESK_ROOT_D%MySQL\bin\mysql.exe" -u%DBUSER% -p%DBPASSWD% %DBNAME% < "%horde%scripts\sql\horde_signups.sql"
if x%ERRORLEVEL%x NEQ x0x set RETURN=%ERRORLEVEL%
"%PHP5_INSTALLDIR%php.exe" -c "%PHP5_INSTALLDIR%php.ini" "%horde%scripts\upgrades\convert_datatree_groups_to_sql.php"
if x%ERRORLEVEL%x NEQ x0x set RETURN=%ERRORLEVEL%
"%PHP5_INSTALLDIR%php.exe" -c "%PHP5_INSTALLDIR%php.ini" "%horde%scripts\upgrades\convert_datatree_perms_to_sql.php"
if x%ERRORLEVEL%x NEQ x0x set RETURN=%ERRORLEVEL%
"%PHP5_INSTALLDIR%php.exe" -c "%PHP5_INSTALLDIR%php.ini" "%horde%scripts\upgrades\move_history_out_of_datatree.php"
if x%ERRORLEVEL%x NEQ x0x set RETURN=%ERRORLEVEL%

rem Update imp 4.1.6 -> 4.3.7
"%PLESK_ROOT_D%MySQL\bin\mysql.exe" -u%DBUSER% -p%DBPASSWD% %DBNAME% < "%horde%imp\scripts\sql\imp.sql"
if x%ERRORLEVEL%x NEQ x0x set RETURN=%ERRORLEVEL%

rem Update ingo 1.1.5 -> 1.2.4
"%PLESK_ROOT_D%MySQL\bin\mysql.exe" -u%DBUSER% -p%DBPASSWD% %DBNAME% < "%horde%ingo\scripts\sql\ingo.sql"
if x%ERRORLEVEL%x NEQ x0x set RETURN=%ERRORLEVEL%

rem Update kronolith 2.1.8 -> 2.3.4
"%PLESK_ROOT_D%MySQL\bin\mysql.exe" -u%DBUSER% -p%DBPASSWD% %DBNAME% < "%horde%kronolith\scripts\upgrades\2.1_to_2.2.sql"
if x%ERRORLEVEL%x NEQ x0x set RETURN=%ERRORLEVEL%
"%PLESK_ROOT_D%MySQL\bin\mysql.exe" -u%DBUSER% -p%DBPASSWD% %DBNAME% < "%horde%kronolith\scripts\upgrades\2.2_to_2.3.sql"
if x%ERRORLEVEL%x NEQ x0x set RETURN=%ERRORLEVEL%
"%PHP5_INSTALLDIR%php.exe" -c "%PHP5_INSTALLDIR%php.ini" "%horde%kronolith\scripts\upgrades\convert_datatree_shares_to_sql.php"
if x%ERRORLEVEL%x NEQ x0x set RETURN=%ERRORLEVEL%

rem Update mnemo 2.1.2 -> 2.2.3
"%PLESK_ROOT_D%MySQL\bin\mysql.exe" -u%DBUSER% -p%DBPASSWD% %DBNAME% < "%horde%mnemo\scripts\upgrades\2.2_to_2.2.1.sql"
if x%ERRORLEVEL%x NEQ x0x set RETURN=%ERRORLEVEL%
"%PLESK_ROOT_D%MySQL\bin\mysql.exe" -u%DBUSER% -p%DBPASSWD% %DBNAME% < "%horde%mnemo\scripts\upgrades\2.2.1_to_2.2.2.sql"
if x%ERRORLEVEL%x NEQ x0x set RETURN=%ERRORLEVEL%
"%PHP5_INSTALLDIR%php.exe" -c "%PHP5_INSTALLDIR%php.ini" "%horde%mnemo\scripts\upgrades\convert_datatree_shares_to_sql.php"
if x%ERRORLEVEL%x NEQ x0x set RETURN=%ERRORLEVEL%

rem Update passwd 3.0.1 -> 3.1.2
	rem Nothing

rem Update turba 2.1.7 -> 2.3.4
"%PLESK_ROOT_D%MySQL\bin\mysql.exe" -u%DBUSER% -p%DBPASSWD% %DBNAME% < "%horde%turba\scripts\upgrades\2.1_to_2.2_add_sql_share_tables.sql"
if x%ERRORLEVEL%x NEQ x0x set RETURN=%ERRORLEVEL%
"%PHP5_INSTALLDIR%php.exe" -c "%PHP5_INSTALLDIR%php.ini" "%horde%turba\scripts\upgrades\2.1_to_2.2_sql_schema.php"
if x%ERRORLEVEL%x NEQ x0x set RETURN=%ERRORLEVEL%
"%PLESK_ROOT_D%MySQL\bin\mysql.exe" -u%DBUSER% -p%DBPASSWD% %DBNAME% < "%horde%turba\scripts\upgrades\2.2.1_to_2.3.sql"
if x%ERRORLEVEL%x NEQ x0x set RETURN=%ERRORLEVEL%
"%PHP5_INSTALLDIR%php.exe" -c "%PHP5_INSTALLDIR%php.ini" "%horde%turba\scripts\upgrades\convert_datatree_shares_to_sql.php"
if x%ERRORLEVEL%x NEQ x0x set RETURN=%ERRORLEVEL%

exit %RETURN%
