@@echo off & setlocal enabledelayedexpansion >nul 2>nul
:: [W] This file is part of WinLove project

:: --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- #
:: ██╗    ██╗██╗███╗   ██╗██╗      ██████╗ ██╗   ██╗███████╗
:: ██║    ██║██║████╗  ██║██║     ██╔═══██╗██║   ██║██╔════╝
:: ██║ █╗ ██║██║██╔██╗ ██║██║     ██║   ██║██║   ██║█████╗  
:: ██║███╗██║██║██║╚██╗██║██║     ██║   ██║╚██╗ ██╔╝██╔══╝  
:: ╚███╔███╔╝██║██║ ╚████║███████╗╚██████╔╝ ╚████╔╝ ███████╗
:: ╚══╝╚══╝ ╚═╝╚═╝  ╚═══╝╚══════╝ ╚═════╝   ╚═══╝  ╚══════╝
:: https://www.teteos.net/winlove - https://www.hasanmerkit.com/winlove
:: --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- #

:: === === === === === === === === === === === === === === === === === === === === === === === === === === === === === === #
:: [!] It is strongly recommended not to edit this file
:: === === === === === === === === === === === === === === === === === === === === === === === === === === === === === === #

:: --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- #
:: [%] Before script, checks
	:: [F] Message functions
        :: [V] Icons
            :: [T] TeTeOS.Net
                set "_T_=[38;5;166m[T][0m"
            :: [P] PiluX
                set "_P_=[38;5;165m[P][0m"
            :: [i] Message icons
                set "_empty_=[94m[ ][0m"
                set "_info_=[94m[i][0m"
                set "_question_=[94m[?][0m"
                set "_success_=[32m[+][0m"
                set "_warning_=[33m[^!][0m"
                set "_loading_=[33m[%%][0m"
                set "_error_=[31m[X][0m"
            :: [>] Task icons
                set "_read_=[[33m<[0m]"
                set "_write_=[[33m>[0m]"
                set "_step_= [33m-[0m "
            :: [V] OS icons
                set "_U_=[38;5;166m[U][0m"
                set "_L_=[94;5;166m[L][0m"
                set "_W_=[94;5;166m[W][0m"
        :: [X] Errors
            :: [F] error
                goto error_z
                :error
                    if "!OS_Ver_CmdNew!"=="1" ( if not "%~1" == "" ( echo !_error_! [33m%~1[0m ) ) else ( if not "%~1" == "" ( echo [X] %~1 ) )
                    if not "%~2" == "" ( echo     %~2 )
                    if not "%~3" == "" ( echo     %~3 )
                    if "!OS_Ver_CmdNew!"=="1" ( echo [31m[^^!] [33m Press any key to exit.[0m ) else ( if not "%~1" == "" ( echo [^^!] Press any key to exit. ) )
                    pause >nul 2>nul & exit /b 1
                :error_z
            :: [F] error_os
                goto error_os_z
                :error_os
                    call :error "Unsupported OS" "The version of Windows you are currently running is not supported." & exit /b 1;
                :error_os_z
            :: [F] error_arch
                goto error_arch_z
                :error_arch
                    call :error "Unsupported OS" "The system architecture you are currently running is not supported." & exit /b 1;
                :error_arch_z
        :: [!] Warning
            goto warning_z
            :warning
                if "!OS_Ver_CmdNew!" == "1" ( if not "%~1" == "" ( echo !_warning_! %~1 ) ) else ( if not "%~1" == "" ( echo [^!] %~1 ) )
                if not "%~2" == "" ( echo     %~2 )
                if not "%~3" == "" ( echo     %~3 )
                exit /b
            :warning_z
    :: [$] Check commands
        :: [<] Get commands
            :: [F] Function
                goto command_check_z
                :command_check
                    if "%~1" == "" ( exit /b 1 )
                    if exist "%SystemDrive%\Windows\System32\wpeutil.exe" if not exist "%SystemDrive%\Windows\System32\where.exe" (
                        if exist "%SystemDrive%\Windows\System32\%~1.exe" ( exit /b 0 )
                    )
                    where "%~1" >nul 2>nul && exit /b 0
                    exit /b 1
                :command_check_z
            :: [$] Check the commands that should exist on all Windows
                call :command_check "cmd" || ( call :error_os & exit /b 1 )
                call :command_check "attrib" || ( call :error_os & exit /b 1 )
                call :command_check "find" || ( call :error_os & exit /b 1 )
                call :command_check "icacls" || ( call :error_os & exit /b 1 )
                call :command_check "ping" || ( call :error_os & exit /b 1 )
                call :command_check "reg" || ( call :error_os & exit /b 1 )
                call :command_check "takeown" || ( call :error_os & exit /b 1 )
        :: [=] Check Result
            call :command_check "dism" || ( call :error_os & exit /b 1 )
    :: [1] OS_Name = "Windows"
        set "OS_Name=Windows"
    :: [2] OS_Ver = "10", "11", "8.1"
        :: [<] Get OS version
            :: [$] Get Windows Kernel version
                for /f "tokens=4-5 delims=. " %%i in ('ver') do ( set "OS_Kernel=%%i.%%j" )
                set "OS_Ver_No=!OS_Kernel!"
            :: [.] Get Windows version
                if "!OS_Kernel!" == "6.1" ( set "OS_Ver=7" & set "OS_Ver_CmdNew=0" )
                if "!OS_Kernel!" == "6.2" ( set "OS_Ver=8" & set "OS_Ver_CmdNew=0" )
                if "!OS_Kernel!" == "6.3" ( set "OS_Ver=8.1" & set "OS_Ver_CmdNew=0" )
                if "!OS_Kernel!" == "10.0" ( set "OS_Ver=10" & set "OS_Ver_CmdNew=1" )
                if "!OS_Ver!" == "" ( call :error_os & exit /b 1 )
            :: [.] Get Windows Build Number
                for /f "tokens=2,*" %%A in ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v "CurrentBuildNumber" 2^>nul ^| find "CurrentBuildNumber"') do set "OS_Ver_BuildNo=%%B"
                if "!OS_Ver!" == "10" (
                    if !OS_Ver_BuildNo! GEQ 22000 set "OS_Ver=11"
                )
            :: [.] If CMD not support colors
                if "!OS_Ver!" == "10" (
                    if not !OS_Ver_BuildNo! GEQ 17763 (
                        set "OS_Ver_CmdNew=0"
                    )
                )
                if not "!OS_Ver_CmdNew!" == "1" (
                    set "_T_=[T]"
                    set "_P_=[P]"
                    set "_empty_=[ ]"
                    set "_info_=[i]"
                    set "_question_=[?]"
                    set "_success_=[+]"
                    set "_warning_=[^!]"
                    set "_loading_=[%%]"
                    set "_error_=[X]"
                    set "_read_=[<]"
                    set "_write_=[>]"
                    set "_step_= - "
                    set "_U_=[U]"
                    set "_L_=[L]"
                    set "_W_=[W]"
                )
            :: [<] Get product name
                for /f "tokens=2,*" %%A in ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v "ProductName" 2^>nul ^| find "ProductName"') do set "OS_Ver_ProductName=%%B"
            :: [<] Check is server edition?
                echo !OS_Ver_ProductName! | find /i "Server">nul 2>nul && ( set "OS_Server=1" ) || ( set "OS_Server=0" )
        :: [=] Check Result
            if not "!OS_Ver!"=="8.1" if not "!OS_Ver!"=="10" if not "!OS_Ver!"=="11" (
                call :error "Unsupported OS" "Script designed for Windows 8.1, Windows 10 and Windows 11." & exit /b 1;
            )
            if "!OS_Ver!" == "11" (
                if not !OS_Ver_BuildNo! GEQ 26100 (
                    call :error "Windows 11 version is old." "Please update your Windows version." "Minimum [94m24H2[0m 10.0.26100 supported." & exit /b 1
                )
            )
            if "!OS_Ver!" == "10" (
                if not !OS_Ver_BuildNo! GEQ 19041 (
                    call :error "Windows 10 version is old." "Please update your Windows version." "Minimum -20H1- [94m10.0.19041[0m supported." & exit /b 1
                )
            )
            if "!OS_Ver!" == "8.1" (
                if not !OS_Ver_BuildNo! GEQ 9600 (
                    call :error "Windows 8.1 version is old." "Minimum Update 3 is required for Windows 8.1." & exit /b 1
                )
            )
            if not exist "%SystemDrive%\Users\Default" ( call :error "Unsupported OS" "Failed to get default user folder." & exit /b 1 )
            if not exist "%SystemDrive%\Users\Default\NTUSER.DAT" ( call :error "Unsupported OS" "Failed to get NTUSER.DAT in default user folder." & exit /b 1 )
    :: [3] OS_Arch = "A64", "x64" or "x86"
        :: [<] Get OS architecture
            if not defined PROCESSOR_ARCHITECTURE ( call :error_os & exit /b 1 )
            if "%PROCESSOR_ARCHITECTURE%"=="ARM64" ( set "OS_Arch=A64" )
            if "%PROCESSOR_ARCHITECTURE%"=="AMD64" ( set "OS_Arch=x64" )
            if "%PROCESSOR_ARCHITECTURE%"=="x86" ( set "OS_Arch=x86" )
            if "!OS_Arch!"=="" ( set "OS_Arch=?" )
        :: [=] Check Result
            if not "!OS_Arch!"=="A64" if not "!OS_Arch!"=="x64" if not "!OS_Arch!"=="x86" (
                call :error_arch & exit /b 1;
            )
    :: [4] Admin permission
        :: [<] Get permission level
            :: [#] User is Administrator?
                if not defined USERNAME ( call :error_os & exit /b 1 )
                if "%USERNAME%"=="SYSTEM" set "User_IsAdmin=1"
                if "%USERNAME%"=="Administrator" set "User_IsAdmin=1"
                if "%USERNAME%"=="Default" set "User_IsAdmin=1"
            :: [#] User can execute "net session" successfuly?
                if "!User_IsAdmin!"=="" ( call :command_check "net" && (
                    net session >nul 2>nul && ( set "User_IsAdmin=1" )
                ) )
            :: [#] User is in admin group?
                if "!User_IsAdmin!"=="" ( call :command_check "whoami" && ( call :command_check "findstr" && (
                    whoami /groups | findstr /b BUILTIN\Administrators >nul 2>nul && ( set "User_IsAdmin=2" )
                ) ) )
            :: [X] If still cannot detected, user not admin.
                if "!User_IsAdmin!"=="" ( set "User_IsAdmin=0" )
        :: [=] Check Result
            if not "!User_IsAdmin!"=="1" (
                call :warning "Script requires admin permissions."
                for %%A in (%*) do ( if defined PowerShellArgs ( set "PowerShellArgs=!PowerShellArgs!,\"%%~A\"" ) else ( set "PowerShellArgs=\"%%~A\"" ) )
                call :command_check "powershell" && (
                    if not defined PowerShellArgs (
                        powershell -Command "Start-Process -Verb RunAs -FilePath '%~f0'" >nul 2>nul
                    ) else (
                        powershell -Command "Start-Process -Verb RunAs -FilePath '%~f0' -ArgumentList !PowerShellArgs!" >nul 2>nul
                    )
                )
                exit /b 1
            )
    :: [V] Create path variables
        set "Bin_File=%~nx0"
        set "Bin_Dir=%~dp0"
        set "Bin_Path=%~f0"
        set "Bin_FirstCD=%CD%"
        set "Bin_Params=%*"
        if "%Bin_File:~-1%"=="\" ( set "Bin_File=%Bin_File:~0,-1%" )
        if "%Bin_Dir:~-1%"=="\" ( set "Bin_Dir=%Bin_Dir:~0,-1%" )
        if "%Bin_Path:~-1%"=="\" ( set "Bin_Path=%Bin_Path:~0,-1%" )
        if "%Bin_FirstCD:~-1%"=="\" ( set "Bin_FirstCD=%Bin_FirstCD:~0,-1%" )
    :: [F] Function for parse parameter
        :: [F] Bin_Params_Exist
            goto Bin_Params_Exist_z
            :Bin_Params_Exist
                set "RESULT="
                if "%~1" == "" ( exit /b 1 )
                for %%C in (%Bin_Params%) do ( set "Check=%%C"
                    :: [.] Is a key? (Start with "-, "--")
                        set "Check_FirstLetter=!Check:~0,1!"
                        if "!Check_FirstLetter!"=="-" (
                            if "!Check!"=="%~1" ( exit /b 0 )
                        )
                )
                exit /b 1
            :Bin_Params_Exist_z
            :: Example usage: call :Bin_Params_Exist "--debug" && set "Debug=1" || set "Debug=0"
        :: [F] Bin_Params_Parse
            goto Bin_Params_Parse_z
                :Bin_Params_Parse
                    set "RESULT="
                    if "%~1" == "" ( exit /b 1 )
                    for %%C in (%Bin_Params%) do ( set "Check=%%~C"
                        :: [.] Is a key? (Start with "-, "--")
                            set "Check_FirstLetter=!Check:~0,1!"
                            if "!Check_FirstLetter!"=="-" (
                                if "!Check!"=="%~1" ( set "Key_Found=1" & set "Key_Found_Once=1" ) else set "Key_Found=0"
                            )
                        :: [.] If wanted key found, add to return value
                            if !Key_Found!==1 if not !Check!==%~1 (
                                if defined Result (
                                    set "RESULT=!RESULT! !Check!"
                                ) else (
                                    set "RESULT=!Check!"
                                )
                            )
                    )
                    exit /b
                :Bin_Params_Parse_z
                :: Example usage: call :Bin_Params_Parse --test & if defined Result ( echo !RESULT! )
:: --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- #

:: --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- #
:: [*] Welcome message
    goto welcome_z
    :welcome
    if "!OS_Ver_CmdNew!" == "1" ( echo [94m ) else ( echo. )
    echo Thanks for using;
    echo []     [] [] []]    [] []       [][][]  []    [] [][][]]
    echo []     [] [] [][]   [] []      []    [] []    [] []
    echo []     [] [] [] []  [] []      []    [] []    [] [][][]]
    echo [] []] [] [] []  [] [] []      []    []  []  []  []
    echo  []] []]  [] []   [][] [][][]]  [][][]    [][]   [][][]]
    echo        A customize Windows patch by TeTeOS.Net community
    if "!OS_Ver_CmdNew!" == "1" ( echo [0m ) else ( echo. )
    exit /b
    :welcome_z
:: --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- #

:: --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- #
:: [?] Ask for drive letter.
    :AskDrive
    set /p drive="!_question_! Please enter the drive letter (default: C):"
:: [.] Default value is "C"
    if "!drive!" == "" ( set "drive=C" )
:: [.] Check selected drive
if "!drive!:" == "%SystemDrive%" (
    echo !_error_! Selected drive '!drive!:' is current Windows drive.
    echo.
    goto AskDrive
)
if not exist "!drive!:\" (
    echo !_error_! Selected drive '!drive!:' is not exist.
    echo.
    goto AskDrive
)
if not exist "!drive!:\Windows\System32\Licenses" (
    if not exist "!drive!:\Windows\System32\license.rtf" (
        echo !_error_! Windows license file not found on drive '!drive!:'.
        echo     Please make sure you have entered the correct drive letter.
        echo.
        goto AskDrive
    )
)
:: [?] Ask for target wim location
    set /p imagepath="!_question_! Please enter the target image path ^(Without file extension^) ^(default: D:\install^):"
:: [.] Default value is "D:\install.wim"
    if "!imagepath!" == "" ( set "imagepath=D:\install" )
:: [.] Cleanup
    if exist "!Bin_Dir!\cleanup.cmd" (
        echo [$] Execute: "!Bin_Dir!\cleanup.cmd" -t !drive!:"
        start /wait /realtime "" cmd.exe "/c ""!Bin_Dir!\cleanup.cmd"" -f -t !drive!:
        echo.
    ) else (
        if exist "!Bin_Dir!\..\cleanup.cmd" (
            echo [$] Executing: !Bin_Dir!\..\cleanup.cmd -t !drive!:
            start /wait /realtime "" cmd.exe "/c ""!Bin_Dir!\..\cleanup.cmd"" -f -t !drive!:
            echo.
        )
    )
:: [.] Delete Administrator folder if wanted
    if exist "!drive!:\Program Files\TeTeOS.Net\os\Delete_Admin_Folder_Wanted" (
        del /q /f "!drive!:\Program Files\TeTeOS.Net\os\Delete_Admin_Folder_Wanted"
        echo [i] Deleting Users\Administrator folder...
        rmdir /s /q "!drive!:\Users\Administrator" >nul 2>nul
        for /d %%i in (!drive!:\Users\*Temp*) do rmdir /s /q "%%i" >nul 2>nul
    )
:: [.] Reset Rearm if wanted
    if exist "!drive!:\Program Files\TeTeOS.Net\os\Delete_Rearm_Wanted" (
        reg load HKLM\RESET_REARM_HIVE "!drive!:\Windows\System32\config\system" >nul 2>nul
        reg delete "HKLM\RESET_REARM_HIVE\WPA" /f >nul 2>nul
        reg unload HKLM\RESET_REARM_HIVE >nul 2>nul
        del /q /f "!drive!:\Program Files\TeTeOS.Net\os\Delete_Rearm_Wanted" >nul 2>nul
    )
:: [.] Capturing image
    echo [%%] Capturing !drive!: to %imagepath%
    if exist "!drive!:\WinLove" (
        dism /Capture-Image /ImageFile:"%imagepath%.wim" /CaptureDir:!drive!:\ /Name:WinLove
    ) else (
        dism /Capture-Image /ImageFile:"%imagepath%.wim" /CaptureDir:!drive!:\ /Name:Windows
    )
:: [?] Ask for converting to ESD?
    if exist "%imagepath%.wim" (
        echo.
        set /p sure=[?] Did you want to convert ESD? ^(Not recommended inside Virtual Machines.^):
        if "!sure!" == "y" (
            dism /Export-Image /SourceImageFile:"%imagepath%.wim" /SourceIndex:1 /DestinationImageFile:"%imagepath%.esd" /Compress:recovery /CheckIntegrity
        )
    )
:: [Z] Ending
    :: [i] Press any key to exit.
        echo.
        echo !_info_! Press any key to exit. & pause >nul 2>nul & exit /b 0
:: --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- #
