@echo off & setlocal

GOTO :init

:header
    echo %DOCENTRICAX_NAME% v%DOCENTRICAX_VERSION%
    echo Docentric AX core model installation and upgrade tool.
	echo.
	echo ** Detected D365FO Paths and tools	
	echo   D365FO AOS Path: %DOCENTRICAX_AOSPath%
	echo   D365FO AOS ModelUtil Tool: %DOCENTRICAX_AOSTOOL_MODELUTIL%
	echo.
	echo ** Detected and installed Docentric AX models
	echo   %DOCENTRICAX_MODEL_MAIN_MODELNAME%=%DOCENTRICAX_MODEL_MAIN_PATH_EXISTS% (Version:%DOCENTRICAX_MODEL_MAIN_VERSION%; Path=%DOCENTRICAX_MODEL_MAIN_PATH%)
	echo   %DOCENTRICAX_MODEL_EXTENSION_MODELNAME%=%DOCENTRICAX_MODEL_EXTENSION_PATH_EXISTS% (Version:%DOCENTRICAX_MODEL_EXTENSION_VERSION%; Path=%DOCENTRICAX_MODEL_EXTENSION_PATH%)
	echo   %DOCENTRICAX_MODEL_EMAILS_MODELNAME%=%DOCENTRICAX_MODEL_EMAILS_PATH_EXISTS% (Version:%DOCENTRICAX_MODEL_EMAILS_VERSION%; Path=%DOCENTRICAX_MODEL_EMAILS_PATH%)
	echo   %DOCENTRICAX_MODEL_SSRSREPLICAS_MODELNAME%=%DOCENTRICAX_MODEL_SSRSREPLICAS_PATH_EXISTS% (Version:%DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION%; Path=%DOCENTRICAX_MODEL_SSRSREPLICAS_PATH%)
	echo   %DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME%=%DOCENTRICAX_MODEL_CBDREPLICAS_PATH_EXISTS% (Version:%DOCENTRICAX_MODEL_CBDREPLICAS_VERSION%; Path=%DOCENTRICAX_MODEL_CBDREPLICAS_PATH%)
	echo   %DOCENTRICAX_MODEL_WAREHOUSELABELS_MODELNAME%=%DOCENTRICAX_MODEL_WAREHOUSELABELS_PATH_EXISTS% (Version:%DOCENTRICAX_MODEL_WAREHOUSELABELS_VERSION%; Path=%DOCENTRICAX_MODEL_WAREHOUSELABELS_PATH%)
	echo.
    goto :eof

:usage
    echo USAGE:
    echo   %DOCENTRICAX__BAT_NAME% [flags]
    echo.
	echo.  ** NOTE: Please run the tool as administrator
	echo.
	echo.  --^> Available flags
	echo.  /u, --uninstall              uninstall all the core models
	echo.  /?, --help                   shows this help
    echo.  /v, --version                shows the version
	echo.
    goto :eof

:version
    if "%~1"=="full" call :header & goto :eof
    echo %DOCENTRICAX_VERSION%
    goto :eof

:missing_aos
    call :header
    call :usage
    echo.
    echo *******************************************
    echo ****    MISSING "D365FO AOS FOLDER"    ****
    echo *******************************************
    echo.
    goto :eof
:missing_tools
    call :header
    call :usage
    echo.
    echo *******************************************
    echo **** MISSING "REQUIRED D365FO TOOLS"   ****
    echo *******************************************
    echo.
    goto :eof

:elevated_rights
	call :header
    echo.
    echo *********************************************************
    echo ****            ELEVATED RIGHTS REQUIRED             ****
	echo ****      PLEASE RUN THE TOOL AS ADMINISTRATOR.      ****
    echo *********************************************************
    echo.
	goto :eof

