@ECHO OFF
IF NOT EXIST %systemroot%\system32\inetsrv\APPCMD.exe Goto _NoIIS
echo.
echo.
echo.

set installdir=%CD%
Echo Program will be installed to : %installdir%
echo.
echo.
set virtualdir=%VIRTUAL%
if "%virtualdir%" == "" (
echo Nothing entered, we will stop doing anything
pause 
Goto _Done
)
echo.
echo.


:: Quote the variable and remove the !
Set virtualdir="%virtualdir:!=%"
:: Now revert to previous settings. The Set is needed to pass the
:: virtualdir variable back to the previous environment
EndLocal&Set virtualdir=%virtualdir%
Setlocal EnableDelayedExpansion
For %%I In (^| ^& ^< ^> ^^ + ^( ^) \ / . @ # $ { } [ ] ' ; : , ? ` ^%% ^") Do Set virtualdir=!virtualdir:%%I=!
:: Repeatedly search for ~, =, and *
:_parse
For /F "Tokens=1* Delims=~=*" %%J In ('Echo !virtualdir!') Do (
Set virtualdir=%%J%%K
Set _flag=%%K
)
If NOT "%_flag%"=="" Goto _parse
:: Now revert to previous settings. The Set is needed to pass the
:: virtualdir variable back to the previous environment
Endlocal&Set virtualdir=%virtualdir%


%systemroot%\system32\inetsrv\APPCMD set apppool /apppool.name: "DefaultAppPool" /enable32bitapponwin64:true
if errorlevel 1 Goto _NoAdmin
%systemroot%\system32\inetsrv\APPCMD delete app "Default Web Site/%virtualdir%"
%systemroot%\system32\inetsrv\APPCMD add app /site.name:"Default Web Site" /path:"/%virtualdir%" /physicalPath:"%installdir%"

%systemroot%\system32\inetsrv\APPCMD delete app "Default Web Site/%virtualdir%/images"
%systemroot%\system32\inetsrv\APPCMD add app /site.name:"Default Web Site" /path:"/%virtualdir%/images" /physicalPath:"%installdir%\images"

%systemroot%\system32\inetsrv\APPCMD delete app "Default Web Site/%virtualdir%/photos"
%systemroot%\system32\inetsrv\APPCMD add app /site.name:"Default Web Site" /path:"/%virtualdir%/photos" /physicalPath:"%installdir%\photos"

%systemroot%\system32\inetsrv\APPCMD delete app "Default Web Site/%virtualdir%/docs"
%systemroot%\system32\inetsrv\APPCMD add app /site.name:"Default Web Site" /path:"/%virtualdir%/docs" /physicalPath:"%installdir%\docs"

%systemroot%\system32\inetsrv\APPCMD delete app "Default Web Site/%virtualdir%/bootstrap"
%systemroot%\system32\inetsrv\APPCMD add app /site.name:"Default Web Site" /path:"/%virtualdir%/bootstrap" /physicalPath:"%installdir%\bootstrap"

%systemroot%\system32\inetsrv\APPCMD delete app "Default Web Site/%virtualdir%/js"
%systemroot%\system32\inetsrv\APPCMD add app /site.name:"Default Web Site" /path:"/%virtualdir%/js" /physicalPath:"%installdir%\js"


%systemroot%\system32\inetsrv\APPCMD delete app "Default Web Site/%virtualdir%javascript"
%systemroot%\system32\inetsrv\APPCMD add app /site.name:"Default Web Site" /path:"/%virtualdir%/javascript" /physicalPath:"%installdir%\javascript"

if exist %installdir%\%virtualdir%.html if exist C:\inetpub\wwwroot\  if not exist C:\inetpub\wwwroot\%virtualdir%.html (
	copy %installdir%\%virtualdir%.html C:\inetpub\wwwroot\%virtualdir%.html
	echo copied %installdir%\%virtualdir%.html to C:\inetpub\wwwroot\%virtualdir%.html
	)
%systemroot%\system32\inetsrv\APPCMD set config /section:defaultDocument /+files.[value='%virtualdir%.html']
%systemroot%\system32\inetsrv\APPCMD set config /section:defaultDocument /remove files.[value='%virtualdir%.html']
%systemroot%\system32\inetsrv\APPCMD set config /section:defaultDocument /insert:0.[value='%virtualdir%.html']

REM %systemroot%\system32\inetsrv\APPCMD add app /site.name:"Default Web Site" /path:"/%virtualdir%/temp" /physicalPath:"%installdir%\temp"

	
if errorlevel 1 Goto _NoAdmin
echo.
echo.
echo.
echo Install Directory is %installdir%
echo.
echo Virtual Directory is %virtualdir%
pause


start iexplore http://localhost/%virtualdir%/default.html
Goto _Done


:_NoIIS

echo.
echo. 
echo Action needed:
echo IIS 7 or higher appears to NOT be installed!!
echo Please make sure minimum requirements are met. 
echo. 
pause 
Goto _Done

:_NoAdmin

echo.
echo. 
echo Action needed:
echo Administrator (or equivalent) appears to NOT be running setup!!
echo Please make sure minimum requirements are met. 
echo. 
pause 
Goto _Done

:_err
pause
exit /b %errorlevel%

:_Done
installdir=
