@echo off
setlocal EnableExtensions
title BeHost Comedor Point Red - Instalacion local
set "APP_KEY=red"
set "APP_TITLE=BeHost Comedor Point Red"
set "APP_FOLDER=Red"
set "APP_ZIP_URL=https://behostmexico.com/comedor/windows/offline/comedor-red-local-v107.zip"
set "APP_DEST=%LOCALAPPDATA%\BeHost\Comedor\Red"
set "APP_ZIP=%TEMP%\comedor-red-local-v107.zip"
set "STARTUP=%APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup"
set "DESKTOP=%USERPROFILE%\Desktop"
echo.
echo Instalando Comedor Point Red en esta PC...
echo El sistema se copiara localmente en este equipo.
echo Los datos quedan guardados en este dispositivo, no en la nube.
echo.
if exist "%~dp0index.html" (
  echo Copiando sistema desde esta carpeta...
  if exist "%APP_DEST%" rmdir /s /q "%APP_DEST%"
  mkdir "%APP_DEST%" >nul 2>nul
  xcopy "%~dp0*" "%APP_DEST%" /E /I /Y /Q >nul
) else (
  echo Descargando paquete local desde BeHost...
  powershell -NoProfile -ExecutionPolicy Bypass -Command "[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12; $ErrorActionPreference='Stop'; $url=$env:APP_ZIP_URL; $zip=$env:APP_ZIP; $dest=$env:APP_DEST; if(Test-Path $zip){Remove-Item $zip -Force}; Invoke-WebRequest -Uri $url -OutFile $zip; if(Test-Path $dest){Remove-Item $dest -Recurse -Force}; [void](New-Item -ItemType Directory -Path $dest -Force); if(Get-Command Expand-Archive -ErrorAction SilentlyContinue){Expand-Archive -Path $zip -DestinationPath $dest -Force}else{$sh=New-Object -ComObject Shell.Application; $ns=$sh.NameSpace($zip); $dst=$sh.NameSpace($dest); $dst.CopyHere($ns.Items(),16)}"
  if errorlevel 1 (
    echo.
    echo No se pudo descargar o descomprimir el sistema.
    echo Revisa internet para esta primera instalacion o descarga el ZIP completo.
    pause
    exit /b 1
  )
)
if not exist "%APP_DEST%\index.html" (
  echo.
  echo No se encontro index.html en la instalacion local.
  echo Ruta esperada: %APP_DEST%\index.html
  pause
  exit /b 1
)
if not exist "%APP_DEST%\iniciar-windows-red.bat" (
  echo.
  echo No se encontro el iniciador local: iniciar-windows-red.bat
  pause
  exit /b 1
)
powershell -NoProfile -ExecutionPolicy Bypass -Command "$ErrorActionPreference='Stop'; $app=$env:APP_TITLE; $dest=$env:APP_DEST; $startup=$env:STARTUP; $desktop=$env:DESKTOP; $launcher=Join-Path $dest 'iniciar-windows-red.bat'; $icon=Join-Path $dest 'icons\icon-v34-192.png'; $ws=New-Object -ComObject WScript.Shell; if(Test-Path $startup){Get-ChildItem $startup -Filter '*.lnk' -ErrorAction SilentlyContinue | ForEach-Object {try{$lnk=$ws.CreateShortcut($_.FullName); $raw=($_.Name + ' ' + $lnk.TargetPath + ' ' + $lnk.Arguments + ' ' + $lnk.WorkingDirectory)}catch{$raw=$_.Name}; if($raw -match '(?i)(localhost|127\.0\.0\.1|behost|comedor)'){Remove-Item $_.FullName -Force -ErrorAction SilentlyContinue}}}; [void](New-Item -ItemType Directory -Path $startup -Force); $deskPath=Join-Path $desktop ($app + '.lnk'); $s=$ws.CreateShortcut($deskPath); $s.TargetPath=$launcher; $s.WorkingDirectory=$dest; $s.Arguments=''; $s.WindowStyle=7; if(Test-Path $icon){$s.IconLocation=$icon}; $s.Save(); $startPath=Join-Path $startup ($app + '.lnk'); $s2=$ws.CreateShortcut($startPath); $s2.TargetPath=$launcher; $s2.WorkingDirectory=$dest; $s2.Arguments='startup'; $s2.WindowStyle=7; if(Test-Path $icon){$s2.IconLocation=$icon}; $s2.Save()"
if errorlevel 1 (
  echo No se pudo crear el acceso directo de arranque.
  pause
  exit /b 1
)
echo.
echo Listo. Comedor Point Red quedo instalado localmente.
echo Se limpio el arranque anterior y se creo un inicio limpio para Windows.
echo Al reiniciar Windows abrira el sistema local con perfil propio de Chrome.
echo Para impresion directa deja la impresora 80mm como predeterminada.
echo.
call "%APP_DEST%\iniciar-windows-red.bat"
pause
endlocal
