#! /usr/bin/perl

# Copyright (c) Members of the EGEE Collaboration. 2004-2010. 
# See http://www.eu-egee.org/partners/ for details on the copyright
# holders.  
# 
# Licensed under the Apache License, Version 2.0 (the "License"); 
# you may not use this file except in compliance with the License. 
# You may obtain a copy of the License at 
# 
#     http://www.apache.org/licenses/LICENSE-2.0 
# 
# Unless required by applicable law or agreed to in writing, software 
# distributed under the License is distributed on an "AS IS" BASIS, 
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
# See the License for the specific language governing permissions and 
# limitations under the License.
# 
# 
#  Authors:
#  2009-
#     Oscar Koeroo <okoeroo@nikhef.nl>
#     Mischa Sall\'e <msalle@nikhef.nl>
#     David Groep <davidg@nikhef.nl>
#     NIKHEF Amsterdam, the Netherlands
#     <grid-mw-security@nikhef.nl> 
# 
#  2007-2009
#     Oscar Koeroo <okoeroo@nikhef.nl>
#     David Groep <davidg@nikhef.nl>
#     NIKHEF Amsterdam, the Netherlands
# 
#  2003-2007
#     Martijn Steenbakkers <martijn@nikhef.nl>
#     Gerben Venekamp <venekamp@nikhef.nl>
#     Oscar Koeroo <okoeroo@nikhef.nl>
#     David Groep <davidg@nikhef.nl>
#     NIKHEF Amsterdam, the Netherlands


$opt_n=50;
$opt_l=3;
$opt_basedn="dc=farmnet,dc=nikhef,dc=nl";
$opt_nfs="hooimijt:/export/cache/edgdevtb/poolaccounts";

@optdef=qw( v vo=s n:i basedn l:i b|uidbase:i g|gid=i );

use Getopt::Long;
$Getopt::Long::ignorecase=0;
&GetOptions(@optdef);

$opt_vo or die "No VO specified\n";
length($opt_vo)>5 and die "VO name too long\n";
$opt_g or die "No GroupID specified\n";
$opt_b or die "No start of UID numbering specified\n";

$opt_v and print "Generating VO $opt_vo with $opt_n accounts\n";
$opt_v and print "in DIT position $opt_basedn\n";

$fmtstring='%s%0'.$opt_l.'d';

for($i=1;$i<=$opt_n;$i++) {
  $uid=sprintf($fmtstring,$opt_vo,$i);
  $n=$i+$opt_b;

  print "dn: uid=$uid, ou=PoolAccounts, $opt_basedn\n";
  print "objectclass: top\n";
  print "objectclass: posixAccount\n";
  print "cn: VO $opt_vo member no $i\n";
  print "uid: $uid\n";
  print "uidNumber: $n\n";
  print "gidNumber: $opt_g\n";
  print "homeDirectory: /home/$uid\n";
  print "loginShell: /bin/sh\n";
  print "description: $opt_nfs/$uid\n";
  print "\n";

  print "dn: cn=$uid,ou=pool,ou=auto.home,ou=automount,$opt_basedn\n";
  print "objectclass: top\n";
  print "objectclass: automount\n";
  print "cn: $uid\n";
  print "automountInformation: $opt_nfs/$uid\n";
  print "description: EDG dev testbed pool account $uid\n";
  print "\n";
}

