#!/root/clam/clam
#include "stdlib.h"
#include "stat.h"

main(list argv)
{
  fd = open("/etc/passwd",O_RDONLY);

  if (fd < 0) {
    print("Unable to open /etc/password for reading\n");
    exit(1);
  }
  while(line = gets(fd)) {
    x = line / ":";
    if (x[0] == argv[1]) {
      x[4] /= ",";
      output(x);
    }
  }
}

# Login: ice                              Name: Steve Baker
# Directory: /u2/h0/ice                   Shell: /bin/tcsh
# Office: Normal Hall 301, x8361          Home Phone: 231-1748

output(list u)
{
  [user, pwd, uid, gid, gcos, home, shell] = u;
  if (sizeof(gcos) != 0)
    [name, office, ophone, hphone] = gcos;
  else
    [name, office, ophone, hphone] = "";
  name = sizeof(name) ? name : "(null)";

  print('Login: %{user:-32s} Name: %{name}\n');
  print('Directory: %{home:-28s} Shell: %{shell}\n');
  if (sizeof(gcos) != 0) {
    if (sizeof(office) && sizeof(ophone))
       print('Office: %{office+", "+ophone:-31s} ');
    else if (sizeof(office))
       print('Office: %{office:-31s} ');
    else if (sizeof(ophone))
       print('Office: %{ophone:-31s} ');

    if (sizeof(hphone))
      print('Home Phone: %{hphone}\n');
    else if (sizeof(office) || sizeof(ophone)) print("\n");
  }
  mail(user,home);
  plan(home);
}

mail(str user, str home)
{
  p = open(home + "/.forward", O_RDONLY);
  if (p > -1) {
    print("Mail forwarded to ");
    do {
      s = gets(p);
      if (!sizeof(s)) continue;
      print(s,"\n");
    } while(!eof(p));
  }
  st = stat("/var/spool/mail/" + user);
  if (typeof(st) == DT_LIST) {
    if (st[ST_MTIME] > st[ST_ATIME]) {
      print("New mail received ", ctime(st[ST_MTIME]));
      print("     Unread since ", ctime(st[ST_ATIME]));
    } else print("Mail last read ", ctime(st[ST_ATIME]));
  } else print("No mail.\n");
}

plan(str home)
{
  p = open(home + "/.plan", O_RDONLY);
  if (p > -1) {
    print("Plan:\n");
    do {
      print(gets(p),"\n");
    } while (!eof(p));
    close(p);
  } else print("No Plan.\n");
}
