@echo off
setlocal enabledelayedexpansion

echo.
echo  ==============================================================
echo  "     _____ _   _ ____   ___  ____  ____    _    _   _       "
echo  "    |_   _| | | |  _ \ / _ \|  _ \/ ___|  / \  | \ | |      "
echo  "      | | | |_| | | | | | | | |_) \___ \ / _ \ |  \| |      "
echo  "      | | |  _  | |_| | |_| |  _ < ___) / ___ \| |\  |      "
echo  "      |_| |_| |_|____/ \___/|_| \_\____/_/   \_\_| \_|      "
echo  "                                                            "  
echo  ==============================================================
echo                        www.THDORSAN.com
echo  ==============================================================
echo.

net session >nul 2>&1
if %errorlevel% neq 0 (
    echo Requesting administrative privileges...
    powershell -Command "Start-Process '%~dpnx0' -Verb RunAs"
    exit /b
)

cd /d "%~dp0"

if exist "%PROGRAMFILES(X86)%" (
    set "VirtualPath=%LOCALAPPDATA%\VirtualStore\Program Files (x86)\Tamin\DataBase"
    set "InstallPath=%PROGRAMFILES(X86)%\Tamin"
) else (
    set "VirtualPath=%LOCALAPPDATA%\VirtualStore\Program Files\Tamin\DataBase"
    set "InstallPath=%PROGRAMFILES%\Tamin"
)


if not exist "!VirtualPath!" (
    echo Virtual Store path does not exist: !VirtualPath!
    :: pause
    :: exit /b 1
)

if not exist "!InstallPath!\PrjList.exe" (
    echo Installation path does not exist: !InstallPath!
    setup\Setup.exe
)

:: Close any running instances of PrjList.exe
taskkill /F /IM PrjList.exe 2>nul
taskkill /F /IM PrjList-V6.5.exe 2>nul
timeout /t 2 /nobreak >nul

:: Backup Manufactory2.mdb
if not exist "Manufactory2.mdb" (

    if exist "!InstallPath!\Manufactory2.mdb" (
        copy /Y "!InstallPath!\Manufactory2.mdb" "." >nul
    ) else if exist "!VirtualPath!\Manufactory2.mdb" (
        copy /Y "!VirtualPath!\Manufactory2.mdb" "." >nul
    )

    if exist "!VirtualPath!\Manufactory2.mdb" (
        if exist "!InstallPath!\DataBase\Manufactory2.mdb" (
            for /f %%X in ('powershell -Command "$source = Get-Item '!VirtualPath!\Manufactory2.mdb'; $dest = Get-Item '!InstallPath!\DataBase\Manufactory2.mdb' -ErrorAction SilentlyContinue; if ($dest -eq $null -or $source.LastWriteTime -gt $dest.LastWriteTime) { Write-Host 'virtual' } else { Write-Host 'install' }"') do (
                if "%%X"=="virtual" (
                    echo Backup copied from !VirtualPath!\Manufactory2.mdb
                    copy /Y "!VirtualPath!\Manufactory2.mdb" "." >nul
                ) else if "%%X"=="install" (
                    echo Backup copied from !InstallPath!\DataBase\Manufactory2.mdb
                    copy /Y "!InstallPath!\DataBase\Manufactory2.mdb" "." >nul
                )
            )
        )
    )
)


:: Copy new version of PrjList
copy /Y "update\PrjList-V6.5.exe" "!InstallPath!\PrjList.exe" >nul
if errorlevel 1 (
    echo Failed to copy PrjList-V6.5.exe to !InstallPath!\PrjList.exe
    pause
    exit /b 1
)

:: Copy database files
copy /Y "update\DSKKAR00.DBF" "!InstallPath!\DataBase\" >nul
copy /Y "update\DSKWOR00.DBF" "!InstallPath!\DataBase\" >nul
if errorlevel 1 (
    echo Failed to copy database files to !InstallPath!\DataBase\
    pause
    exit /b 1
)

:: Create desktop shortcut
set "DesktopPath=%USERPROFILE%\Desktop"
set "ShortcutPath=!DesktopPath!\ListDisk.exe.lnk"
set "TargetPath=!InstallPath!\PrjList.exe"

if not exist "!DesktopPath!\ListDisk.exe.lnk" (
    if not exist "%PUBLIC%\Desktop\ListDisk.exe.lnk" (
        powershell -Command "$WS = New-Object -ComObject WScript.Shell; $SC = $WS.CreateShortcut('%ShortcutPath%'); $SC.TargetPath = '%TargetPath%'; $SC.Save()"
    )
)

:: Run the program
start "" "!InstallPath!\PrjList.exe"

echo Installation completed successfully.
pause