#include <djUser.h>
#include <djDir.h>
#include <djAppBase.h>
#include <stdio.h>

using namespace dj;

class App : public AppBase {
public:
  App(int & argc, char ** argv);
};

App::App(int & argc, char ** argv) :
  AppBase("dir", argc, argv)
{
  if (argc != 2) {
    print("Usage: dir (path)");
    exitValue(1);
    return;
  }

do {

  Dir dir(argv[1]);
  List<FileInfo> itemList = dir.itemList();
  itemList = seq(itemList);
  
  for (ListIt<FileInfo> it = itemList; it.isValid(); ++it)
    print(
      Str("%1 %2 %3MB %4 %5 %6").
        arg(it().get(-1, false), 55 - terminalWidth()).
        arg(fileTypeStr()[it().type()], -4).
        arg(it().size() / double(megabyte), 8, 2).
        arg(userToStr(it().user()), -8).
        arg(filePermStr(it().perm()), -3).
        arg(timeToStr(it().time()), -24)
    );

} while (0);
}

int main(int argc, char ** argv)
{
  App app(argc, argv);
  return app.exec();
}
