@echo off
rem  This script is for iPortal to upgrade from old version to new version.
rem  
rem  First of all, you should to set two variables :
rem  OLD_PKG : point to the absolute path of old version iPortal's root directory, and it should not end with "\"
rem  NEW_PKG : point to the absolute path of new version iPortal's root directory, and it should not end with "\"
rem  
chcp 65001
set "CURRENT_DIR=%~dp0"
set /p OLD_PKG=Please enter old version package path : 
set /p NEW_PKG=Please enter new version package path : 

rem  check old version package is exist or not
if not exist %OLD_PKG% goto oldPkgNotExist

rem  check new version package is exist or not
if not exist %NEW_PKG% goto newPkgNotExist

set "OLD_PKG_APP_HOME=%OLD_PKG%\webapps"
set "NEW_PKG_APP_HOME=%NEW_PKG%\webapps"

setlocal
setlocal ENABLEDELAYEDEXPANSION
cd /d %NEW_PKG%
rem  backup conf and webapps of new version package, and crete tmp directory in order to store datas of old version package
mkdir tmp

rem  check if the old version enabled proxy
if not exist %OLD_PKG%\webapp goto updateFiles
set "OLD_PKG_APP_HOME=%OLD_PKG%\webapp"


:updateFiles
rem  cover logs and temp of new version with logs of old version
xcopy /E /R /Y /Q /I %OLD_PKG%\logs %NEW_PKG%\logs

xcopy /E /R /Y /Q %OLD_PKG%\conf\server.xml %NEW_PKG%\conf\server.xml
xcopy /E /R /Y /Q %OLD_PKG%\conf\web.xml %NEW_PKG%\conf\web.xml

rem  copy iserver of old version to tmp of new version
cd /d %OLD_PKG_APP_HOME%
xcopy /E /R /Y /Q iserver %NEW_PKG%\tmp

if exist %NEW_PKG%\tmp\resources (
rd /S /Q %NEW_PKG%\tmp\resources
)

if exist %NEW_PKG%\tmp\META-INF (
rd /S /Q %NEW_PKG%\tmp\META-INF
)

if exist %NEW_PKG%\tmp\font (
rd /S /Q %NEW_PKG%\tmp\font
)

if exist %NEW_PKG%\tmp\output\temp (
rd /S /Q %NEW_PKG%\tmp\output\temp
)

rem  delete tmp\WEB-INF\lib , tmp\WEB-INF\web.xml , tmp\WEB-INF\urlrewrite.xml , tmp\WEB-INF\iserver-storage.xml and tmp\WEB-INF\shiro.ini
cd /d %NEW_PKG%\tmp\WEB-INF
rd /S /Q lib
rd /S /Q worker-lib
if exist urlrewrite.xml (
del /F /Q urlrewrite.xml
)

rem  cover iserver of new version with tmp
cd /d %NEW_PKG%
xcopy /E /R /Y /Q /I tmp\WEB-INF %NEW_PKG_APP_HOME%\iserver\WEB-INF
xcopy /E /R /Y /Q /I tmp\templates\webprinting-layouts %NEW_PKG_APP_HOME%\iserver\templates\webprinting-layouts

cd /d %NEW_PKG%
rem  delete tmp
rd /S /Q tmp

echo Congratulations! Upgrade success!
goto end

:oldPkgNotExist
echo %OLD_PKG% is not exist!
goto end

:newPkgNotExist
echo %NEW_PKG% is not exist!
goto end

:end
pause