:init
	set "DOCENTRICAX_NAME=%~n0"
    set "DOCENTRICAX_VERSION=1.0"
    set "DOCENTRICAX_YEAR=2021"

    set "DOCENTRICAX__BAT_FILE=%~0"
    set "DOCENTRICAX__BAT_PATH=%~dp0"
    set "DOCENTRICAX__BAT_NAME=%~nx0"

    set "DOCENTRICAX_OptHelp="
    set "DOCENTRICAX_OptVersion="
    set "DOCENTRICAX_OptVerbose=no"
	set "DOCENTRICAX_OptQuiet=no"

	REM Find AOS Service path
	set "DOCENTRICAX_AOSPath=[path not found]"
	set "DOCENTRICAX_AOSWebRootPath=[path not found]"	
	
	if exist "C:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=C:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=C:\AosService\WebRoot"
	if exist "D:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=D:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=D:\AosService\WebRoot"
	if exist "E:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=E:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=E:\AosService\WebRoot"
	if exist "F:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=F:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=F:\AosService\WebRoot"
	if exist "G:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=G:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=G:\AosService\WebRoot"
	if exist "H:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=H:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=H:\AosService\WebRoot"
	if exist "I:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=I:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=I:\AosService\WebRoot"
	if exist "J:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=J:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=J:\AosService\WebRoot"
	if exist "K:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=K:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=K:\AosService\WebRoot"
	if exist "M:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=M:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=L:\AosService\WebRoot"
	if exist "N:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=N:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=M:\AosService\WebRoot"
	if exist "O:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=O:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=N:\AosService\WebRoot"
	if exist "P:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=P:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=O:\AosService\WebRoot"
	if exist "Q:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=Q:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=P:\AosService\WebRoot"
	if exist "R:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=R:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=R:\AosService\WebRoot"
	if exist "S:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=S:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=S:\AosService\WebRoot"
	if exist "T:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=T:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=T:\AosService\WebRoot"
	if exist "U:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=U:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=U:\AosService\WebRoot"
	if exist "V:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=V:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=V:\AosService\WebRoot"
	if exist "W:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=W:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=W:\AosService\WebRoot"
	if exist "X:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=X:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=X:\AosService\WebRoot"
	if exist "Y:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=Y:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=Y:\AosService\WebRoot"
	if exist "Z:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=Z:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=Z:\AosService\WebRoot"
	
	REM Init AOS Tools
	set "DOCENTRICAX_AOSTOOL_MODELUTIL=[tool is missing]"
	set "DOCENTRICAX_AOSTOOL_XPPC=[tool is missing]"
	set "DOCENTRICAX_AOSTOOL_LABELC=[tool is missing]"
	set "DOCENTRICAX_AOSTOOL_ReportsC=[tool is missing]"
	set "DOCENTRICAX_AOSTOOL_DBSyncEngine=[tool is missing]"

	REM Check if AOS path was found
    if not defined DOCENTRICAX_AOSPath call :missing_aos & goto :end
	if not exist "%DOCENTRICAX_AOSPath%" call :missing_aos & goto :end

	REM Set all tools
	set "DOCENTRICAX_AOSTOOL_MODELUTIL=%DOCENTRICAX_AOSPath%\Bin\modelutil.exe"

	REM Check if all tools exist
	if not defined DOCENTRICAX_AOSTOOL_MODELUTIL call :missing_tools & goto :end
	if not exist "%DOCENTRICAX_AOSTOOL_MODELUTIL%" call :missing_tools & goto :end

	REM Set Docentric modules
	SET "DOCENTRICAX_MODEL_MAIN_MODULENAME=DocentricAX"
	SET "DOCENTRICAX_MODEL_MAIN_MODELNAME=Docentric AX"
	SET "DOCENTRICAX_MODEL_MAIN_VERSION=Unknown"
	SET "DOCENTRICAX_MODEL_MAIN_PATH=%DOCENTRICAX_AOSPath%\%DOCENTRICAX_MODEL_MAIN_MODULENAME%"
	SET "DOCENTRICAX_MODEL_MAIN_VERSION_DESCRIPTOR_FILE=%DOCENTRICAX_MODEL_MAIN_PATH%\Descriptor\%DOCENTRICAX_MODEL_MAIN_MODELNAME%.xml"
	SET "DOCENTRICAX_MODEL_MAIN_PATH_EXISTS=Yes"
	if not exist "%DOCENTRICAX_MODEL_MAIN_PATH%" set "DOCENTRICAX_MODEL_MAIN_PATH_EXISTS=No" & goto skip_read_version_model_main
	if not exist "%DOCENTRICAX_MODEL_MAIN_VERSION_DESCRIPTOR_FILE%" goto skip_read_version_model_main
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMajor>" ^< "%DOCENTRICAX_MODEL_MAIN_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMajor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMinor>" ^< "%DOCENTRICAX_MODEL_MAIN_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMinor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionBuild>" ^< "%DOCENTRICAX_MODEL_MAIN_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionBuild=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionRevision>" ^< "%DOCENTRICAX_MODEL_MAIN_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionRevision=%%~a
	set "DOCENTRICAX_MODEL_MAIN_VERSION=%DocentricAX_model_versionMajor%.%DocentricAX_model_versionMinor%.%DocentricAX_model_versionBuild%.%DocentricAX_model_versionRevision%"
	:skip_read_version_model_main
	
	SET "DOCENTRICAX_MODEL_EXTENSION_MODULENAME=DocentricAXExtension"
	SET "DOCENTRICAX_MODEL_EXTENSION_MODELNAME=Docentric AX Extension"
	SET "DOCENTRICAX_MODEL_EXTENSION_VERSION=Unknown"
	SET "DOCENTRICAX_MODEL_EXTENSION_PATH=%DOCENTRICAX_AOSPath%\%DOCENTRICAX_MODEL_EXTENSION_MODULENAME%"
	SET "DOCENTRICAX_MODEL_EXTENSION_VERSION_DESCRIPTOR_FILE=%DOCENTRICAX_MODEL_EXTENSION_PATH%\Descriptor\%DOCENTRICAX_MODEL_EXTENSION_MODELNAME%.xml"
	SET "DOCENTRICAX_MODEL_EXTENSION_PATH_EXISTS=Yes"
	if not exist "%DOCENTRICAX_MODEL_EXTENSION_PATH%" set "DOCENTRICAX_MODEL_EXTENSION_PATH_EXISTS=No" & goto skip_read_version_model_extension
	if not exist "%DOCENTRICAX_MODEL_EXTENSION_VERSION_DESCRIPTOR_FILE%" goto skip_read_version_model_extension
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMajor>" ^< "%DOCENTRICAX_MODEL_EXTENSION_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMajor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMinor>" ^< "%DOCENTRICAX_MODEL_EXTENSION_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMinor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionBuild>" ^< "%DOCENTRICAX_MODEL_EXTENSION_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionBuild=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionRevision>" ^< "%DOCENTRICAX_MODEL_EXTENSION_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionRevision=%%~a
	set "DOCENTRICAX_MODEL_EXTENSION_VERSION=%DocentricAX_model_versionMajor%.%DocentricAX_model_versionMinor%.%DocentricAX_model_versionBuild%.%DocentricAX_model_versionRevision%"
	:skip_read_version_model_extension

	SET "DOCENTRICAX_MODEL_EMAILS_MODULENAME=DocentricAXEmails"
	SET "DOCENTRICAX_MODEL_EMAILS_MODELNAME=Docentric AX Emails"
	SET "DOCENTRICAX_MODEL_EMAILS_VERSION=Unknown"
	SET "DOCENTRICAX_MODEL_EMAILS_PATH=%DOCENTRICAX_AOSPath%\%DOCENTRICAX_MODEL_EMAILS_MODULENAME%"
	SET "DOCENTRICAX_MODEL_EMAILS_VERSION_DESCRIPTOR_FILE=%DOCENTRICAX_MODEL_EMAILS_PATH%\Descriptor\%DOCENTRICAX_MODEL_EMAILS_MODELNAME%.xml"
	SET "DOCENTRICAX_MODEL_EMAILS_PATH_EXISTS=Yes"
	if not exist "%DOCENTRICAX_MODEL_EMAILS_PATH%" SET "DOCENTRICAX_MODEL_EMAILS_PATH_EXISTS=No" & goto skip_read_version_model_emails
	if not exist "%DOCENTRICAX_MODEL_EMAILS_VERSION_DESCRIPTOR_FILE%" goto skip_read_version_model_emails
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMajor>" ^< "%DOCENTRICAX_MODEL_EMAILS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMajor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMinor>" ^< "%DOCENTRICAX_MODEL_EMAILS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMinor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionBuild>" ^< "%DOCENTRICAX_MODEL_EMAILS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionBuild=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionRevision>" ^< "%DOCENTRICAX_MODEL_EMAILS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionRevision=%%~a
	set "DOCENTRICAX_MODEL_EMAILS_VERSION=%DocentricAX_model_versionMajor%.%DocentricAX_model_versionMinor%.%DocentricAX_model_versionBuild%.%DocentricAX_model_versionRevision%"
	:skip_read_version_model_emails
	
	SET "DOCENTRICAX_MODEL_SSRSREPLICAS_MODULENAME=DocentricAXSSRSReplicas"
	SET "DOCENTRICAX_MODEL_SSRSREPLICAS_MODELNAME=Docentric AX SSRS Replicas"
	SET "DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION=Unknown"
	SET "DOCENTRICAX_MODEL_SSRSREPLICAS_PATH=%DOCENTRICAX_AOSPath%\%DOCENTRICAX_MODEL_SSRSREPLICAS_MODULENAME%"
	SET "DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION_DESCRIPTOR_FILE=%DOCENTRICAX_MODEL_SSRSREPLICAS_PATH%\Descriptor\%DOCENTRICAX_MODEL_SSRSREPLICAS_MODELNAME%.xml"
	SET "DOCENTRICAX_MODEL_SSRSREPLICAS_PATH_EXISTS=Yes"
	if not exist "%DOCENTRICAX_MODEL_SSRSREPLICAS_PATH%" SET "DOCENTRICAX_MODEL_SSRSREPLICAS_PATH_EXISTS=No" & goto skip_read_version_model_ssrsreplicas
	if not exist "%DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION_DESCRIPTOR_FILE%" goto skip_read_version_model_ssrsreplicas
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMajor>" ^< "%DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMajor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMinor>" ^< "%DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMinor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionBuild>" ^< "%DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionBuild=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionRevision>" ^< "%DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionRevision=%%~a
	set "DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION=%DocentricAX_model_versionMajor%.%DocentricAX_model_versionMinor%.%DocentricAX_model_versionBuild%.%DocentricAX_model_versionRevision%"
	:skip_read_version_model_ssrsreplicas
	
	SET "DOCENTRICAX_MODEL_CBDREPLICAS_MODULENAME=DocentricAXCBDReplicas"
	SET "DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME=Docentric AX CBD Replicas"
	SET "DOCENTRICAX_MODEL_CBDREPLICAS_VERSION=Unknown"
	SET "DOCENTRICAX_MODEL_CBDREPLICAS_PATH=%DOCENTRICAX_AOSPath%\%DOCENTRICAX_MODEL_CBDREPLICAS_MODULENAME%"
	SET "DOCENTRICAX_MODEL_CBDREPLICAS_VERSION_DESCRIPTOR_FILE=%DOCENTRICAX_MODEL_CBDREPLICAS_PATH%\Descriptor\%DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME%.xml"
	SET "DOCENTRICAX_MODEL_CBDREPLICAS_PATH_EXISTS=Yes"
	if not exist "%DOCENTRICAX_MODEL_CBDREPLICAS_PATH%" SET "DOCENTRICAX_MODEL_CBDREPLICAS_PATH_EXISTS=No" & goto skip_read_version_model_cbdreplicas
	if not exist "%DOCENTRICAX_MODEL_CBDREPLICAS_VERSION_DESCRIPTOR_FILE%" goto skip_read_version_model_cbdreplicas
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMajor>" ^< "%DOCENTRICAX_MODEL_CBDREPLICAS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMajor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMinor>" ^< "%DOCENTRICAX_MODEL_CBDREPLICAS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMinor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionBuild>" ^< "%DOCENTRICAX_MODEL_CBDREPLICAS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionBuild=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionRevision>" ^< "%DOCENTRICAX_MODEL_CBDREPLICAS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionRevision=%%~a
	set "DOCENTRICAX_MODEL_CBDREPLICAS_VERSION=%DocentricAX_model_versionMajor%.%DocentricAX_model_versionMinor%.%DocentricAX_model_versionBuild%.%DocentricAX_model_versionRevision%"
	:skip_read_version_model_cbdreplicas
	
	SET "DOCENTRICAX_MODEL_WAREHOUSELABELS_MODULENAME=DocentricAXWarehouseLabels"
	SET "DOCENTRICAX_MODEL_WAREHOUSELABELS_MODELNAME=Docentric AX Warehouse Labels"
	SET "DOCENTRICAX_MODEL_WAREHOUSELABELS_VERSION=Unknown"
	SET "DOCENTRICAX_MODEL_WAREHOUSELABELS_PATH=%DOCENTRICAX_AOSPath%\%DOCENTRICAX_MODEL_WAREHOUSELABELS_MODULENAME%"
	SET "DOCENTRICAX_MODEL_WAREHOUSELABELS_VERSION_DESCRIPTOR_FILE=%DOCENTRICAX_MODEL_WAREHOUSELABELS_PATH%\Descriptor\%DOCENTRICAX_MODEL_WAREHOUSELABELS_MODELNAME%.xml"
	SET "DOCENTRICAX_MODEL_WAREHOUSELABELS_PATH_EXISTS=Yes"
	if not exist "%DOCENTRICAX_MODEL_WAREHOUSELABELS_PATH%" SET "DOCENTRICAX_MODEL_WAREHOUSELABELS_PATH_EXISTS=No" & goto skip_read_version_model_warehouselabels
	if not exist "%DOCENTRICAX_MODEL_WAREHOUSELABELS_VERSION_DESCRIPTOR_FILE%" goto skip_read_version_model_warehouselabels
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMajor>" ^< "%DOCENTRICAX_MODEL_WAREHOUSELABELS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMajor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMinor>" ^< "%DOCENTRICAX_MODEL_WAREHOUSELABELS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMinor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionBuild>" ^< "%DOCENTRICAX_MODEL_WAREHOUSELABELS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionBuild=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionRevision>" ^< "%DOCENTRICAX_MODEL_WAREHOUSELABELS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionRevision=%%~a
	set "DOCENTRICAX_MODEL_WAREHOUSELABELS_VERSION=%DocentricAX_model_versionMajor%.%DocentricAX_model_versionMinor%.%DocentricAX_model_versionBuild%.%DocentricAX_model_versionRevision%"
	:skip_read_version_model_warehouselabels
	
	REM Detect if AOS applications are running (IIS, IISExpress, Batch.exe)
	set "DOCENTRICAX_AOSWEB_RUNNING=No"
	set "DOCENTRICAX_AOSBATCH_RUNNING=No"
	tasklist /fi "IMAGENAME eq w3wp.exe" | find /i "w3wp.exe">NUL
	if %ERRORLEVEL% equ 0 set "DOCENTRICAX_AOSWEB_RUNNING=Yes"
	tasklist /fi "IMAGENAME eq iisexpress.exe" | find /i "iisexpress.exe">NUL
	if %ERRORLEVEL% equ 0 set "DOCENTRICAX_AOSWEB_RUNNING=Yes"
	REM if none of the exe files are running maybe IIS is waiting for the first command
	for /f "tokens=4" %%a in ('sc query W3SVC ^| find "STATE"') do if /i "%%~a" equ "RUNNING" set "DOCENTRICAX_AOSWEB_RUNNING=Yes"
	tasklist /fi "IMAGENAME eq batch.exe" | find /i "batch.exe">NUL
	if %ERRORLEVEL% equ 0 set "DOCENTRICAX_AOSBATCH_RUNNING=Yes"
	
