#!/usr/bin/perl

# passwd_to_cdf: converts passwd(5) format user information into character
# delimited file expected by Workgroup Manager, for importing into LDAP
# Note that user passwords will *not* be propagated into the CDF; this appears
# impossible at the moment.
# leeg 2004-11-23
# Actually, can indeed import passwords but then the users have DES (legacy,
# world-readable) passwords rather than PasswordServer passwords.

use strict;

($#ARGV == 1) or die "Usage: $0 <infile> <outfile>\nAborted";

open(INFILE,"<@ARGV[0]") or die "Cannot open @ARGV[0] for reading: $!\nAborted";
open(OUTFILE,">@ARGV[1]") or die "Cannot open @ARGV[1] for writing: $!\nAborted";

select OUTFILE;

print "0x0A 0x5C 0x3A 0x2C dsRecTypeStandard:Users 9 dsAttrTypeStandard:RecordName dsAttrTypeStandard:Password dsAttrTypeStandard:UniqueID dsAttrTypeStandard:PrimaryGroupID dsAttrTypeStandard:RealName dsAttrTypeStandard:NFSHomeDirectory dsAttrTypeStandard:HomeDirectoryQuota dsAttrTypeStandard:UserShell dsAttrTypeStandard:MCXFlags\n";

# Output format is
# uname::uid:gid:GECOS:/home/dir:quota_in_bytes:/login/shell:plist

while(<INFILE>)
{
    chomp;
    my ($uname,$passwd,$uid,$gid,$gecos,$homeDir,$loginShell)=split /:/;
    print STDERR "Doing user $uname\n";
    my $quota=100*1024*1024;
    $quota/=5 if ($gid==101); #Booking staff get smaller quota
    # Tidy up the shells
    $loginShell="/bin/bash" if ($loginShell eq "/bin/csh");
    $loginShell="/bin/zsh" if ($loginShell eq "/usr/bin/zsh");
    $loginShell="/bin/tcsh" if ($loginShell eq "/usr/local/bin/tcsh");
    $loginShell="/bin/bash" if ($loginShell eq "/usr/local/bin/bash");
    $loginShell="/bin/bash" if ($loginShell eq "/usr/bin/ksh"); #No ksh on OSX
    print "$uname:$passwd:$uid:$gid:$gecos:$homeDir:$quota:$loginShell:";
    print <<'XML';
<?xml version="1.0" encoding="UTF-8"?>\
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http\://www.apple.com/DTDs/PropertyList-1.0.dtd">\
<plist version="1.0">\
<dict>\
	<key>simultaneous_login_enabled</key>\
	<true/>\
</dict>\
</plist>\

XML
}
close OUTFILE;
close INFILE;
