@echo off

:: This batch program updates the configArchive file of UWL, to include the name of your custom connector
:: The following variables need to be set before running this batch program

:: you custom connector name
:: e.g CUST_CON=RssConnector
set CUST_CON=

:: e.g. J2EE_HOME=C:\usr\sap\P38\JC00\j2ee
set J2EE_HOME=

:: e.g JAVA_HOME=C:\usr\sap\P38\jdk1.4.2
set JAVA_HOME=

:: you custom connector portal project location - specifically the PORTAL-INF directory location
:: e.g DEST_DIR=C:\tmp\.dtc\LocalDevelopment\DCs\sap.com\uwl.rss.portal\_comp\dist\PORTAL-INF
set DEST_DIR=


if ""=="%CUST_CON%" (
 echo Name of the Custom Connector not set
 goto errorPath
)

if NOT EXIST "%J2EE_HOME%" (
 echo J2EE_HOME not set, or the set path does not exist.
 goto errorPath
)

if NOT EXIST "%JAVA_HOME%" (
 echo JAVA_HOME not set, or the set path does not exist
 goto errorPath
)

if NOT EXIST "%DEST_DIR%" (
 echo Destination Directory, PORTAL-INF location of your custom connector project, not set or the set path does not exist
 goto  errorPath
)

:: not error yet - so continue
goto continue

:errorPath
echo Edit this batch file and set the above paths
pause
exit


:continue

set startDir=%cd%
set path=%path%;"%JAVA_HOME%\bin"

pushd %J2EE_HOME%\cluster\server0\apps\sap.com\irj\servlet_jsp\irj\root\WEB-INF\portal\portalapps\com.sap.netweaver.bc.uwl\config
jar -xf com.sap.netweaver.bc.uwl.configarchive install\meta\lib\com.sap.netweaver.bc.uwl.configmeta

pushd install\meta\lib
jar -xf com.sap.netweaver.bc.uwl.configmeta com.sap.netweaver.bc.uwl\systems\UWLSystems.cc.xml

pushd com.sap.netweaver.bc.uwl\systems
for /F "tokens=1* delims=," %%i in (UWLSystems.cc.xml) do (
 for /f "tokens=1,2,3* delims== " %%a in ("%%i") do (
  if "connector"==%%c (
   echo %%i,%CUST_CON%,%%j >> tmp.xml
  ) else (
     if ""=="%%j" (
      echo %%i >> tmp.xml
     ) else echo %%i,%%j >> tmp.xml
  )
 )
)
rename UWLSystems.cc.xml UWLSystems.cc.xml.bak
rename tmp.xml UWLSystems.cc.xml
popd

jar -uf com.sap.netweaver.bc.uwl.configmeta com.sap.netweaver.bc.uwl
rmdir /S /Q com.sap.netweaver.bc.uwl
popd

copy com.sap.netweaver.bc.uwl.configarchive tmp.configarchive
jar -uf tmp.configarchive install
rmdir /S /Q install
move tmp.configarchive "%DEST_DIR%"
popd

pushd %DEST_DIR%
if NOT EXIST config mkdir config
move tmp.configarchive config\com.sap.netweaver.bc.uwl.configarchive


pause