program ZMLookup;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  SysUtils, Classes, StrUtils, Types,
  ZaphodsMap,
  ucVers, ucOther, uCode;

type
  TQuickXPath = array of TVarRec;

var
  ZM: TZaphodsMap;
  ZaphodBranch, AKeyGroupName, AKeyName, AXPath, AAttribID: string;
  AKeyedFileRootName: string;
  DefaultFilespec, DefaultFolderName, DefaultValue: string;
  Warning: string;
  KeyedDetail: TKeyedDetail;
  S: string;
  WrongUsage: Boolean;
  i: Integer;
const
  cUsage1 = 'ZMLookup.exe /Key2Folder HREFTools\Production\cv001 Delphi D07 ' +
    'c:\temp\ /debug';
  cUsage2 = '/Key2Folder ZaphodBranch AKeyGroupName AKeyName DefaultFolderName';
  cUsage3 = '/Key2Value  ZaphodBranch AKeyGroupName AKeyName DefaultValue';
  cUsage4 = '/Key2File   ZaphodBranch AKeyGroupName AKeyName DefaultFilespec';
  cUsage5 = '/KeyedFileZNodeAttr ' +
                         'ZaphodBranch AKeyGroupName AKeyName ' +
                         'KeyedFileRootName XPath AttribID DefaultValue' +
                         ' where XPath example is "[a, b, c, @d, e]';
  cUsage6 = '/DefaultContext';

  procedure DebugVerbose;
  var
    i: Integer;
  begin
    for i := 1 to ParamCount do
      WriteLn(Format('Parameter #%d = %s', [i, ParamStr(i)]));
    WriteLn(KeyedDetail.ErrorMessage);
  end;

begin

  WrongUsage := True;
  ZM := nil;

  if HaveParam('/DefaultContext') and (ParamCount = 1) then
  begin
    WrongUsage := False;
    try
      ZM := TZaphodsMap.Create(nil);
      WriteLn(ZM.DefaultMapContext);
    finally
      FreeAndNil(ZM);
    end;
  end
  else
  if HaveParam('/Key2Folder') then
  begin
    if (ParamCount >= 4) then
    begin
      ZaphodBranch := ParamStr(2);
      AKeyGroupName := ParamStr(3);
      AKeyName := ParamStr(4);
      if ParamCount >= 5 then
        DefaultFolderName := ParamStr(5)
      else
        DefaultFolderName := '';
      S := ZaphodKeyedFolder(ZaphodBranch, AKeyGroupName, AKeyName, cxOptional,
        usrNone, DefaultFolderName, KeyedDetail);
      if KeyedDetail.ExceptionOccurred then
      begin
        WriteLn('Exception occurred');
        WriteLn(KeyedDetail.ErrorMessage);
      end
      else
      begin
        WrongUsage := False;
        WriteLn(S);
        if HaveParam('/debug') then DebugVerbose;
      end;
    end;

  end
  else
  if HaveParam('/Key2Value') then
  begin
    if (ParamCount >= 4) then
    begin
      ZaphodBranch := ParamStr(2);
      AKeyGroupName := ParamStr(3);
      AKeyName := ParamStr(4);
      if ParamCount >= 5 then
        DefaultValue := ParamStr(5)
      else
        DefaultValue := '';
      S := ZaphodKeyedValue(ZaphodBranch, AKeyGroupName, AKeyName, cxOptional,
        usrNone, DefaultValue, KeyedDetail);
      if KeyedDetail.ExceptionOccurred then
      begin
        WriteLn('Exception occurred');
        WriteLn(KeyedDetail.ErrorMessage);
      end
      else
      begin
        WrongUsage := False;
        WriteLn(S);
        if HaveParam('/debug') then DebugVerbose;
      end;
    end
    else
  end
  else
  if HaveParam('/Key2File') then
  begin
    if (ParamCount >= 4) then
    begin
      ZaphodBranch := ParamStr(2);
      AKeyGroupName := ParamStr(3);
      AKeyName := ParamStr(4);
      if ParamCount >= 5 then
        DefaultFilespec := ParamStr(5)
      else
        DefaultFilespec := '';
      S := ZaphodKeyedFileName(ZaphodBranch, AKeyGroupName, AKeyName,
        cxOptional, usrNone, DefaultFilespec, KeyedDetail);
      if KeyedDetail.ExceptionOccurred then
      begin
        WriteLn('Exception occurred');
        WriteLn(KeyedDetail.ErrorMessage);
      end
      else
      begin
        WrongUsage := False;
        WriteLn(S);
        if HaveParam('/debug') then DebugVerbose;
      end;
    end;
  end
  else
  if HaveParam('/KeyedFileZNodeAttr') then
  begin
    if (ParamCount >= 8) then  // required DefaultValue
    begin
      ZaphodBranch := ParamStr(2);
      AKeyGroupName := ParamStr(3);
      AKeyName := ParamStr(4);
      AKeyedFileRootName := ParamStr(5);


      {compensate for = symbol being turned to space in BAT files
       Reference: http://support.microsoft.com/kb/35938}
      for i := 6 to ParamCount - 2 do
      begin
        if i > 6 then
          AXPath := AXPath + ' ' + ParamStr(i)
        else
          AXPath := ParamStr(i);
      end;
      AXPath := StringReplace(AXPath, ' ', '=', [rfReplaceAll]);

      {compensate for single quote being used in BAT files}
      AXPath := StringReplace(AXPath, '''', '"', [rfReplaceAll]);

      {AttributeID is always the second-to-last parameter}
      AAttribID := ParamStr(ParamCount-1);

      {DefaultValue is always the last parameter}
      DefaultValue := ParamStr(ParamCount);

      S := ZaphodKeyedFileZNodeAttr(ZaphodBranch, AKeyGroupName, AKeyName,
        AKeyedFileRootName, [AXPath], cxOptional, usrNone,
        AAttribID, DefaultValue, Warning);
      if Warning <> '' then
      begin
        WriteLn('Warning: ' + Warning);
      end
      else
      begin
        WrongUsage := False;
        WriteLn(S);
      end;
    end;
  end;
  if WrongUsage then
  begin
    WriteLn('ZMLookup v' + GetVersionDigits(False));
    WriteLn('Pascal; Open Source at www.ZaphodsMap.com; ' +
      'Creative Commons License.');
    WriteLn(Format('Copyright (c) 2010-%s HREF Tools Corp.',
      [FormatDateTime('yyyy', Now)]));
    WriteLn('');
    WriteLn(cUsage1);
    WriteLn(cUsage2);
    WriteLn(cUsage3);
    WriteLn(cUsage4);
    WriteLn(cUsage5);
    WriteLn(cUsage6);
  end;
end.