:parse
    if "%~1"=="" goto :validate

	if /i "%~1"=="/u"         set "DOCENTRICAX_UNINSTALL=yes"  & shift & goto :parse
    if /i "%~1"=="-u"         set "DOCENTRICAX_UNINSTALL=yes"  & shift & goto :parse
    if /i "%~1"=="--uninstall"  set "DOCENTRICAX_UNINSTALL=yes"  & shift & goto :parse

    if /i "%~1"=="/?"         call :header & call :usage "%~2" & goto :end
    if /i "%~1"=="-?"         call :header & call :usage "%~2" & goto :end
    if /i "%~1"=="--help"     call :header & call :usage "%~2" & goto :end

    if /i "%~1"=="/v"         call :version      & goto :end
    if /i "%~1"=="-v"         call :version      & goto :end
    if /i "%~1"=="--version"  call :version full & goto :end

    shift
    goto :parse

:validate
	
	REM Check for elevated rights
	net.exe session 1>NUL 2>NUL || (call :elevated_rights & goto :end)

	goto main

:stop_aos_applications
	REM To recycle IIS web apps or IIS Express web apps rename the web.config to other name and then rename it back
	echo Stopping AOS applications	
	echo.
	if /i "%DOCENTRICAX_AOSWEB_RUNNING%" equ "Yes" (	
		echo   Stopping Internet services...
		iisreset /stop
		taskkill /IM w3wp.exe /F 1>NUL 2>NUL
		
		echo   Stopping AOS Web applications...
		move /Y "%DOCENTRICAX_AOSWebRootPath%\web.config" "%DOCENTRICAX_AOSWebRootPath%\___web.config">NUL
		move /Y "%DOCENTRICAX_AOSWebRootPath%\___web.config" "%DOCENTRICAX_AOSWebRootPath%\web.config">NUL
	) else echo   AOS Web not running
	
	if /i "%DOCENTRICAX_AOSBATCH_RUNNING%" equ "Yes" (
		echo   Stopping AOS Batch service...
		call net stop DynamicsAxBatch
		taskkill /IM batch.exe /F 1>NUL 2>NUL
	) else echo   AOS Batch service not running
	
	echo   Terminating Visual Studio related assembly locks
	taskkill /IM msvsmon.exe /F 1>NUL 2>NUL
	echo.
	TIMEOUT /T 2 /NOBREAK 1>NUL 2>NUL
	
	goto :eof

