program clijasshelper;
(*$APPTYPE CONSOLE *)    

{$R 'grammar.res' 'grammar.rc'}

uses
  SysUtils,
  Windows,
  Tlhelp32,
  jasshelperConfigFile,
  vxFilesys,
  Preprocessor,
  Structs,
  JASSerException,
  ExternalUsage,
  Constants,
  JHInterface,
  JHParams,
  Inliner,
  ReturnFix,
  ShadowHelper,
  ImportPaths,
  cUnit in 'cUnit.pas',
  clierrors in 'clierrors.pas',
  jhErrors in 'jhErrors.pas',
  storm in 'storm.pas',
  winexec in 'winexec.pas';

{$R *.res}



//==============================================================================
procedure ConsoleDoAbout;
begin
   WriteLn('Jasshelper-cli '+Constants.VERSION);
end;

//==============================================================================
procedure ConsoleHandleException(const prefix:string; const e:Exception);
begin
  Writeln(prefix);
  Writeln(e.message);
  Halt(1);
end;

//==============================================================================
procedure RedirectToFileIfNoConsole();
begin
  try
      Write('');
  except on e:EInOutError  do
    begin
      AssignFile(output, 'stdout.txt');
      Rewrite(output);
    end;
  end; // end of try
end;

//==============================================================================
//  BEGIN
//==============================================================================
begin
  try
    //-------------------------------------------
		RedirectToFileIfNoConsole();
		cUnit.DoAbout := ConsoleDoAbout;
		//cUnit.DoRequire := ConsoleDoRequire;
		jhErrors.HandleException := ConsoleHandleException;

    cUnit.Main();  // <--------<<
    //-------------------------------------------
  except on e:Exception do
    begin
      jhErrors.HandleException('Unexpected Code Exception!', e);
    end;
  end;  // end of try
end.
