#! /usr/local/bin/perl

$, =",";

$filename = $ARGV[0];

# read the mail

open (f, "<$filename");

@fields = (
"O",
"OU",
"OU2",
"POSTALCODE",
"oABBREVIATION",
"oDOMAIN",
"C",
"PHONE",
"DESCRIPTION",
"FACSIMILETELEPHONENUMBER",
"LATITUDE",
"LONGITUDE",
"BUSINESSCATEGORY",
"ouABBREVIATION",
"ouDOMAIN",
"ouDOMAIN2",
"Date",
"Time",
"Remote User",
"STREETADDRESS",
"dmCN",
"dmFACSIMILETELEPHONENUMBER",
"dmEMAIL",
"dmADDRESS",
"dmPHONE",
"computeresourceDESCRIPTION",
"wwwDESCRIPTION",
"schedulersDESCRIPTION" );

foreach $f (@fields) {
  $field{$f} = 1;
}


@multiline = (
"STREETADDRESS",
"dmADDRESS",
"computeresourceDESCRIPTION",
"wwwDESCRIPTION",
"schedulersDESCRIPTION" );

#print @fields;

# ignore till ****
$start = 0;

$last_attr = "";
$lines = "";
while (<f>) {
  
    s/  +/ /g;
  if ($start) {
    $line = $_;
    

    ($attr, $rest) = split (':', $_, 2);
    
    if ($field{$attr}) {
      $lines =~ s/^[\n]+//g;
      $lines =~ s/[\n]+$//g;
      $lines =~ s/^[ ]+//g;
      $lines =~ s/[ ]+$//g;
      $lines =~ s/[\n]/\\n/g;
      if ($last_attr) {
	$v{$last_attr} =  $lines;
      }
      $lines = $rest;
      $last_attr = $attr;
    } else {
      $lines .= $line;
    }
    
  }
  $start = $start | (/^\*+/) ;
}


for $f (@fields) {
  printf "# %s=%s\n", $f, $v{$f};
}

# prepare the fields

############################################################
$o=$v{O};
$o_zip = $v{POSTALCODE} . ", " . $v{C};
$ou_zip = $v{POSTALCODE} . ", " . $v{C};
$o_abbreviation = $v{oABBREVIATION};
$o_domain= $v{oDOMAIN};
#$o_abbreviation = "SU";
$ou=$v{OU};
$ou_abbreviation = $v{ouABBREVIATION};
$ou_domain= $v{ouDOMAIN};
$lon=$v{LONGITUDE};
$lat=$v{LATITUDE};
$businesscategory=$v{BUSINESSCATEGORY};
############################################################

if ($o) {
	$o_dn = "o=$o";
	$o_dn .= ", o=Globus, c=US";  
} else {
	printf "no organization o= specified\n";
	exit;
}

if ($ou) {
  $ou_dn = "ou=$ou, ". $o_dn;
}

# create o_dn

$part_dn = $ou_dn;
$part_dn =~ s/, o=Globus, c=US//g;

@parts = split(',', $part_dn);
@reverse_parts = sort { $a cmp $b } @parts;

$current_dn = "o=Globus, c=US";
foreach $part (@reverse_parts) {
  $part =~s/^ +| +$//g;
  $current_dn = $part . ", ".$current_dn ;
  printf "# creating dn=%s\n", $current_dn;
}

# create organization
print "\n";
printf "dn: %s\n", $o_dn;
printf "changetype: add\n";
printf "objectclass: GlobusTop\n";
printf "objectclass: GlobusOrganization\n";
printf "o: %s\n", $o;
if ($o_abbreviation){
  printf "o: %s\n", $o_abbreviation;
}
printf "postalcode: %s\n", $o_zip;
printf "facsimiletelephonenumber: %s\n", $v{FACSIMILETELEPHONENUMBER};
printf "streetaddress: %s\n", $v{STREETADDRESS};

#printf "subtreeaci: +(&(|(privilege=compare)".
#  "(privilege=search)(privilege=read)(privilege=write))".
#  "(client=ldap:///cn=Directory Manager, %s))", $o_dn;

printf "aci: (target =\"ldap:///";
printf "%s\")\n",$o_dn;
printf " (targetattr = \"*\")(version 3.0; acl \"Untitled\"; allow (compare,search,read,write,add,delete)\n";
printf " userdn = \"ldap:///cn=Directory Manager, %s\" ;)\n", $o_dn;


printf "domainname: %s\n", $o_domain;
if ($businesscategory) {
  printf "businesscategory: %s\n", $businesscategory;
}
$now = `date -u`;
$now =~ s/UTC/GMT/g;
$now =~ s/\n//g;
printf "lastupdate: %s\n", $now;
printf "ttl: constant\n", $now;
printf "backup: yes\n";

# create organizational unit
print "\n";
printf "dn: %s\n", $ou_dn;
printf "changetype: add\n";
printf "objectclass: GlobusTop\n";
printf "objectclass: GlobusOrganization\n";
printf "ou: %s\n", $ou;
printf "postalcode: %s\n", $ou_zip;
printf "facsimiletelephonenumber: %s\n", $v{FACSIMILETELEPHONENUMBER};
printf "streetaddress: %s\n", $v{STREETADDRESS};
if ($o_abbreviation){
  printf "ou: %s\n", $ou_abbreviation;
}

printf "aci: (target =\"ldap:///";
printf "%s\")\n", $ou_dn;
printf " (targetattr = \"*\")(version 3.0; acl \"Untitled\"; allow (compare,search,read,write,add,delete)\n";
printf " userdn = \"ldap:///cn=Directory Manager, %s\" ;)\n", $ou_dn;

printf "domainname: %s\n", $ou_domain;
if ($businesscategory) {
  printf "businesscategory: %s\n", $businesscategory;
}
$now = `date -u`;
$now =~ s/UTC/GMT/g;
$now =~ s/\n//g;
printf "lastupdate: %s\n", $now;
printf "ttl: constant\n", $now;
printf "backup: yes\n";

#if (ldap_exists($dm_o) {
#  skip
#} else {
#  use dm as specified
#}


if ($ou) {
  $dm_dn = $ou_dn;
  $abbr = $v{ouABBREVIATION};
} else {
  $dm_dn = $o_dn;
  $abbr = $v{oABBREVIATION};
}

printf "\n";
printf "dn: cn=Directory Manager, $dm_dn\n";
printf "changetype: add\n";
printf "objectclass: GlobusTop\n";
printf "objectclass: GlobusMDSAdministrator\n";
printf "cn: Directory Manager\n";
printf "email: %s\n", $v{dmEMAIL};
printf "phone: %s\n", $v{dmPHONE};
printf "facsimiletelephonenumber: %s\n", 
  $v{dmFACSIMILETELEPHONENUMBER};
printf "userpassword: %sadmin\n",$abbr;
printf "globusAdministrator: %s\n", $v{dmCN};
printf "lastupdate: %s\n", $now;
printf "ttl: constant\n";
printf "backup: yes\n";
printf "\n";

