#!/usr/bin/perl -w

use strict ;
use Getopt::Std ;

# Looks at the gridmapfile located from /etc/sysconfig/edg 
# and create a dcache.kpwd file from the grid-mapfile.
# a -o output can be used to create the file in a different location.

my $domainname = `hostname -d` ;
chomp($domainname) ;

my $usage=
"usage: grid-mapfile2dcache-kpwd [OPTIONS]
         you can specify the following options:
	
    -h         to print this message
    -g         to enable debug messages printing
    -i <grid-mapfile> to specify the path of grid-mapfile
               the default path is /etc/grid-security/grid-mapfile
    -o <dcache-kpwd> to specify the path of dcache-kpwd output file
               the default path is dcache.kpwd
    -r <pnfs-root> to specify the root of the users in pnfs
               the default root is /pnfs/$domainname/usr/cms
    -H <pnfs-home> to specify the home of user within pnfs, relative to
               the pnfs root, default is / 
    -a         to append the name of the user to the pnfs root \n";
    	       
my %opts=();
getopts("hi:o:r:H:ag", \%opts) ;

if($opts{'h'})
{
	print $usage;
	exit ;
}
my $dcacheKpwd ="dcache.kpwd";
$dcacheKpwd = $opts{'o'} if ($opts{'o'}) ;
my $gridmap = "/etc/grid-security/grid-mapfile";
$gridmap= $opts{'i'} if ($opts{'i'}) ;
my $pnfsroot = "/pnfs/$domainname/usr/cms";
$pnfsroot = $opts{'r'} if ($opts{'r'}) ;
my $pnfshome = "/";
$pnfshome = $opts{'H'} if ($opts{'H'}) ;
sub dprint;
sub dprint {
 print @_ if defined $opts{'g'};
}

open(GRIDMAP,"<$gridmap") or die "Could not open $gridmap: $!\n" ;
my %vos ;
dprint "reading grid mapping from $gridmap";
dprint "writing into $dcacheKpwd";
while(<GRIDMAP>) {
 if (/^\s*"(.*CN=.*)"\s+(\S+)\s*$/) {
    dprint "read mapping from $1 to $2 \n" ;
    my $first = $1;
    my $second = $2;
    if(/\/E=/)
    {
        dprint("found E= in \"$first\" \n");
        my $substitute = $first;
        $substitute =~ s/\/E=/\/Email=/;
       if(getpwnam($second))
       {
          dprint(" storing $substitute\n");
          push( @{$vos{$second}} , $substitute ) ;
       }
       else
       {
           print  STDERR "user $second is unknown to the system\n";
       }
    }
    if(/\/Email=/)
    {
        dprint("found Email= in \"$first\" \n");
        my $substitute = $first;
        $substitute =~ s/\/Email=/\/E=/;
       if(getpwnam($second))
       {
          dprint(" storing $substitute\n");
          push( @{$vos{$second}} , $substitute ) ;
       }
       else
       {
           print  STDERR "user $second is unknown to the system\n";
       }
    }
    if(getpwnam($second))
    {
      dprint(" storing $first\n");
      push( @{$vos{$second}} , $first ) ;
    }
    else
    {
       print  STDERR "user $second is unknown to the system\n";
    }
 }
}

open(DCACHE,">$dcacheKpwd") || die "Could not open $dcacheKpwd: $!\n" ;

print DCACHE "# This was created by the grid-mapfile2dcache-kpwd command\n" ;
print DCACHE "# from the grid-mapfile\n" ;
print DCACHE <<EOF;
# the following are the mappings from secure credetials ids to user names
# these are used to map credentials to the default user,
# if user is not supplied and can not be derived from credentials
# in user created files this do not have to be in a separate section
# The following entry is just an example for a mapping of a DN to a l

EOF

foreach my $vo (keys %vos) {
  print DCACHE "# Mappings for $vo users\n" ;
  foreach ( sort  @{$vos{$vo}} ) {
     print DCACHE 'mapping "'.$_.'" '.$vo."\n" ;
  }
  print DCACHE "\n" ;
}


print DCACHE <<EOF;

# the following are the user auth records
# The following is just an example of how the local user is mapped to a
# pnfs root. Multiple users with their DN can be mapped

EOF



foreach my $vo (keys %vos) {
	dprint "processing $vo \n";
  my ( $vouid,  $vogid ) = (getpwnam($vo))[2,3] ;
  if(!$vouid || !$vogid)
  {
        print STDERR "cannot determine uid/gid for \"$vo\"  \n";
	next;
  }
  dprint " $vo uid is $vouid $vo gid is $vogid \n";
  print DCACHE  "# logins for VO $vo.\n" ;
  if($opts{'a'})
  {
      print DCACHE  "login $vo $vouid $vogid $pnfshome $pnfsroot/$vo $pnfsroot/$vo\n" ;
  }
  else
  {
      print DCACHE  "login $vo $vouid $vogid $pnfshome $pnfsroot $pnfsroot\n" ;
  }
  foreach ( sort @{$vos{$vo}} ) {
    print DCACHE "\t$_\n" ;
  }
  print DCACHE "\n" ;
}




