#!/afs/athena/contrib/perl5/perl -w
# read (and write?) OLC backup.dat database files (really short form)

use strict;
use FileHandle;

sub read_str {
  my $str = '';
  read($_[0], $str, 16) or die "read(str): $!";
  $str;
}

sub read_user_info {
  my ($one, $two, $thr) = ('', '', '');
  read($_[0], $one, 401) or die "read_user_info(1): $!";
  read($_[0], $two, 40)  or die "read_user_info(2): $!";
  read($_[0], $thr, 24) or die "read_user_info(3): $!";
  return
    (unpack("Li A9 A64 A64 A64 A64 A64 A64", $one))[2..8],
    [unpack("i10", $two)],
    unpack("iiiiii", $thr);
  # username, realname, nickname, title1, title2, machine, realm,
  # [ specialties1..10 ], no_specialties, permissions, status,
  # no_knuckles, max_ask, max_answer

}

#my $P='';
#print length(pack("i10", (0)x10))."\n";
#exit 0;

my $fh = *STDIN;
my $type = '';

for ($type = read_str($fh);
     $type ne "  new data:    \000";
     $type = read_str($fh)) {
  
  if ($type eq "  new user:    \000") {
    my @info = read_user_info($fh);
    for (@info) { s/\000.*//; }
    print map("[$_]", @info),"\n";
  } else {
    warn "invalid type {$type}, wtf?";
  }

}

__END__


  struct tKNUCKLE **knuckles;       /* all user instances */
  int    uid;                       /* user id */
  char   username[LOGIN_SIZE];      /* user name */
  char   realname[NAME_SIZE];
  char   nickname[NAME_SIZE];         
  char   title1[NAME_SIZE];        /* title of user in OLC */
  char   title2[NAME_SIZE];        /* title of consultant in OLC */
  char   machine[NAME_SIZE];      /* user location */
  char   realm[NAME_SIZE];
  int    specialties[SPEC_SIZE];    /* Specialty list. */
  int    no_specialties;
  int    permissions;
  int    status;                    /* status of the user 
                                        (logout, idle, etc) */
  int    no_knuckles;               /* number of current connections */
  int    max_ask;                   /* maximum allowable connections */
  int    max_answer;
}
