@echo off
chcp 65001 >nul
setlocal
title JX2 WEB MAIL Setup
set "URL=https://webmail.pratamajx2.co.id/"
set "DIR=%LOCALAPPDATA%\JX2WebMail"
echo.
echo  Installing the JX2 WEB MAIL app icon.
echo  Address: %URL%
echo.
if not exist "%DIR%" mkdir "%DIR%"
powershell -NoProfile -ExecutionPolicy Bypass -Command "try { Invoke-WebRequest -UseBasicParsing -Uri '%URL%jx2.ico' -OutFile '%DIR%\jx2.ico' } catch { exit 1 }"
if errorlevel 1 echo  [Warning] Could not download the icon. Check the server connection.

set "BROWSER="
for %%B in ("%ProgramFiles(x86)%\Microsoft\Edge\Application\msedge.exe" "%ProgramFiles%\Microsoft\Edge\Application\msedge.exe" "%ProgramFiles%\Google\Chrome\Application\chrome.exe" "%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe" "%LOCALAPPDATA%\Google\Chrome\Application\chrome.exe") do (
  if not defined BROWSER if exist "%%~B" set "BROWSER=%%~B"
)

if defined BROWSER (
  powershell -NoProfile -ExecutionPolicy Bypass -Command "$w=New-Object -ComObject WScript.Shell; foreach($d in @([Environment]::GetFolderPath('Desktop'),[Environment]::GetFolderPath('Programs'))){ $l=$w.CreateShortcut((Join-Path $d 'JX2 WEB MAIL.lnk')); $l.TargetPath=$env:BROWSER; $l.Arguments='--app=' + $env:URL; $l.IconLocation=$env:DIR + '\jx2.ico'; $l.Description='JX2 WEB MAIL'; $l.Save() }"
) else (
  powershell -NoProfile -ExecutionPolicy Bypass -Command "foreach($d in @([Environment]::GetFolderPath('Desktop'),[Environment]::GetFolderPath('Programs'))){ Set-Content -Encoding ASCII -Path (Join-Path $d 'JX2 WEB MAIL.url') -Value ('[InternetShortcut]',('URL=' + $env:URL),('IconFile=' + $env:DIR + '\jx2.ico'),'IconIndex=0') }"
)
if errorlevel 1 (
  echo  [Failed] Could not create the icon.
) else (
  echo  [Done] "JX2 WEB MAIL" icon created on the desktop and Start menu.
  if defined BROWSER start "" "%BROWSER%" --app=%URL%
)
echo.
pause
