::
:: --- BEGIN INFO ---
::
::
:: Applicable Custom App:  All 'Emsisoft xxx' app configurations in d7II default custom app configurations.
::
:: Last Update:  2017-02-03
::
:: Created by Nick @ FoolishIT.com [Foolish IT LLC] as an example for the user-configurable d7II custom app's system.
::
::
:: - d7II Config Location:  (applicable to most anything having to do with this batch file)
::   Open Config (under Main drop down menu) \ Custom Apps (tab) \ (search/find/highlight desired custom app in left column/box first) \ New/Edit App (sub-tab)
::
:: - This batch file is referenced from custom app configuration items in d7II Config, and may make references to other data available from within
::   the custom app configuration or used by it such as noted here.
::
:: - The 'Whitelist.txt' file created by this batch file will by referenced in the command line arguments passed to the custom app; the configuration
::   for this is located in custom apps config as mentioned above \ then the Execution (sub-tab)
::
:: - This batch file is called into action when configured in the 'Import Config Before Execution' setting in a d7II custom app configuration,
::   which can be found on the 'Pre-Execution' tab of the custom app configuration mentioned above.
::
::   NOTES:  * When a .BAT/.CMD file such as this is found configured under this setting (as opposed to any other text or data file) it will be
::             copied to the custom app's final running directory (the same as any other files would be) but then executed as a batch script (after
::             the installer is executed if one is configured, but prior to execution of the main custom app itself.
::
::           * Similarly, a .VBS file would be copied to the custom app directory and executed as a script as above.
::
::           * Further, a .REG file would be imported to the registry if found configured for this setting.
::
::           * This setting may reference multiple files separated by a comma with no spacing required; e.g. 'file1.bat,file2.txt,file3.reg'
::
::           * All files (no paths) configured here must be located within your 'd7II\Config\CustomApps\3rd Party Configs' path (create the last
::             dirs if not exist.)  They will likely work if located in the 'd7II\Config\CustomApps_d7II\3rd Party Configs' path, but with all
::             d7II default custom app configurations, this path with all content will be deleted and rewritten during updates to the default apps.
::
::
:: Batch File Objectives:
::
:: 1. Obtain d7II path via registry; use this in creating a whitelist to be used by the custom app
::    (to include all custom apps in subdirs of d7II)
::
:: 2. Obtain 3rd Party Tools path via registry; if this is not a subdir of the main d7II Path, then
::    add this to a whitelist to be used by the custom app (to include all custom apps in subdirs)
::
:: 3. create a2cmd whitelist as a new file in the location/directory of the custom app (overwrite existing if any)
::    - batch file current directory should be same as the main executable of the custom app (Execution tab)
::    - add d7II path and include all custom apps within the d7II subdir structure) and other Foolish IT apps
::      to a new file (overwrite) in the current directory (of the custom app whitelist for a2cmd.)
::
::
:: --- BEGIN CODE ---
:: Disable local echo, clear screen, output a blank line.
::
@echo off&cls&echo.
:: Set window title to visually identify what this console window is doing.
title [a2cmd] Performing Additional Custom App Tasks...
:: Change from the Current Working Directory (available to the %cd% variable)
:: to the Current Directory (available as %~dp0 below) where the file actually "lives"
:: and was the location where this batch file was copied to by d7II.exe prior to running it.
::
:: - This should be the path of the main exe for this custom app as configured in d7II;
::   located in custom apps config as mentioned above \ then the Execution (sub-tab)
::
:: - Syntax explanation:
::
::   '~' removes wrapping quotes (never use in a variable if possible, instead wrap the variable as necessary!)
::   'd' returns a drive letter and colon
::   'p' returns a full directory path (without a prepended driveletter, and including a trailing backslash)
::   '0' refers to self (this batch file) as the subject of the above conditions
::
pushd "%~dp0"
:: Set variables for registry value queries and app info
::
set "RegKey=HKLM\Software\Foolish IT\d7II\Session\Paths"
set "RegNameEXE=AppEXE"
set "RegName3PT=3PTDir"
set "MainPath=%systemdrive%\EmsisoftCmd"
set "MainEXE=a2cmd.exe"
set "InstEXE32=EmsisoftCommandlineScanner32.exe"
set "InstEXE64=EmsisoftCommandlineScanner64.exe"
set "InstParms=/S"
:: Determine if 64bit paths should be used.  Note that by using the syntax '%programfiles% (x86)' and not the single
:: '%programfiles(x86)%' then we avoid any errors with the variable not existing and throwing off the actual exist check.
::
if "[%programfiles(x86)%]" NEQ "[]" if exist "%programfiles% (x86)" set Win64=True
::
if defined Win64 (
    set "ProgramDir32=%programfiles(x86)%"
    set "InstEXE=%InstEXE64%"
) else (
    set "ProgramDir32=%programfiles%"
    set "InstEXE=%InstEXE32%"
)
:: Run installer/self-extractor
::
if not exist "%MainPath%\%MainEXE%" if exist %InstEXE% start "" /wait "%InstEXE%" %InstParms%
:: Exit if install path does not exist...
::
if not exist "%MainPath%" (
    goto :eof
) else (
    pushd "%MainPath%"
)
:: Add exclusions for other Foolish IT product paths (unrelated to d7II) that may be on a system;
:: While we could first test for dir exist, the dumb addition of a few extra exclusions to the
:: whitelist without checking for their existence will make much of a difference to any custom app...
::
:: - NOTE:  observe the first code line using a single ">" chr prior to the whitelist filename,
::          this creates new or overwrites an existing file; all subsequent usage for the same
::          filename must include the syntax of double ">>" chrs, which creates new or appends
::          to an existing file.
::
echo %ProgramDir32%\dSupportSuite\>a2cmd_Whitelist.txt
echo %ProgramDir32%\CryptoPrevent\>>a2cmd_Whitelist.txt
echo %ProgramDir32%\Foolish IT\CryptoPrevent\>>a2cmd_Whitelist.txt
:: Extract the full path to d7II.exe for the "d7IIPath" variable; obtain d7II path for the currently active
:: d7II 'session' via a registry query to the registry's copy of d7II's current session configuration.
::
:: - NOTE:  The ' 2^>nul' syntax below redirects errors from the 'reg query' command to 'nul' in order to
::          keep garbage/error messages from defining this variable on an unexpected error...
::
::          Also, similar to the '%~dp0' syntax used with pushd at the beginning of the batch file, the
::          variable syntax '~dp' is used with the variable '%%_' to ensure output as a drive\path without
::          without a filename and without wrapping quotes (a trailing backslash is included in the output
::          and for these purposes it is also expected syntax for the custom app whitelist.
::
for /f "usebackq tokens=2*" %%a in (`reg query "%RegKey%" /v "%RegNameEXE%" 2^>nul`) do set "d7IIPath=%%~dpb"
::
:: Check for a result; if variable is defined above then add this path to the whitelist.
::
if defined d7IIPath echo %d7IIpath%>>a2cmd_Whitelist.txt
:: Next get the path to d7II custom apps / 3rd party tools via reg query, to provide redundancy if this path
:: for any reason is not located within a subdir of d7II.exe itself.  First set the new registry value name.
::
for /f "usebackq tokens=2*" %%a in (`reg query "%RegKey%" /v "%RegName3PT%" 2^>nul`) do set "ToolsPath=%%~b"
::
:: Check for a result; if variable is defined above then add this path to the whitelist, otherwise the rest
:: of this script is useless, so exit.
::
if not defined ToolsPath goto :eof
echo %ToolsPath%\>>a2cmd_Whitelist.txt
:: If no parameters were passed to this batch file, it is being run as part of the "Copy Config" custom app
:: setting (it will be run a second time with parameters for the execution of the custom app itself, which is
:: configured to run this batch file again instead of %MainEXE% as the actual configured exe for this custom
:: app.)
::
:: Test to find out if any parameters were passed to this batch file, and if none are found just update...
::
if [%1] EQU [] goto :RunUpdate
:: If we make it this far then this batch file was launched with command line parameters intended for %MainEXE%,
:: so run the custom app passing all parameters as received and waiting for exit.
::
echo Running scan... (in a separate window; please do NOT close this window!)
::
start "" /wait "%MainEXE%" %*
goto :eof
:RunUpdate
::
:: Since getting here means no parameters were passed, run %MainEXE% using parameters for it's own internal
:: definition/signature update, then exit.
::
echo Starting update... (in a separate window; please do NOT close this window!)
::
start "" /wait "%MainEXE%" /update
goto :eof