:start_aos_applications
	set "_aos_apps_start_needed="
	if /i "%DOCENTRICAX_AOSWEB_RUNNING%" equ "Yes" set "_aos_apps_start_needed=1"
	if /i "%DOCENTRICAX_AOSBATCH_RUNNING%" equ "Yes" set "_aos_apps_start_needed=1"
	if "%_aos_apps_start_needed%" neq "1" goto :eof
	
	echo.
	echo Starting AOS applications

	if /i "%DOCENTRICAX_AOSWEB_RUNNING%" equ "Yes" (
		REM JUST IN CASE SOMEONE TEMPERED WITH IIS WE FIRST STOP IT AND THEN WE RUN IT AGAIN
		iisreset /stop>NUL
		taskkill /IM w3wp.exe /F 1>NUL 2>NUL
		echo.		
		echo   Starting AOS Web applications and Internet services...
		iisreset /start
	)

	if /i "%DOCENTRICAX_AOSBATCH_RUNNING%" equ "Yes" (
		echo.
		echo   Starting AOS Batch service...
		call net start DynamicsAxBatch
	)
	
	goto :eof

:main
	call :header

	if /i "%DOCENTRICAX_UNINSTALL%" EQU "Yes" goto uninstall
	
	REM DETECT present model files
	set "DOCENTRICAX_MAIN_MODEL_FILE=%DOCENTRICAX__BAT_PATH%Docentric AX.axmodel"
	set "DOCENTRICAX_EXTENSION_MODEL_FILE=%DOCENTRICAX__BAT_PATH%Docentric AX Extension.axmodel"
	set "DOCENTRICAX_EMAILS_MODEL_FILE=%DOCENTRICAX__BAT_PATH%Docentric AX Emails.axmodel"
	set "DOCENTRICAX_WAREHOUSELABELS_MODEL_FILE=%DOCENTRICAX__BAT_PATH%Docentric AX Warehouse Labels.axmodel"
	
	set /p "DOCENTRICAX_MAIN_MODEL_FILE=Enter the Docentric AX model with full path or press ENTER to confirm [%DOCENTRICAX_MAIN_MODEL_FILE%]: "
	set /p "DOCENTRICAX_EXTENSION_MODEL_FILE=Enter the Docentric AX Extension model with full path or press ENTER to confirm [%DOCENTRICAX_EXTENSION_MODEL_FILE%]: "
	set /p "DOCENTRICAX_EMAILS_MODEL_FILE=Enter the Docentric AX Emails model with full path or press ENTER to confirm [%DOCENTRICAX_EMAILS_MODEL_FILE%]: "
	set /p "DOCENTRICAX_WAREHOUSELABELS_MODEL_FILE=Enter the Docentric AX Warehouse Labels model with full path or press ENTER to confirm [%DOCENTRICAX_WAREHOUSELABELS_MODEL_FILE%]: "
	
	if not exist "%DOCENTRICAX_MAIN_MODEL_FILE%" echo "Model file %DOCENTRICAX_MAIN_MODEL_FILE% does not exist!" & goto end
	if not exist "%DOCENTRICAX_EXTENSION_MODEL_FILE%" echo "Model file %DOCENTRICAX_EXTENSION_MODEL_FILE% does not exist!" & goto end
	if not exist "%DOCENTRICAX_EMAILS_MODEL_FILE%" echo "Model file %DOCENTRICAX_EMAILS_MODEL_FILE% does not exist!" & goto end
	
	if exist "%DOCENTRICAX_MODEL_MAIN_VERSION_DESCRIPTOR_FILE%" echo NOTE: ANY CUSTOMIZATIONS WILL BE DELETED. IF YOU HAVE CHANGED ANYTHING MAKE SURE YOU CREATED A BACKUP BEFORE YOU CONTINUE AND PLEASE DO NOT CHANGE DOCENTRIC CORE MODELS IN THE FUTURE!
	
	set /p "DOCENTRICAX_PROCEEDWITHINSTALL=Are you sure you want to install or upgrade Docentric AX models? Enter YES to continue: "
	if /i "%DOCENTRICAX_PROCEEDWITHINSTALL%" NEQ "Yes" set errorlevel=0 & goto :end
	
	if not exist "%DOCENTRICAX_MODEL_WAREHOUSELABELS_VERSION_DESCRIPTOR_FILE%" set /p "DOCENTRICAX_WAREHOUSELABELSINSTALL=To install the optional Docentric AX Warehouse Labels model enter YES or press ENTER to skip: "

	call :stop_aos_applications
	if %ERRORLEVEL% neq 0 echo AOS applications can not be stopped. Terminating...& goto end

	call :uninstall_obsolete_models

	echo Installing models...
	echo.
	if exist "%DOCENTRICAX_MODEL_MAIN_VERSION_DESCRIPTOR_FILE%" (
		echo   Upgrading model: %DOCENTRICAX_MODEL_MAIN_MODELNAME%
		"%DOCENTRICAX_AOSTOOL_MODELUTIL%" -replace -force -metadatastorepath="%DOCENTRICAX_AOSPath%" -modelname="%DOCENTRICAX_MODEL_MAIN_MODELNAME%" -file="%DOCENTRICAX_MAIN_MODEL_FILE%"
		if %errorlevel% NEQ 0 call :start_aos_applications & goto end
	) else (
		echo   Installing model: %DOCENTRICAX_MODEL_MAIN_MODELNAME%
		"%DOCENTRICAX_AOSTOOL_MODELUTIL%" -import -metadatastorepath="%DOCENTRICAX_AOSPath%" -modelname="%DOCENTRICAX_MODEL_MAIN_MODELNAME%" -file="%DOCENTRICAX_MAIN_MODEL_FILE%"
		if %errorlevel% NEQ 0 call :start_aos_applications & goto end
	)
	if exist "%DOCENTRICAX_MODEL_EXTENSION_VERSION_DESCRIPTOR_FILE%" (
		echo   Upgrading model: %DOCENTRICAX_MODEL_EXTENSION_MODELNAME%
		"%DOCENTRICAX_AOSTOOL_MODELUTIL%" -replace -force -metadatastorepath="%DOCENTRICAX_AOSPath%" -modelname="%DOCENTRICAX_MODEL_EXTENSION_MODELNAME%" -file="%DOCENTRICAX_EXTENSION_MODEL_FILE%"
		if %errorlevel% NEQ 0 call :start_aos_applications & goto end
	) else (
		echo   Installing model: %DOCENTRICAX_MODEL_EXTENSION_MODELNAME%
		"%DOCENTRICAX_AOSTOOL_MODELUTIL%" -import -metadatastorepath="%DOCENTRICAX_AOSPath%" -modelname="%DOCENTRICAX_MODEL_EXTENSION_MODELNAME%" -file="%DOCENTRICAX_EXTENSION_MODEL_FILE%"
		if %errorlevel% NEQ 0 call :start_aos_applications & goto end
	)
	if exist "%DOCENTRICAX_MODEL_EMAILS_VERSION_DESCRIPTOR_FILE%" (
		echo   Upgrading model: %DOCENTRICAX_MODEL_EMAILS_MODELNAME%
		"%DOCENTRICAX_AOSTOOL_MODELUTIL%" -replace -force -metadatastorepath="%DOCENTRICAX_AOSPath%" -modelname="%DOCENTRICAX_MODEL_EMAILS_MODELNAME%" -file="%DOCENTRICAX_EMAILS_MODEL_FILE%"
		if %errorlevel% NEQ 0 call :start_aos_applications & goto end
	) else (
		echo   Installing model: %DOCENTRICAX_MODEL_EMAILS_MODELNAME%
		"%DOCENTRICAX_AOSTOOL_MODELUTIL%" -import -metadatastorepath="%DOCENTRICAX_AOSPath%" -modelname="%DOCENTRICAX_MODEL_EMAILS_MODELNAME%" -file="%DOCENTRICAX_EMAILS_MODEL_FILE%"
		if %errorlevel% NEQ 0 call :start_aos_applications & goto end
	)
	if exist "%DOCENTRICAX_MODEL_WAREHOUSELABELS_VERSION_DESCRIPTOR_FILE%" (
			if not exist "%DOCENTRICAX_WAREHOUSELABELS_MODEL_FILE%" echo "Model file %DOCENTRICAX_WAREHOUSELABELS_MODEL_FILE% does not exist!" & call :start_aos_applications & goto end
			
			echo   Upgrading model: %DOCENTRICAX_MODEL_WAREHOUSELABELS_MODELNAME%
			"%DOCENTRICAX_AOSTOOL_MODELUTIL%" -replace -force -metadatastorepath="%DOCENTRICAX_AOSPath%" -modelname="%DOCENTRICAX_MODEL_WAREHOUSELABELS_MODELNAME%" -file="%DOCENTRICAX_WAREHOUSELABELS_MODEL_FILE%"
			if %errorlevel% NEQ 0 call :start_aos_applications & goto end
	) else (
			if /i "%DOCENTRICAX_WAREHOUSELABELSINSTALL%" EQU "Yes" (
	
				if not exist "%DOCENTRICAX_WAREHOUSELABELS_MODEL_FILE%" echo "Model file %DOCENTRICAX_WAREHOUSELABELS_MODEL_FILE% does not exist!" & call :start_aos_applications & goto end
				
				echo   Installing model: %DOCENTRICAX_MODEL_WAREHOUSELABELS_MODELNAME%
				"%DOCENTRICAX_AOSTOOL_MODELUTIL%" -import -metadatastorepath="%DOCENTRICAX_AOSPath%" -modelname="%DOCENTRICAX_MODEL_WAREHOUSELABELS_MODELNAME%" -file="%DOCENTRICAX_WAREHOUSELABELS_MODEL_FILE%"
				if %errorlevel% NEQ 0 call :start_aos_applications & goto end
			)
	)

	call :start_aos_applications

	goto end
	
