anchor.diff:  goto :Done
   
   :Run
  -set cmd=%*
  -if EXIST %LogFile% del %LogFile% /f /q
  -call %cmd%>%LogFile%
  +set _cmd=%*
  +if EXIST %_logFile% del %_logFile% /f /q
  +call %_cmd%>%_logFile%
   exit /b
   
   :Failure
  -set ExitCode=1
  -if NOT '%cmd%'=='' (
  +set _exitCode=1
  +if NOT '%_cmd%'=='' (
       echo.
       echo Failed to run:
  -    echo %cmd%
  +    echo %_cmd%
   )
  -if EXIST %LogFile% type %LogFile%
  +if EXIST %_logFile% type %_logFile%
   
   :Done
  -exit /b %ExitCode%
  -endlocal+exit /b %_exitCode%
  +endlocal
  
positive.code: if '%1'=='/help' goto :Usage
  if '%1'=='-help' goto :Usage
  
  :: Make sure IIS appcmd.exe is accessible
  set appcmd=%windir%\System32\inetsrv\appcmd.exe
  if NOT EXIST %appcmd% (
      echo Could not find: %appcmd%.
      echo Please make sure IIS is installed.
      goto :Failure
  )
  
  :: Clean up any existing setup
  echo Deleting WCF repo at %CurrentRepo% if exists and associated application pools and services hosted on IIS...
  if exist %CurrentRepo% rmdir /s /q %CurrentRepo%
  if exist %WcfTestDir% if /I '%MasterRepo%'=='%CurrentRepo%' rmdir /s /q %WcfTestDir%
  %appcmd% delete app "Default Web Site/%PRServiceName%" >nul
  %appcmd% delete apppool %PRServiceName% >nul
  %appcmd% delete app "Default Web Site/%WcfServiceName%" >nul
  %appcmd% delete apppool %WcfServiceName% >nul
  echo Clean up done.
  if /I '%2'=='/c' goto :Done
  
  
negative.code: )
  
  :: Clone a WCF repo
  set _cmd=git clone %_githubWcfRepoUrl% %_currentRepo%
  call %_cmd%
  if ERRORLEVEL 1 goto :Failure
  pushd %_currentRepo%
  call :Run git config --add origin.fetch "+refs/pull/*/head:refs/remotes/origin/pr/*"
  if ERRORLEVEL 1 goto :Failure
  popd
  
  :: Create a new application pool and an application for the WCF test service
  echo Create IIS application pool: %_wcfServiceName%
  call :Run %_appcmd% add apppool /name:%_wcfServiceName% /processModel.IdentityType:LocalSystem /managedRuntimeVersion:v4.0 /managedPipelineMode:Integrated
  if ERRORLEVEL 1 goto :Failure
  echo Create IIS application: %_wcfServiceName%
  call :Run %_appcmd% add app /site.name:"Default Web Site" /path:/%_wcfServiceName% /physicalPath:%_currentRepo%\src\System.Private.ServiceModel\tools\IISHostedWcfService /applicationPool:%_wcfServiceName% /enabledProtocols:"http,net.tcp"
  if ERRORLEVEL 1 goto :Failure
  
  :: Grant app pool %_wcfServiceName% "Read and Execute" access to the IISHostedWcfService and its subdirectories
  echo Grant app pool %_wcfServiceName% "Read and Execute" access to the IISHostedWcfService and its subdirectories
  call :Run icacls %_currentRepo%\src\System.Private.ServiceModel\tools\IISHostedWcfService /grant:r "IIS APPPOOL\%_wcfServiceName%":(OI)(CI)RX /Q
  if ERRORLEVEL 1 goto :Failure
  
  :: Setup PR service only if this is a master repo
  if /I NOT '%_masterRepo%'=='%_currentRepo%' goto :SkipPRService
  
  :: Create a new application pool and an application for the PR service
  echo Create IIS application pool: %_prServiceName%
  call :Run %_appcmd% add apppool /name:%_prServiceName% /managedRuntimeVersion:v4.0 /managedPipelineMode:Integrated
  if ERRORLEVEL 1 goto :Failure
  echo Create IIS application: %_prServiceName%
  call :Run %_appcmd% add app /site.name:"Default Web Site" /path:/%_prServiceName% /physicalPath:%_currentRepo%\src\System.Private.ServiceModel\tools\PRService -applicationPool:%_prServiceName%
  if ERRORLEVEL 1 goto :Failure
  
  :: Grant app pool %_prServiceName% "Modify" access to %_currentRepo% and its subdirectories
  echo Grant app pool %_prServiceName% "Read and Execute" access to the PRService and its subdirectories
  call :Run icacls %_currentRepo%\src\System.Private.ServiceModel\tools\PRService /grant:r "IIS APPPOOL\%_prServiceName%":(OI)(CI)RX /Q
  if ERRORLEVEL 1 goto :Failure
  
  :SkipPRService
  
  :: Grant master PR service's app pool access to current repo if master PRService exists
  if /I '%_masterRepo%'=='%_currentRepo%' goto :SkipGrantAccess
  if NOT EXIST %_masterRepo% goto :SkipGrantAccess
  %_appcmd% list apppool %_prServiceMaster% >nul
  if ERRORLEVEL 1 goto :SkipGrantAccess
  echo Grant master PRService app pool: %_prServiceMaster% "Modify" access to %_currentRepo% and its sudirectories
  call :Run icacls %_currentRepo% /grant:r "IIS APPPOOL\%_prServiceMaster%":(OI)(CI)M /Q
  if ERRORLEVEL 1 goto :Failure
  
  :SkipGrantAccess
  
  :: Install certificates if %_wcfTestDir% does not exist; otherwise, assume certs are installed already
  if EXIST %_wcfTestDir% goto :SkipCertInstall
  
  :: Make sure there is an HTTPS binding in IIS site "Default Web Site"
  %_appcmd% list site "Default Web Site" /text:* | findstr /i bindings | findstr /i https >nul
  if ERRORLEVEL 1 (
      echo Add an HTTPS binding to "Default Web Site"
      call :Run %_appcmd% set site "Default Web Site" /+bindings.[protocol='https',bindingInformation='*:443:']
      if ERRORLEVEL 1 goto :Failure
  )
  
  :: Use master repo for cert generation if master repo exists; otherwise use current repo
  if EXIST %_masterRepo% (
      set _certRepo=%_masterRepo%
      set _certService=%_wcfServiceMaster%
  ) else (
      set _certRepo=%_currentRepo%
      set _certService=%_wcfServiceName%
  )
  
  echo Build CertificateGenerator tool
  call :Run %_certRepo%\src\System.Private.ServiceModel\tools\scripts\BuildCertUtil.cmd
  if ERRORLEVEL 1 goto :Failure
  
  echo Run CertificateGenerator tool. This will take a little while...
  md %_wcfTestDir%
  set certGen=%_certRepo%\bin\Wcf\tools\CertificateGenerator\CertificateGenerator.exe
  echo ^<?xml version="1.0" encoding="utf-8"?^>^<configuration^>^<appSettings^>^<add key="testserverbase" value="%_certService%"/^>^<add key="CertExpirationInDay" value="%_certExpirationInDays%"/^>^<add key="CrlFileLocation" value="%_wcfTestDir%\test.crl"/^>^</appSettings^>^<startup^>^<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/^>^</startup^>^</configuration^>>%certGen%.config
  call :Run %certGen%
  if ERRORLEVEL 1 goto :Failure
  
  echo Configue SSL certificate ports
  call :Run powershell -NoProfile -ExecutionPolicy unrestricted %_certRepo%\src\System.Private.ServiceModel\tools\scripts\ConfigHttpsPort.ps1
  if ERRORLEVEL 1 goto :Failure
  
  :: TODO: Grant all existing app pools named WCFService# 'Read' access to %_wcfTestDir%. This is not needed for now
  
  :SkipCertInstall
  
  :: Grant app pool %_wcfServiceName% "Read" access to %_wcfTestDir% and its subdirectories
  echo Grant app pool %_wcfServiceName% "Read" access to %_wcfTestDir% and its subdirectories
  call :Run icacls %_wcfTestDir% /grant:r "IIS APPPOOL\%_wcfServiceName%":(OI)(CI)R /Q
  if ERRORLEVEL 1 goto :Failure
  
  :: Clean up log file if everything worked perfectly
  if EXIST %_logFile% del %_logFile% /f /q
  
  echo.
  echo All operations completed successfully!
  