@echo off

set SAMPLE_NAME=SilverlightWSRPPortlets

rem Uncomment these defaults if you find the computed values are inadequate.  Be sure to end the PRODUCTINSTALL and CURRENTPATH locations with trailing \
rem set PRODUCTINSTALL=C:\Oracle\Middleware\WSRPProducerForDotNet\
rem set CURRENTPATH=C:\mydownloads\

rem ############
rem Look for installation location if not set
rem ############

if not "%PRODUCTINSTALL%" == "" goto gethostname

rem Check for 32 bit by seeing if a variable only available on 64 bit windows is present.
if "%ProgramFiles(x86)%" == "" (
	rem test for existence of key
	reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{AEE1001E-C636-47C9-AAA3-D77BFD1F8CA7}" /v InstallLocation
	echo %errorlevel%
	if errorlevel 1 goto regerror
	for /f "tokens=2*" %%a in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{AEE1001E-C636-47C9-AAA3-D77BFD1F8CA7}" /v InstallLocation') do (set PRODUCTINSTALL=%%b)
) else (
	rem test for existence of key
	reg query "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{AEE1001E-C636-47C9-AAA3-D77BFD1F8CA7}" /v InstallLocation
	echo %errorlevel%
	if errorlevel 1 goto regerror
	for /f "tokens=2*" %%a in ('reg query "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{AEE1001E-C636-47C9-AAA3-D77BFD1F8CA7}" /v InstallLocation') do (set PRODUCTINSTALL=%%b)
)


:gethostname

echo %PRODUCTINSTALL% is current location of your WSRP Producer for .NET install

rem ############
rem Get the host name as an IP address if not set
rem ############

if not "%HOSTNAME%" == "" goto getcurrentdir

ipconfig |find "IP" > %temp%\TEMPIP.txt
for /f "tokens=2 delims=:" %%a in (%temp%\TEMPIP.txt) do set HOSTNAME=%%a
del %temp%\TEMPIP.txt
set HOSTNAME=%HOSTNAME:~1%

:getcurrentdir

rem ############
rem Copy over required files from the sample
rem ############
rem get the current location of this batch script first so that relative paths from here can be used.
if not "%CURRENTPATH%" == "" goto copyfiles
set CURRENTPATH=%~dp0

:copyfiles

rem Copy required files from the default wsrpdefault location
mkdir %CURRENTPATH%%SAMPLE_NAME%\bin
xcopy /y /q %PRODUCTINSTALL%wsrpdefault\bin\Oracle.AJAXHelper_signed.dll %CURRENTPATH%%SAMPLE_NAME%\bin\
xcopy /y /q %PRODUCTINSTALL%wsrpdefault\bin\Oracle.WSRPProducer_signed.dll  %CURRENTPATH%%SAMPLE_NAME%\bin\
xcopy /y /q %PRODUCTINSTALL%wsrpdefault\bin\log4net.dll %CURRENTPATH%%SAMPLE_NAME%\bin\
xcopy /y /q %PRODUCTINSTALL%wsrpdefault\Global.asax %CURRENTPATH%%SAMPLE_NAME%\
mkdir %CURRENTPATH%%SAMPLE_NAME%\portlets\Resources
mkdir %CURRENTPATH%%SAMPLE_NAME%\wsdl
xcopy /y /q /s %PRODUCTINSTALL%wsrpdefault\portlets\Resources  %CURRENTPATH%%SAMPLE_NAME%\portlets\Resources
xcopy /y /q /s %PRODUCTINSTALL%wsrpdefault\wsdl %CURRENTPATH%%SAMPLE_NAME%\wsdl

rem Use a tool to update the WSDL files to reference a new virtual directory location from the current wsrpdefault one.
%CURRENTPATH%SampleDeploymentHelper.exe ReplaceToken %CURRENTPATH%%SAMPLE_NAME%\wsdl\1.0\WSRPService.wsdl /wsrpdefault/ /%SAMPLE_NAME%/
%CURRENTPATH%SampleDeploymentHelper.exe ReplaceToken %CURRENTPATH%%SAMPLE_NAME%\wsdl\2.0\WSRPService.wsdl /wsrpdefault/ /%SAMPLE_NAME%/


:iissetup

rem First, set up necessary permissions using tool that App Accelerator for .NET installer used
rem Note that this tool is used instead of CACLS since these users are localized on certain operating systems.
rem IMPORTANT NOTE - Permmanaage will sometimes give an error win Windows 2008 R2 (not regular 2008).  This error can be ignored since the tool does what it expected.
rem Grant EVERYONE read and execute permissions
%PRODUCTINSTALL%uninstall_resources\PermManage.exe add FileSystem %CURRENTPATH%%SAMPLE_NAME% EVERYONE 1 1
rem Grant NETWORK SERVICE user
%PRODUCTINSTALL%uninstall_resources\PermManage.exe add FileSystem %CURRENTPATH%%SAMPLE_NAME% "NETWORK SERVICE" 3 1


rem Next, set up virtual directories for the WSRP portlet producer.  Do this in the same IIS Web Site that WSRP Producer sets up by default called "WSRPProducers"
%PRODUCTINSTALL%uninstall_resources\IISAppAccelSiteCreator.exe install WSRPProducers %SAMPLE_NAME% "%CURRENTPATH%%SAMPLE_NAME%" 8678 2.0.50727

goto end

:regerror
echo The directory for your WSRP Producer for .NET installation could not be located.  If you are sure you have installed the product on this machine modify this batch file so that the variable PRODUCTINSTALL is set to your product installation.

:end
rem reset variables
set PRODUCTINSTALL=
set HOSTNAME=
set CURRENTPATH=