:uninstall

	echo NOTE: ANY CUSTOMIZATIONS WILL BE DELETED. IF YOU HAVE CHANGED ANYTHING MAKE SURE YOU CREATED A BACKUP BEFORE YOU CONTINUE AND PLEASE DO NOT CHANGE DOCENTRIC CORE MODELS IN THE FUTURE!
	set /p "DOCENTRICAX_PROCEEDWITHUNINSTALL=Are you sure you want to uninstall? Enter YES to continue: "
	if /i "%DOCENTRICAX_PROCEEDWITHUNINSTALL%" NEQ "Yes" set errorlevel=0 & goto :end	
	
	call :stop_aos_applications
	
	echo Uninstalling models...

	echo   Removing model: %DOCENTRICAX_MODEL_MAIN_MODELNAME%
	"%DOCENTRICAX_AOSTOOL_MODELUTIL%" -delete -force -metadatastorepath="%DOCENTRICAX_AOSPath%" -modelname="%DOCENTRICAX_MODEL_MAIN_MODELNAME%"

	echo   Removing model: %DOCENTRICAX_MODEL_EXTENSION_MODELNAME%
	"%DOCENTRICAX_AOSTOOL_MODELUTIL%" -delete -force -metadatastorepath="%DOCENTRICAX_AOSPath%" -modelname="%DOCENTRICAX_MODEL_EXTENSION_MODELNAME%"
		
	echo   Removing model: %DOCENTRICAX_MODEL_EMAILS_MODELNAME%
	"%DOCENTRICAX_AOSTOOL_MODELUTIL%" -delete -force -metadatastorepath="%DOCENTRICAX_AOSPath%" -modelname="%DOCENTRICAX_MODEL_EMAILS_MODELNAME%"
	
	echo   Removing model: %DOCENTRICAX_MODEL_WAREHOUSELABELS_MODELNAME%
	"%DOCENTRICAX_AOSTOOL_MODELUTIL%" -delete -force -metadatastorepath="%DOCENTRICAX_AOSPath%" -modelname="%DOCENTRICAX_MODEL_WAREHOUSELABELS_MODELNAME%"
	echo.
	
	echo Removing empty folders...
    if not exist "%DOCENTRICAX_MODEL_MAIN_VERSION_DESCRIPTOR_FILE%" rmdir /Q /S "%DOCENTRICAX_MODEL_MAIN_PATH%"
	if not exist "%DOCENTRICAX_MODEL_EXTENSION_VERSION_DESCRIPTOR_FILE%" rmdir /Q /S "%DOCENTRICAX_MODEL_EXTENSION_PATH%"
	if not exist "%DOCENTRICAX_MODEL_EMAILS_VERSION_DESCRIPTOR_FILE%" rmdir /Q /S "%DOCENTRICAX_MODEL_EMAILS_PATH%"
	if not exist "%DOCENTRICAX_MODEL_WAREHOUSELABELS_VERSION_DESCRIPTOR_FILE%" rmdir /Q /S "%DOCENTRICAX_MODEL_WAREHOUSELABELS_PATH%"
	echo Done
	
	call :uninstall_obsolete_models
	
	call :start_aos_applications
	
	goto end

