{
    +------------------------------------------------------+
    |NFD (Newest File Data) © (copyright) 1991 Jon Maxwell |
    +------------------------------------------------------+
    |This program may be freely distributed, but I reserve |
    |all rights to it and to the source code.              |
    +------------------------------------------------------+
    |USAGE: NFD [±Pattern] [Directory] [Command]           |
    |  [Command] is the line to be run with the newest file|
    |name, in this form: (Text)¿(Text) where (Text) is part|
    |of the command and ¿ (shift-alt-M) determines where   |
    |the file name will be inserted.                       |
    |  a + pattern means that it must match that pattern, a|
    |- means that it must not match.                       |
    +------------------------------------------------------+
    |  If there is no [Command] parameter, NFD will assume |
    |that you have ARP and will set an ARP global variable |
    |under the name of $FileName.                          |
    +------------------------------------------------------+
    |Source compiled with Patrick Quaid's PCQ              |
    +------------------------------------------------------+
}
PROGRAM LastFileWrittenName (input,output);
{$I "Include:Libraries/DOS.i"}
{$I "Include:Utils/StringLib.i"}
{$I "Include:Utils/Parameters.i"}
VAR
  FIB:FileInfoBlockPtr;
  FL:FileLock;
  I,
  Pos,
  Minutes,
  PatternType,
  Days:Integer;
  BestDate:DateStampPtr;
  DirName,
  Pattern,
  BestName,
  FinalName,
  TempStr,
  CmdLine:String;
  QuoteMode,
  PatternResult:Boolean;


FUNCTION PatternCheck (Str,Pat:String):Boolean;
VAR
  StrPos,
  PatPos:Integer;
BEGIN
  StrPos:=0;
  PatPos:=0;
  WHILE StrPos<strlen(Str) DO BEGIN
    IF ToUpper(Str[StrPos])=ToUpper(Pat[PatPos]) THEN
        Inc(PatPos)
      ELSE
        PatPos:=0;
    IF PatPos=strlen(Pat) THEN PatternCheck:=TRUE;
    Inc(StrPos);
  END;
  PatternCheck:=FALSE;
END;


PROCEDURE StrCpyII(Dest,Source:String;Start,Finish:Integer);
VAR Loop:Integer;
    DPos:Integer;
BEGIN
   DPos:=0;
   FOR Loop:=Start TO Finish DO BEGIN
      Dest[DPos]:=Source[Loop];
      Inc(DPos);
   END;
   Dest[DPos]:=' ';
END;


PROCEDURE WriteInfo;
CONST
  NumLines=18;
  Lines:Array [1..NumLines] OF String=(
    "+-------------------------------------------------------+",
    "|NFD (Newest File Data) © (copyright) 1991 Jon Maxwell  |",
    "+-------------------------------------------------------+",
    "|This program may be freely distributed, but I reserve  |",
    "|all rights to it and to the source code.               |",
    "+-------------------------------------------------------+",
    "|USAGE: NFD [±Pattern] [Directory] [Command]            |",
    "|  [Command] is the line to be run with the newest file |",
    "|name, in this form: (Text)¿(Text) where (Text) is part |",
    "|of the command and ¿ (shift-alt-M) determines where    |",
    "|the file name will be inserted.                        |",
    "|  a + pattern means that it must match that pattern, a |",
    "|- means that it must not match.                        |",
    "+-------------------------------------------------------+",
    "|  If there is no [Command] parameter, NFD will assume  |",
    "|that you have ARP and will set an ARP global variable  |",
    "|under the name of $FileName.                           |",
    "+-------------------------------------------------------+");
VAR
  Loop:Integer;
BEGIN
  FOR Loop:=1 TO NumLines DO
    writeln(Lines[Loop]);
  Exit(0);
END;

