@setlocal
@echo off

rem INITIALIZE COMMAND LINE PARAM VARIABLES
    SET APP_NAME=%1
    SET APP_NAMESPACE=%3
    SET APP_ROOT_DIR=%2
    set APP_NAME=%APP_NAME:"=%
    set APP_ROOT_DIR=%APP_ROOT_DIR:"=%
    set APP_NAMESPACE=%APP_NAMESPACE:"=%

rem VALIDATE COMMAND LINE PARAMETERS
(
    rem REQUIRES THREE PARAMETERS
    if "%3"=="" (
        goto :SYNTAX
    )

    rem THERE IS A FOURTH PARAMETER
    if not "%4"=="" (
        rem IF IT IS usevbc, THEN IT IS OK
        if "%4"=="usevbc" (
            set USE_VBC=%4
        ) else (
            goto :SYNTAX
        )
    )

REM echo " %* " | find /v " -? " | find /v " /? " | find /i /v " -help " | find /i /v " /help " | find " %* " > NUL
    if errorlevel 1 goto :SYNTAX
)

rem INITIALIZE HARDCODED VARIABLES (I.E. CONSTANTS)
    rem IF WE REQUEST TO COMPILE USING VBC, TRY USING ONLY VBC
    if "%USE_VBC%"=="usevbc" (
        if Exist "%windir%\Microsoft.NET\Framework\v1.1.4322\vbc.exe" (
            SET VBC_DIR=%windir%\Microsoft.NET\Framework\v1.1.4322
        ) else (
            if Exist "%windir%\Microsoft.NET\Framework\v1.0.3705\vbc.exe" (
                set VBC_DIR=%windir%\Microsoft.NET\Framework\v1.0.3705
            ) else (
                goto :SYNTAX
            )
        )
    ) else (
        rem TRY USING VS.NET 2003 (.NET FRAMEWORK 1.1)
        if Exist "%ProgramFiles%\Microsoft Visual Studio .NET 2003\Common7\IDE\devenv.com" (
            set DEVENV_DIR=%ProgramFiles%\Microsoft Visual Studio .NET 2003\Common7\IDE
        ) else (
            rem TRY USING VS.NET 2002 (.NET FRAMEWORK 1.0)
            if Exist "%ProgramFiles%\Microsoft Visual Studio .NET\Common7\IDE\devenv.com" (
                set DEVENV_DIR=%ProgramFiles%\Microsoft Visual Studio .NET\Common7\IDE
            ) else (
                rem TRY USING VBC .NET FRAMEWORK 1.1
                if Exist "%windir%\Microsoft.NET\Framework\v1.1.4322\vbc.exe" (
                    SET VBC_DIR=%windir%\Microsoft.NET\Framework\v1.1.4322
                ) else (
                    rem TRY USING VBC .NET FRAMEWORK 1.0
                    if Exist "%windir%\Microsoft.NET\Framework\v1.0.3705\vbc.exe" (
                        set VBC_DIR=%windir%\Microsoft.NET\Framework\v1.0.3705
                    ) else (
                        goto :SYNTAX
                    )
                )
            )
        )
    )

rem INITIALIZE DERIVED VARIABLES
    SET APP_BIN_DIR=%APP_ROOT_DIR%\bin
    SET APP_DLL=%APP_BIN_DIR%\%APP_NAME%.dll
    SET APP_PDB=%APP_BIN_DIR%\%APP_NAME%.pdb

rem MODIFY ENVIRONMENT VARIABLES
    SET PATH=%PATH%;%VBC_DIR%;%DEVENV_DIR%
    SET LIB=%LIB%;%APP_BIN_DIR%

rem CHECK WHETHER ANY OF THE APP'S FOLDERS CONTAIN VB FILES
(
    for /R "%APP_ROOT_DIR%" %%F in (*.vb) do (
        goto :HASVB
    )
    rem COMPILATION OF %APP_NAME%.DLL SKIPPED: APP HAS NO VB FILES
    exit /b 0
)

:HASVB

rem MAKE THE APP'S EXISTING DLL AND PDB FILES (IF ANY) WRITABLE
(
    attrib -r "%APP_DLL%" > NUL
    if exist "%APP_PDB%" (
        attrib -r "%APP_PDB%" > NUL
    )
)

rem COMPILE APPLICATION
(
    if Not "%DEVENV_DIR%" == "" (
        rem COMPILING APPLICATION %APP_NAME% Using Visual Studio.Net
        cd /d "%APP_ROOT_DIR%"
        devenv.com %APP_NAME%.vbproj /build release
    ) else (
        rem COMPILE ALL FILES WITH .VB EXTENSIONS IN THE APP'S ROOT FOLDER (AND ANY SUBFOLDERS)
        vbc @"%APP_ROOT_DIR%\CompileApplication.rsp" /out:"%APP_DLL%" /recurse:"%APP_ROOT_DIR%\*.vb" /rootnamespace:%APP_NAMESPACE%
    )
) || (
    echo UNABLE TO COMPILE %APP_NAME%.DLL
    exit /b 3
)

goto :EOF
:SYNTAX
echo.
echo Usage:  CompileApplication AppName AppDirectory AppNameSpace
echo Example1:  CompileApplication MyApp C:\MyApp ParentNS.ChildNS
echo Example2:  CompileApplication MyApp "C:\My App" ParentNS.ChildNS
echo.
echo   AppName    	    The application name.
echo   AppDirectory         The application directory.
echo   AppNameSpace         The application name space.
exit /b 0
goto :EOF
