// -*- ASCII:EDT -*-
// TeX System.edt
// TeX Live System
// Last modified on:
//  Tuesday, December 6, 2016  10:34

  Requires(20161103); // Requires this build of WinEdt to work properly

  PushTagsandRegisters;

  SetOK(1);
  SetErrorFlag(0);

Assign('TeX System','TeX Live');
SetCPPErr(0,1,"");    // C-style Errors: Off

// Auto-Detect TeX System
SetEnvVar("PDFLATEX",!"pdflatex.exe"); // Just in case
SetEnvVar("LATEX",!"latex.exe");       // Just in case
Exe('%b\Exec\TeX Live\ExeModeAutoDetect.edt'); // Current Auto Detection Settings

// Original PATH
SetEnvVar('PATH','%$(`xPATH`)'); // The Original Local PATH
Assign("TeX-PATH");

// TeX Root =========================================================

LetReg(3,'\bin\win32');
IfFileExists("%$('TeX-Root');\bin\win64\tex.exe",!"LetReg(3,'\bin\win64');");

IfStr("%$('TeXAuto-Root');","0","=","JMP('no_Auto-Root');");

// Search for tex.exe or tlmgr.bat or dviout.exe
// to see if TeXLive\Bin is already on your PATH
  Assign("TeX-Root");
  SearchForFile(0,'tex.exe');
  IfOK("Relax;","LetReg(0);");
  FindInString('%!0','\(0*\)\(1\\bin\\win*\)\\tex\.exe',1,2,1,0);
  IfOK(!"LetReg(0,'%#0');",!"LetReg(0);");
  Assign("TeX-Root","%!0");
  LetReg(3,'%#1');
  IfFileExists("%$('TeX-Root');%!3\tex.exe","JMP('no_Auto-Root');");
  Assign("TeX-Root");
  SearchForFile(0,'tlmgr.bat');
  IfOK("Relax;","LetReg(0);");
  FindInString('%!0','\(0*\)\(1\\bin\\win*\)\\tlmgr\.bat',1,2,1,0);
  IfOK(!"LetReg(0,'%#0');",!"LetReg(0);");
  Assign("TeX-Root","%!0");
  LetReg(3,'%#1');
  IfFileExists("%$('TeX-Root');%!3\tex.exe","JMP('no_Auto-Root');");
  Assign("TeX-Root");
  SearchForFile(0,'dviout.exe');
  IfOK("Relax;","LetReg(0);");
  FindInString('%!0','\(0*\)\(1\\bin\\win*\)\\dviout\.exe',1,2,1,0);
  IfOK(!"LetReg(0,'%#0');",!"LetReg(0);");
  Assign("TeX-Root","%!0");
  LetReg(3,'%#1');
  IfFileExists("%$('TeX-Root');%!3\tex.exe","JMP('no_Auto-Root');");
  Assign("TeX-Root");
  LetReg(3,'\bin\win32');

  // Oh well: where is TeX Live!
  Assign("TeX-Root","%H:\TeXLive");

:no_Auto-Root:: =====================================================

IfStr("%$('TeXAuto-Bin');",!"0",!"<>",>
  !|Assign(!"TeX-Bin","%$(!'TeX-Root');%!3");|,>
  !|Assign(!"TeX-Bin","%$(!'TeX-BinDef');");|);

IfStr("%$('TeXAuto-Help');",!"0",!"<>",>
  !|Assign(!"TeX-Help","%$(!'TeX-Root');\texmf\doc\texlive");>
    IfFileExists("%$(!'TeX-Help');\",!"Relax;",>
      !"Assign(!`TeX-Help`,`%$(!'TeX-Root');\texmf-dist\doc\texlive`);");|,>
  !|Assign(!"TeX-Help","%$(!'TeX-HelpDef');");|);

IfStr("%$('TeXAuto-DVI');",!"0",!"<>",>
  !|Assign(!"DVIView",!"dviout.exe");|,>
  !|Assign(!"DVIView","%$(!'DVIViewDef');");|);

Assign(!"TeX-Local",@@"%$(!'TeX-LocalDef');");

// ==================================================================

  // Insert TeX Live to the beginning of the PATH (if required)...
  SearchForFile(0,'tex.exe'); // Bin folder on PATH?
  IfOK("Relax;","LetReg(0);");
  IfIStr('%!0','%$("TeX-Bin");\tex.exe','=','JMP("PATH_OK");');

  SearchForFile(0,'tex.exe','%$("TeX-Bin");');
  IfOK("Relax;","LetReg(0);");
  IfIStr('%!0','%$("TeX-Bin");\tex.exe','<>','JMP("PATH_OK");');

  IfFileExists("%$('TeX-Bin');\",!|Assign("TeX-PATH","%$('TeX-Bin')");|); // TeX Bin

:PATH_OK:: ==========================================================

// "e" Response with Environment Variable:
  SetEnvVar('TEXEDIT', 'WinEdt.exe -C="%!W" "[Open(|%%s|);SelPar(%%d,7);]"');
  SetEnvVar('MFEDIT', 'WinEdt.exe -C="%!W" "[Open(|%%s|);SelPar(%%d,7);]"');
  SetEnvVar('MPEDIT', 'WinEdt.exe -C="%!W" "[Open(|%%s|);SelPar(%%d,7);]"');

  Exe('%b\Config\PATH.edt');
  Exe('%b\Config\EnvVar.edt');
  Exe('%b\Config\PATH.edt');
  Exe('%b\Config\PDF.edt');
  Exe('%b\Config\GS.edt');
  Exe('%b\Config\PATH.edt');

  PopTagsandRegisters;

  SetOK(1);
  SetErrorFlag(0);

End;
