@echo off
call check-for-admin
if %ERRORLEVEL% neq 0 exit /b

set CONFIG=config\install-vscode.txt
set EXE=..\software\vscode-setup.exe
set MARKDOWNTABLE=..\software\markdowntable.vsix
set STARTMENU="%PROGRAMDATA%\Microsoft\Windows\Start Menu\Programs"

if NOT EXIST %EXE% (
  echo ERROR: %EXE% not found!
  exit /b
)

echo ####### %0 #######

echo killing VisualStudio code processes...
taskkill /F /IM Code.exe 2>nul

echo cleanup ...
del /F /Q "%PUBLIC%\Desktop\VScode.lnk" 2>nul

echo installing Visual Studio CODE ...
start /wait %EXE% /LOADINF=%CONFIG% /NORESTART /NOCANCEL /SILENT /SUPPRESSMSGBOXES /CLOSEAPPLICATIONS

echo installing Desktop shortcut ...
copy /Y %STARTMENU%\"Visual Studio Code\Visual*.lnk" "%PUBLIC%\Desktop"
move /Y "%PUBLIC%\Desktop\Visual*.lnk" "%PUBLIC%\Desktop\VScode.lnk"

rem refresh desktop (W10 style)
ie4uinit.exe -show

echo moving VScode startmenu shortcut ...
move /Y %STARTMENU%\"Visual Studio Code\Visual*.lnk" %STARTMENU%

echo removing VScode startmenu folder ...
rd /S /Q %STARTMENU%\"Visual Studio Code" 2>nul

echo ####### %0 #######

rem set filetypes ...
if EXIST ..\scripts\set-filetypes.cmd (
  call ..\scripts\set-filetypes.cmd
)

rem IMPORTANT! THIS must be the last action
rem - for some reason stupid VScode is killing the script after executing ...

echo adding VScode MARKDOWNTABLE plugin ...
"%ProgramFiles%\Microsoft VS Code\bin\code" ^
  --extensions-dir "%ProgramFiles%\Microsoft VS Code\resources\app\extensions" ^
  --install-extension %MARKDOWNTABLE% --force