unit uCmdLineParams;

{$mode objfpc}{$H+}

interface

type
  TCommandLineParams = packed record
    NewTab: Boolean;
    NoSplash: Boolean;
    ActivePanelSpecified: Boolean;
    ActiveRight: Boolean;
    LeftPath: array[0..1023] of AnsiChar;
    RightPath: array[0..1023] of AnsiChar;
    ActivePanelPath: array[0..1023] of AnsiChar;
    Client: Boolean;
    Servername: array[0..1023] of AnsiChar;
  end;

procedure ProcessCommandLineParams;

var
  CommandLineParams: TCommandLineParams;

implementation

uses
  Forms, Dialogs, SysUtils, uOSUtils, uDCUtils, uGlobsPaths, getopts, uDebug, uLng;

procedure ProcessCommandLineParams;
var
  Option: AnsiChar = #0;
  OptionIndex: LongInt = 0;
  Options: array[1..5] of TOption;
  OptionUnknown: String;
begin
  FillChar(Options, SizeOf(Options), #0);
  with Options[1] do
  begin
    Name:= 'no-console';
  end;
  with Options[2] do
  begin
    Name:= 'config-dir';
    Has_arg:= 1;
  end;
  with Options[3] do
  begin
    Name:= 'client';
  end;
  with Options[4] do
  begin
    Name:= 'servername';
    Has_arg:= 1;
  end;
  with Options[5] do
  begin
    Name:= 'no-splash';
  end;
  FillChar(CommandLineParams, SizeOf(TCommandLineParams), #0);
  repeat
    try
      Option:= GetLongOpts('L:l:R:r:P:p:TtCc', @Options[1], OptionIndex);
    except
      MessageDlg(Application.Title, rsMsgInvalidCommandLine, mtError, [mbOK], 0, mbOK);
      Exit;
    end;
    case Option of
      #0:
        begin
          case OptionIndex of
            1:
              begin
                {$IF DEFINED(NIGHTLY_BUILD)}
                HideConsoleWindow;
                {$ENDIF}
              end;
            2:
              begin
                gpCmdLineCfgDir:= ParamStrU(TrimQuotes(OptArg));
              end;
            3:
              begin
                CommandLineParams.Client:= True;
                CommandLineParams.NoSplash:= True;
              end;
            4:
              begin
                CommandLineParams.Servername:= ParamStrU(TrimQuotes(OptArg));
              end;
            5:
              begin
                CommandLineParams.NoSplash:= True;
              end;
          end;
        end;
      'L', 'l': CommandLineParams.LeftPath:= ParamStrU(TrimQuotes(OptArg));
      'R', 'r': CommandLineParams.RightPath:= ParamStrU(TrimQuotes(OptArg));
      'P', 'p': begin
        CommandLineParams.ActivePanelSpecified:= True;
        CommandLineParams.ActiveRight:= (UpperCase(OptArg) = 'R');
      end;
      'T', 't': CommandLineParams.NewTab:= True;
      'C', 'c': begin
        CommandLineParams.Client:= True;
        CommandLineParams.NoSplash:= True;
      end;
      '?', ':': DCDebug ('Error with opt : ', OptOpt);
    end; { case }
  until Option = EndOfOptions;

  if OptInd <= ParamCount then
  begin
    // If also found one parameter then use it as path of active panel
    if ParamCount - OptInd = 0 then
      begin
        CommandLineParams.ActivePanelPath:= ParamStrU(OptInd);
        Inc(OptInd, 1);
      end
    // If also found two parameters then use it as paths in panels
    else if ParamCount - OptInd = 1 then
      begin
        CommandLineParams.LeftPath:= ParamStrU(OptInd);
        CommandLineParams.RightPath:= ParamStrU(OptInd + 1);
        Inc(OptInd, 2);
      end;
    // Unknown options, print to console
    if OptInd <= ParamCount then
    begin
      while OptInd <= ParamCount do
      begin
        OptionUnknown:= ParamStrU(OptInd) + ' ';
        Inc(OptInd)
      end;
      DCDebug ('Non options : ', OptionUnknown);
    end;
  end;
end;

end.
