/* monx-dirattn.hh -- move around the directories and keep some track of
   where you've been... this is based on VRP theory, essentially.

   $Log: monx-dirattn.hh,v $
   Revision 1.1  1999/12/27 23:48:46  joanna
   Initial revision

   Revision 1.3  1997/09/10 01:36:43  joanna
   works with robs stuff -- fixed loads more chaining problems,
   new rob libraries.  Interactive rob ringrose, not piped yet.

 */

#include <stdio.h>
#include <sys/param.h>
#include <fstream.h>

extern "C" {
  char *getwd(char *pathname);
}

class Dirattn {
public:
  inline Dirattn(char * pathname);
  ~Dirattn(){};
  
  static lapres cd_attn() {
    if (chdir(attn)) return (SUCCESS); else return (FAILURE);} ;
  inline static lapres attn_to_dir();
  static bool attend_dir() {
    if (attn && attn[0]) return (SUCCESS); else return (FAILURE);};

private:
  static char * attn;
  static Dirattn *directories;
  Dirattn *next;
  int nextdir;
  char name[MAXPATHLEN];
  char dirs[16][MAXPATHLEN];
};

inline Dirattn::Dirattn(char *pathname) {
  strcpy (name, pathname);
  system ("ls -F | egrep / > /tmp/listing.dirattn");
  ifstream listing("/tmp/listing.dirattn");
  int iii;
  for (iii = 0; (iii < 15) && (listing >> dirs[iii]); iii++);
  dirs[iii][0] = 0;
  listing.close();
  nextdir = 0;
}

inline lapres Dirattn::attn_to_dir() {
  static char pathname[MAXPATHLEN];
  
  getwd(pathname);
  Dirattn *here;
  for (here = directories; here; here = here->next) 
    if (!strcmp(pathname, here->name))
      break;
  if (!here) 
    here = new Dirattn(pathname);
  attn = here->dirs[here->nextdir];
  cerr << " attending to \n" << pathname;
  if (attn[0]) // may be more dirs
    here->nextdir++;
  else
    attn = 0;
  return (SUCCESS);  // check result using attend_dir
  }
    

