unit ant_args;

//
//  Parses command line arguments (and maybe environment variables).
//  Affects application-wise behaviour. It may even change
//  path to the config.
//

{$MODE FPC}
{$MODESWITCH DEFAULTPARAMETERS}
{$MODESWITCH OUT}
{$MODESWITCH RESULT}

interface

uses
  dstrnum,
  dansiview,
  deng;

var
CommandLineArgs : record
  ConfigPath: AnsiString;
  Lang: AnsiString;
  FullScreen: Int32;
  Width: Int32;
  Height: Int32;
  OnlyHelp: Boolean;
  PrintStaticStats: Boolean;
  DryRun: Boolean;
end;

procedure SetDefaultArgs;
function ParseCommandLine: Boolean;
procedure PrintArgsHelp;

implementation

procedure SetDefaultArgs;
begin
  CommandLineArgs.ConfigPath := 'profile/config.json';
  CommandLineArgs.Lang := '';
  CommandLineArgs.FullScreen := -1;
  CommandLineArgs.Width := -1;
  CommandLineArgs.Height := -1;
  CommandLineArgs.OnlyHelp := False;
  CommandLineArgs.PrintStaticStats := False;
  CommandLineArgs.DryRun := False;
end;

function ExtractStr(var I: Int32; var Argument: AnsiString): Boolean;
begin
  Inc(I);
  if I > ParamCount then begin
    Log('failed parsing command line: expected argument for ' + ParamStr(I - 1));
    Exit(False);
  end;
  Argument := ParamStr(I);
  Exit(True);
end;

function ExtractI32(var I: Int32; var Argument: Int32): Boolean;
begin
  Inc(I);
  if I > ParamCount then begin
    Log('failed parsing command line: expected argument for ' + ParamStr(I - 1));
    Exit(False);
  end;
  Argument := StrToInt(ParamStr(I));
  Exit(True);
end;

function ParseCommandLine: Boolean;
var
  I: Int32;
  Arg: AnsiString;
begin
  I := 1;
  while I <= ParamCount do begin
    Arg := ParamStr(I);
    if (Arg = '--help')  or (Arg = '-h')
    or (Arg = '--usage') or (Arg = '-?') then begin
      CommandLineArgs.OnlyHelp := True;
    end else if Arg = '--dry-run'then begin
      CommandLineArgs.DryRun := True;
    end else if Arg = '--static-stats'then begin
      CommandLineArgs.PrintStaticStats := True;
    end else if (Arg = '--config') or (Arg = '-c') then begin
      if not ExtractStr(I, CommandLineArgs.ConfigPath) then
        Exit(False);
    end else if (Arg = '--lang') then begin
      if not ExtractStr(I, CommandLineArgs.Lang) then
        Exit(False);
    end else if Arg = '--fullscreen' then begin
      CommandLineArgs.FullScreen := 1;
    end else if Arg = '--windowed' then begin
      CommandLineArgs.FullScreen := 0;
    end else if (Arg = '--width') or (Arg = '-w') then begin
      if not ExtractI32(I, CommandLineArgs.Width) then
        Exit(False);
    end else if (Arg = '--height') or (Arg = '-h') then begin
      if not ExtractI32(I, CommandLineArgs.Height) then
        Exit(False);
    end else if Arg = '--' then begin
      if I < ParamCount then begin
        Log('failed parsing command line: no arguments allowed after --');
        Exit(False);
      end;
    end else begin
      Log('failed parsing command line: unknown option ' + Arg);
      Exit(False);
    end;
    Inc(I);
  end;
  Exit(True);
end;

procedure PrintArgsHelp;
begin
  Log('Usage: ant.exe [arg]*');
  Log('    for list of possible args see ParseCommandLine in ant_args.pas :)');
end;

initialization
  SetDefaultArgs;
end.
