#!/usr/bin/env instantfpc

{
  Copyright 2020-2022 Michalis Kamburelis.

  This file is part of "Castle Game Engine".

  "Castle Game Engine" is free software; see the file COPYING.txt,
  included in this distribution, for details about the copyright.

  "Castle Game Engine" is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  ----------------------------------------------------------------------------
}

{ List all files matching given mask (like * or *.pas) in current directory
  and subdirectories.

  To make this work,
  - install FPC, which should include the instantfpc binary
  - install CGE using FpMake/FpPkg following https://castle-engine.io/fpmake

  Then run like
    ./castle_list_files
    ./castle_list_files '../../src/base/*'
    ./castle_list_files '../../src/base/*.pas'

  Note that above examples quote parameter in apostrophes, so that castle_list_files
  expands the mask, not the shell. You can also let shell expand the mask (it will then
  pass multiple parameters to this application):
    ./castle_list_files ../../src/base/*
    ./castle_list_files ../../src/base/*.pas
}

uses SysUtils,
  CastleParameters, CastleFindFiles;

procedure FileCallback(const FileInfo: TFileInfo; Data: Pointer; var StopSearch: boolean);
begin
  Writeln(FileInfo.URL);
end;

procedure Find(const PathAndMask: String);
var
  FilesCount: Cardinal;
begin
  FilesCount := FindFiles(PathAndMask, false, @FileCallback, nil, [ffRecursive]);
  Writeln(Format('Found %d files matching "%s"', [
    FilesCount,
    PathAndMask
  ]));
end;

var
  I: Integer;
begin
  if Parameters.High = 0 then
    Find('*')
  else
  for I := 1 to Parameters.High do
    Find(Parameters[I]);
end.