BEGIN
  IF CommandLine[0]='?' THEN WriteInfo; {\              }
  PatternType:=0;                       { \             }
  BestName:=AllocString(200);           {  \            }
  DirName:=AllocString(200);            {   \           }
  FinalName:=AllocString(200);          {    »Sets the  }
  CmdLine:=AllocString(200);            {   / variables }
  new(FIB);new(BestDate);               {  /            }
  BestDate^.ds_Days:=0;                 { /             }
  BestDate^.ds_Minute:=0;               {/              }

  GetParam(1,CmdLine);
  IF (CmdLine[0]='+') OR (CmdLine[0]='-') THEN BEGIN
    Pattern:=AllocString(100);
    StrCpyII(Pattern,CmdLine,1,strlen(CmdLine));
    IF CmdLine[0]='+' THEN PatternType:=1 ELSE PatternType:=2;
    GetParam(2,CmdLine);
  END;
  strcpy(DirName,CmdLine);
  FL:=Lock(CmdLine,ACCESS_READ);
  IF FL=nil THEN BEGIN writeln("Couldn't get a lock on: ",CmdLine," directory!"); Exit(20); END;
  IF NOT Examine(FL,FIB) THEN BEGIN UnLock(FL); Exit(20); END;
  WHILE ExNext(FL,FIB)=TRUE DO BEGIN
    Days:=FIB^.fib_Date.ds_Days;
    Minutes:=FIB^.fib_Date.ds_Minute;
    PatternResult:=PatternCheck(string(@FIB^.fib_FileName[0]),Pattern);
    IF (FIB^.fib_DirEntryType<0) AND ((Days>BestDate^.ds_Days) OR ((Days=BestDate^.ds_Days) AND (Minutes>BestDate^.ds_Minute))) THEN
      IF (PatternType=0) OR ((PatternType=1) AND (PatternResult)) OR ((PatternType=2) AND NOT (PatternResult)) THEN BEGIN
           BestDate^.ds_Days:=Days;
           BestDate^.ds_Minute:=Minutes;
           strcpy(BestName,string(@FIB^.fib_FileName[0]) );
      END;
  END;
  strcat(FinalName,"FileName=");
  strcat(FinalName,DirName);
  I:=strlen(DirName)-1;
  IF (DirName[I]<>'/') AND (DirName[I]<>':') THEN strcat(FinalName,"/");
  strcat(FinalName,BestName);
  IF strlen(BestName)=0 THEN strcpy(FinalName,"!");
  IF PatternType=0 THEN GetParam(2,CmdLine) ELSE GetParam(3,CmdLine);
  IF CmdLine[0]=' ' THEN  {ARP Section: Defines variable $FileName as the filename}
    BEGIN IF Execute(FinalName,FileHandle(0),FileHandle(0)) THEN; Exit(0); END
   ELSE BEGIN
    IF strlen(BestName)=0 THEN BEGIN writeln("No match for the pattern or no files in the directory!"); Exit(0); END;
    TempStr:=AllocString(500);  {nonARP Section: Gets 2nd parameter &}
                                {makes an executable string with the }
    Pos:=strpos(CmdLine,'¿');   {Filename and the parameter          }
    QuoteMode:=FALSE;
    IF strpos(BestName,' ')>-1 THEN QuoteMode:=TRUE;{\ are there spaces?}
    IF strpos(DirName,' ')>-1 THEN QuoteMode:=TRUE;{/ in the filename ?}

    StrCpyII(FinalName,CmdLine,0,Pos-1);
    IF QuoteMode THEN StrCat(FinalName,"\"");
    StrCat(FinalName,DirName);
    I:=strlen(DirName)-1;
    IF (DirName[I]<>'/') AND (DirName[I]<>':') THEN strcat(FinalName,"/");
    StrCat(FinalName,BestName);
    IF QuoteMode THEN StrCat(FinalName,"\""); {\" puts a " in the string}
    StrCpyII(TempStr,CmdLine,Pos+1,strlen(CmdLine));
    StrCat(FinalName,TempStr);
    writeln(FinalName);
    IF Execute(FinalName,Filehandle(0),DOSOutput) THEN;
  END;
END.
