Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @echo off
- setlocal
- call :init
- set /a "error_status=%errorlevel%"
- if %error_status% neq 0 exit /b %error_status%
- set /a "i=0"
- :copy_options
- set "option=%~1"
- if defined option (
- set "args[%i%]=%option%"
- shift
- set /a "i+=1"
- goto copy_options
- )
- set /a "args_count=i"
- set /a "i=0"
- :main_loop
- set /a "j=i + 1"
- call set "option=%%args[%i%]%%"
- call set "value=%%args[%j%]%%"
- if %i% geq %args_count% goto main_loop_break
- set /a "is___version=FALSE"
- if "%option%" == "--version" set /a "is___version=TRUE"
- if "%option%" == "-v" set /a "is___version=TRUE"
- if %is___version% equ %TRUE% (
- call :__version_handler
- exit /b %SUCCESS_EC%
- )
- set /a "is___help=FALSE"
- if "%option%" == "--help" set /a "is___help=TRUE"
- if "%option%" == "-h" set /a "is___help=TRUE"
- if %is___help% equ %TRUE% (
- call :__help_handler
- exit /b %SUCCESS_EC%
- )
- set /a "is___name=FALSE"
- if "%option%" == "--name" set /a "is___name=TRUE"
- if "%option%" == "-n" set /a "is___name=TRUE"
- if %is___name% equ %TRUE% (
- set "name=%value%"
- set /a "i+=2"
- goto main_loop
- )
- set /a "is___archive=FALSE"
- if "%option%" == "--archive" set /a "is___archive=TRUE"
- if "%option%" == "-a" set /a "is___archive=TRUE"
- if %is___archive% equ %TRUE% (
- set "archive=%value%"
- set /a "i+=2"
- goto main_loop
- )
- set /a "is___directory=FALSE"
- if "%option%" == "--directory" set /a "is___directory=TRUE"
- if "%option%" == "-d" set /a "is___directory=TRUE"
- if %is___directory% equ %TRUE% (
- set "directory=%value%"
- set /a "i+=2"
- goto main_loop
- )
- echo %WRONG_OPTION_SPECIFIED_MSG%
- exit /b %WRONG_OPTION_SPECIFIED_EC%
- :main_loop_break
- if not defined name (
- echo %NAME_NOT_SPECIFIED_MSG%
- exit /b %NAME_NOT_SPECIFIED_EC%
- )
- if not defined archive (
- echo %ARCHIVE_NOT_SPECIFIED_MSG%
- exit /b %ARCHIVE_NOT_SPECIFIED_EC%
- )
- if not defined directory (
- echo %DIRECTORY_NOT_SPECIFIED_MSG%
- exit /b %DIRECTORY_NOT_SPECIFIED_EC%
- )
- (
- echo @echo off
- echo setlocal
- echo.
- echo call :init
- echo set /a "error_status=%%errorlevel%%"
- echo if %%error_status%% neq 0 exit /b %%error_status%%
- echo.
- echo set /a "i=0"
- echo :copy_options
- echo set "option=%%~1"
- echo if defined option ^(
- echo set "args[%%i%%]=%%option%%"
- echo shift
- echo set /a "i+=1"
- echo goto copy_options
- echo ^)
- echo set /a "args_count=i"
- echo.
- echo set /a "i=0"
- echo :main_loop
- echo set /a "j=i + 1"
- echo call set "option=%%%%args[%%i%%]%%%%"
- echo call set "value=%%%%args[%%j%%]%%%%"
- echo.
- echo if %%i%% geq %%args_count%% goto main_loop_break
- echo.
- echo set /a "is___version=FALSE"
- echo.
- echo if "%%option%%" == "--version" set /a "is___version=TRUE"
- echo if "%%option%%" == "-v" set /a "is___version=TRUE"
- echo.
- echo if %%is___version%% equ %%TRUE%% ^(
- echo call :__version_handler
- echo exit /b %%SUCCESS_EC%%
- echo ^)
- echo.
- echo set /a "is___help=FALSE"
- echo.
- echo if "%%option%%" == "--help" set /a "is___help=TRUE"
- echo if "%%option%%" == "-h" set /a "is___help=TRUE"
- echo.
- echo if %%is___help%% equ %%TRUE%% ^(
- echo call :__help_handler
- echo exit /b %%SUCCESS_EC%%
- echo ^)
- echo.
- echo set /a "is___uninstall=FALSE"
- echo.
- echo if "%%option%%" == "--uninstall" set /a "is___uninstall=TRUE"
- echo if "%%option%%" == "-u" set /a "is___uninstall=TRUE"
- echo.
- echo if %%is___uninstall%% equ %%TRUE%% ^(
- echo set /a "is_uninstall=TRUE"
- echo set /a "i+=1"
- echo goto main_loop
- echo ^)
- echo.
- echo echo %%WRONG_OPTION_SPECIFIED_MSG%%
- echo exit /b %%WRONG_OPTION_SPECIFIED_EC%%
- echo.
- echo :main_loop_break
- echo if %%is_uninstall%% equ %%TRUE%% ^(
- echo if not exist "%%directory%%" ^(
- echo echo %%BATCHTEMPLATE_IS_NOT_INSTALLED_MSG%%
- echo exit /b %%BATCHTEMPLATE_IS_NOT_INSTALLED_EC%%
- echo ^)
- echo setlocal enabledelayedexpansion
- echo rem setx PATH %%PATH:%directory%=%%
- echo echo y^| rd /s "%%directory%%" 2^> nul ^> nul
- echo set /a "error_status=!errorlevel!"
- echo if !error_status! equ 0 (
- echo echo batchtemplate successfully uninstalled from %%directory%%.
- echo ^) else ^(
- echo echo Unexpected error uccured. Possibly there is no access to %%directory%% folder or another process uses it. Please run installer as admin and kill processes use %%directory%% folder.
- echo ^)
- echo exit /b !error_status!
- echo ^)
- echo.
- echo if exist "%%directory%%" ^(
- echo setlocal enabledelayedexpansion
- echo choice /m "Do you want reinstall batchtemplate in %%directory%%?"
- echo set /a "answer=!errorlevel!"
- echo if !answer! equ 2 ^(
- echo echo %%BATCHTEMPLATE_REINSTALL_REJECTED_MSG%%
- echo exit /b %%BATCHTEMPLATE_REINSTALL_REJECTED_EC%%
- echo ^)
- echo if !answer! equ 1 ^(
- echo echo y^| rd /q /s "%%directory%%" 2^> nul ^> nul
- echo set /a "error_status=!errorlevel!"
- echo if !error_status! neq 0 ^(
- echo echo Unexpected error uccured. Possibly there is no access to %%directory%% folder or another process uses it. Please run installer as admin and kill processes use %%directory%% folder.
- echo exit /b !error_status!
- echo ^)
- echo ^)
- echo ^)
- echo.
- echo mkdir "%%directory%%"
- echo chdir "%%directory%%"
- echo.
- echo certutil -decode "%%self%%" "%%archive_name%%" ^> nul
- echo 7z x "%%archive_name%%" ^> nul
- echo rem setx PATH "%%directory%%!!%%PATH%%"
- echo del /q "%%archive_name%%"
- echo exit /b %%SUCCESS_EC%%
- echo.
- echo :init
- echo set /a "SUCCESS_EC=0"
- echo set /a "WRONG_OPTION_SPECIFIED_EC=1"
- echo set /a "SEVENZ_COMMAND_NOT_FOUND_EC=2"
- echo set /a "BATCHTEMPLATE_IS_NOT_INSTALLED_EC=3"
- echo set /a "BATCHTEMPLATE_REINSTALL_REJECTED_EC=4"
- echo.
- echo set "WRONG_OPTION_SPECIFIED_MSG=Option you've passed is not recognized."
- echo set "SEVENZ_COMMAND_NOT_FOUND_MSG=7z command isn't found. Please install 7-Zip archiver."
- echo set "BATCHTEMPLATE_IS_NOT_INSTALLED_MSG=batchtemplate isn't installed to uninstall it."
- echo set "BATCHTEMPLATE_REINSTALL_REJECTED_MSG=batchtemplate reinstall is rejected."
- echo.
- echo set /a "TRUE=0"
- echo set /a "FALSE=1"
- echo.
- echo set "self=%%~f0"
- echo set "directory=%directory%"
- echo set "temp=temp.txt"
- echo set "archive_name=%archive%"
- echo.
- echo set /a "is_uninstall=FALSE"
- echo 7z --help 2^> nul ^> nul ^|^| ^(
- echo echo %%SEVENZ_COMMAND_NOT_FOUND_MSG%%
- echo exit /b %%SEVENZ_COMMAND_NOT_FOUND_EC%%
- echo ^)
- echo exit /b %%SUCCESS_EC%%
- echo.
- echo :__version_handler
- echo echo 1.0 - 2021
- echo exit /b %%SUCCESS_EC%%
- echo.
- echo :__help_handler
- echo echo Description:
- echo echo Installes or uninstalles batchtemplate script.
- echo echo.
- echo echo Syntax:
- echo echo - %name% [options]
- echo echo.
- echo echo Options:
- echo echo - -u^^^|--uninstall - specifies whether uninstallation is performed instead of installation
- echo echo.
- echo echo Examples:
- echo echo %name% --version
- echo echo %name%
- echo echo %name% --uninstall
- echo echo.
- echo echo Author:
- echo echo Alvin Seville ^^^<AlvinSeville7cf@gmail.com^^^>
- echo exit /b %%SUCCESS_EC%%
- echo.
- ) > "%name%"
- 7z a "%archive%" batchtemplate.bat > nul
- if exist "%temp%" del /q "%temp%"
- certutil -encode "%archive%" "%temp%" > nul
- type "%temp%" >> "%name%"
- del /q "%temp%"
- exit /b %SUCCESS_EC%
- :init
- set /a "SUCCESS_EC=0"
- set /a "WRONG_OPTION_SPECIFIED_EC=1"
- set /a "NAME_NOT_SPECIFIED_EC=2"
- set /a "ARCHIVE_NOT_SPECIFIED_EC=2"
- set /a "DIRECTORY_NOT_SPECIFIED_EC=2"
- set /a "SEVENZ_COMMAND_NOT_FOUND_EC=3"
- set "WRONG_OPTION_SPECIFIED_MSG=Option you've passed is not recognized. Installer hasn't been regenerated."
- set "NAME_NOT_SPECIFIED_MSG=--name value isn't specified. Installer hasn't been regenerated."
- set "ARCHIVE_NOT_SPECIFIED_MSG=--archive value isn't specified. Installer hasn't been regenerated."
- set "DIRECTORY_NOT_SPECIFIED_MSG=--directory value isn't specified. Installer hasn't been regenerated."
- set "SEVENZ_COMMAND_NOT_FOUND_MSG=7z command isn't found. Please install 7-Zip archiver. Installer hasn't been regenerated."
- set /a "TRUE=0"
- set /a "FALSE=1"
- set "temp=temp.txt"
- 7z --help 2> nul > nul || (
- echo %SEVENZ_COMMAND_NOT_FOUND_MSG%
- exit /b %SEVENZ_COMMAND_NOT_FOUND_EC%
- )
- exit /b %SUCCESS_EC%
- :__version_handler
- echo 1.0 - 2021
- exit /b %SUCCESS_EC%
- :__help_handler
- echo Description:
- echo Generates installer that installs batchtemplate.bat.
- echo.
- echo Syntax:
- echo - installergenerator [options]
- echo.
- echo Options:
- echo - -n^|--name - installer name
- echo - -a^|--archive - archive name
- echo - -d^|--directory - installation directory
- echo.
- echo Examples:
- echo installergenerator --version
- echo installergenerator --name installer.bat --archive release.7z --directory "C:\Program files\AlvinSeville7cfSoft\batchtemplate\"
- echo.
- echo Author:
- echo Alvin Seville ^<AlvinSeville7cf@gmail.com^>
- exit /b %SUCCESS_EC%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement