(*====================================================================
  =  Program Id:     EXISTDIR.MOD                                    =
  =  Programmer:     Peter M. Perchansky                             =
  =  Copyright:      Copyright (c) Peter M. Perchansky 1990          =
  =  Purpose:        Check for the existance of a given directory.   =
  =                  Sets DOS error level to 0 if directory exists;  =
  =                  otherwise it sets the error level to 1.         =
  =  Usage:          EXISTDIR <full path name of directory>          =
  =  Date Written:   06-14-90                                        =
  =  Memory Modle:   Small                                           =
  =------------------------------------------------------------------=
  =                       Version History                            =
  =------------------------------------------------------------------=
  =  06-14-90  PMP  Date written.                                    =
  ====================================================================*)

(*# check( stack=>on, index=>on, range=>on, overflow=>on, nil_ptr=>on) *)
(*# optimize( speed=>on, cse=>on, const=>on, peep_hole=>on, jump=>on,
              loop=>on, alias=>off, regass=>on, stk_frame=>on, i186=>on) *)

MODULE ExistDir;

(*------------- Procedures from JPI's TopSpeed Modula II -------------*)
FROM FIO    IMPORT DirEntry, directory, FileAttr, ReadFirstEntry, ReadNextEntry;
FROM FIOR   IMPORT SplitPath;
FROM Lib    IMPORT ParamCount, ParamStr, SetReturnCode;
FROM Str    IMPORT Append;
(*--------------------------------------------------------------------*)

(*------------- Procedures from PMP's Library Modules ----------------*)
FROM PMPStr IMPORT StrEqual;
(*--------------------------------------------------------------------*)

TYPE
    PathStr  = ARRAY [0..65] OF CHAR;
    Status   = (found, notfound);

VAR
    attribute  : FileAttr;               (* search attribute          *)
    entry      : DirEntry;               (* directory entry info      *)
    head,                                (* parent of dir to search   *)
    tail,                                (* dir name only             *)
    dir        : PathStr;                (* directory on command line *)
    existance  : Status;                 (* existance? of directory   *)
    errorLevel : SHORTCARD;              (* DOS error level to set    *)

BEGIN
    existance := notfound;

    IF ParamCount () = 1 THEN
       ParamStr (dir, 1);
       SplitPath (dir, head, tail);      (* get the parent dir - head *)
       Append (head, "\*.*");            (* all files *               *)
       attribute := FileAttr {directory};(* only directory & normal   *)

       IF ReadFirstEntry (head, attribute, entry) THEN
          REPEAT
               IF StrEqual(entry.Name, tail) AND (entry.attr = attribute) THEN
                  existance := found
               END
          UNTIL NOT ReadNextEntry (entry) OR (existance = found);
       END;
    END;

    errorLevel := SHORTCARD (ORD (existance));
    SetReturnCode (errorLevel);
END ExistDir.