:uninstall_obsolete_models

	REM Set deprecated Docentric models
	REM The DocentricAXLicensePlateLabels model is deprecated and has been replaced with a new DocentricWarehouseLabels model.
	SET "DOCENTRICAX_MODEL_LICENSEPLATELABELS_MODULENAME=DocentricAXLicensePlateLabels"
	SET "DOCENTRICAX_MODEL_LICENSEPLATELABELS_MODELNAME=Docentric AX License Plate Labels"
	SET "DOCENTRICAX_MODEL_LICENSEPLATELABELS_PATH=%DOCENTRICAX_AOSPath%\%DOCENTRICAX_MODEL_LICENSEPLATELABELS_MODULENAME%"
	SET "DOCENTRICAX_MODEL_LICENSEPLATELABELS_VERSION_DESCRIPTOR_FILE=%DOCENTRICAX_MODEL_LICENSEPLATELABELS_PATH%\Descriptor\%DOCENTRICAX_MODEL_LICENSEPLATELABELS_MODELNAME%.xml"

	SET "DOCENTRICAX_REMOVE_DEPRECATED_MODELS=NO"

	REM First we check if any of the deprecated models are installed
	if exist "%DOCENTRICAX_MODEL_LICENSEPLATELABELS_VERSION_DESCRIPTOR_FILE%" SET "DOCENTRICAX_REMOVE_DEPRECATED_MODELS=YES"

	REM Then we only execute the uninstallation if there are any models installed
	if /i "%DOCENTRICAX_REMOVE_DEPRECATED_MODELS%" EQU "YES" (
		echo Uninstalling deprecated models...
		echo.
		echo NOTE: DEPRECATED MODELS MUST BE UNINSTALLED AS THEY HAVE BEEN REPLACED BY NEW MODELS.

		if exist "%DOCENTRICAX_MODEL_LICENSEPLATELABELS_VERSION_DESCRIPTOR_FILE%" (
			echo   Removing model: %DOCENTRICAX_MODEL_LICENSEPLATELABELS_MODELNAME%
			REM ModelUtil always asks for deletion, this is why we delete the model physically if it exists
			rmdir /Q /S "%DOCENTRICAX_MODEL_LICENSEPLATELABELS_PATH%"
		)
		echo.
	)
	
	if %ERRORLEVEL% NEQ 0 set ERRORLEVEL=0
	
	goto :eof
	
:end
:exit
	endlocal

	IF %ERRORLEVEL% NEQ 0 TIMEOUT 10
	
	@ECHO ON