program consolejasser;

{$APPTYPE CONSOLE}

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

uses
  SysUtils,
  Windows,
  jasshelperconfigfile in 'jasshelperconfigfile.pas',
  jasshelpersymbols in 'jasshelpersymbols.pas',
  slk in 'slk.pas',
  zincparser in 'stage1\zincparser.pas',
  ZincSymbols in 'stage1\ZincSymbols.pas',
  Preprocessor in 'stage1\Preprocessor.pas',
  ScopeStack in 'jhobjects\ScopeStack.pas',
  JASSerException in 'jhobjects\JASSerException.pas',
  Member in 'jhobjects\Member.pas',
  TextMacro in 'stage1\TextMacro.pas',
  TextMacroException in 'stage1\TextMacroException.pas',
  JFunction in 'jhobjects\JFunction.pas',
  JStruct in 'jhobjects\JStruct.pas',
  ExternalUsage in 'stage1\ExternalUsage.pas',
  JHInterface in 'jhobjects\JHInterface.pas',
  JHParams in 'JHParams.pas',
  JType in 'jhobjects\JType.pas',
  Zinc in 'stage1\Zinc.pas',
  ImportPaths in 'ImportPaths.pas',
  PreprocessorImport in 'stage1\PreprocessorImport.pas',
  JHInput in 'JHInput.pas',
  TempUnit in 'stage2\TempUnit.pas',
  Structs in 'stage2\Structs.pas',
  StructVariables in 'stage2\StructVariables.pas',
  ModuleMagic in 'stage2\ModuleMagic.pas',
  Prototypes in 'stage2\Prototypes.pas',
  BlockMagic in 'stage2\BlockMagic.pas',
  Structs00 in 'stage2\Structs00.pas',
  Structs01 in 'stage2\Structs01.pas',
  Structs02 in 'stage2\Structs02.pas',
  Translate in 'stage2\Translate.pas',
  Inliner in 'stage3\Inliner.pas',
  ReturnFix in 'stage3\ReturnFix.pas',
  ShadowHelper in 'stage3\ShadowHelper.pas',
  jasslib in 'stage1\jasslib.pas',
  StructsSLK in 'stage2\StructsSLK.pas',
  InitializerFunctions in 'InitializerFunctions.pas',
  Constants in 'common\Constants.pas',
  PseudoRandom in 'common\PseudoRandom.pas',
  StringHash in 'common\StringHash.pas',
  TextCommon in 'common\TextCommon.pas',
  TextOnly in 'common\TextOnly.pas',
  vxFilesys in 'common\vxFilesys.pas',
  LibrariesAndScopes in 'stage1\LibrariesAndScopes.pas';

//==============================================================================
//  GLOBALS
//==============================================================================
var
   inputFile:string='';
   outputFile:string='';
   folder:string;

const
  TEMP00 = 'temp\temp00.j';
  TEMP01 = 'temp\temp01.j';
  TEMP02 = 'temp\temp02.j';
  TEMP03 = 'temp\temp03.j';
  TEMP04 = 'temp\temp04.j';
  TEMP05 = 'temp\temp05.j';

//==============================================================================
//  PROCEDURES
//==============================================================================
procedure parseCommandLine;
var temi: integer;
begin
  temi:=1;

  if (ParamStr(temi)='') or (ParamStr(temi+1)='') then begin
      WriteLn('consolejasser.exe input.j output.j');
      Halt(2);
  end;

  inputFile:=ParamStr(temi);
  outputFile:=ParamStr(temi+1);
end;



//==============================================================================
procedure InitJasshelper;
begin
  if (not FileExists(GetCommonJ())) then begin
     raise Exception.Create('File does not exist : '+GetCommonJ());
  end;
  if (not FileExists(GetBlizzardJ())) then begin
    raise Exception.Create('File does not exist : '+GetBlizzardJ());
  end;
  folder := AppendBackslash(ExtractFileDir(inputFile));

  //ensures map's path as an import search path
  importPathsClear;
  JasshelperConfigFile.WORK_PATHS:=folder;
  addImportPath(folder);

     //readConfig(jasshelperConfPlacements() );
     //AUTOMETHODEVALUATE:= jasshelperConfigFile.AUTO_METHOD_EVALUATE;
end;

//==============================================================================
procedure TimeStatus(var tick:Cardinal);
begin
  ProStatus('  Time: '+IntToStr(GetTickCount()-tick));
  tick := GetTickCount();
end;

//==============================================================================
procedure doJassStuff;
var tick:Cardinal;
    source: string;
begin
  tick := GetTickCount();

  source := LoadFile(inputFile);        SaveFile(TEMP00, source);

  source := Preprocessor.Run(source);   SaveFile(TEMP01, source);

  source := Structs.Run(source);        SaveFile(TEMP02, source);

  source := ReturnFix.Run(source);      SaveFile(TEMP03, source);

  source := ShadowHelper.Run(source);   SaveFile(TEMP04, source);

  source := Inliner.Run(source);        SaveFile(TEMP05, source);

  SaveFile(outputFile, source);

  ProStatus('ALL OK.');
  TimeStatus(tick);
end;

//==============================================================================
procedure doJassStuffSafe;
begin
  try
     InitJasshelper();
     doJassStuff();
  except on e:TJASSerException do
  begin
     WriteLn('Line '+IntToStr(e.linen+1)+': '+e.msg);
     if(e.two) then begin
       WriteLn('Line '+IntToStr(e.linen2+1)+': '+e.msg2);
     end;
     if(e.macro1>=0) then begin
       WriteLn('Line '+IntToStr(e.macro1+1)+': (From this macro instance)');
     end;
     if(e.macro2>=0) and(e.macro2<>e.macro1) then begin
       WriteLn('Line '+IntToStr(e.macro2+1)+': (From this macro instance)');
     end;
     Halt(1);
  end;
  end;
end;

//==============================================================================
//  MAIN
//==============================================================================
//var tempString: string;
//    tempInteger: integer;

begin
  try
    TurnOn_DEBUG_MODE();  // we do all testing in debug mode.
    parseCommandLine();
    doJassStuffSafe();
    //GetLineWord('///aaa bbb ccc', tempString, tempInteger);
    //WriteLn(tempString);
  except on e:Exception do begin
    WriteLn('Error: '+e.Message);
  end;
  end;
end.

