# File to hold site-specific customizations.

# Enable maximum warnings and disallow sloppy constructs.
use warnings;
use strict;
#use Socket;
#use Net::hostent;

use Net::LDAP;

open(PROTO, "> /etc/protocols");

print PROTO "ip 0 IP\n";
print PROTO "icmp 1 ICMP\n";
print PROTO "tcp 6 TCP\n";
print PROTO "udp 17 UDP\n";
close PROTO;

# As soon as we have 'register first in DHCP' add this back 

$u->{'UserData'}->{'ComputerName'} = 
 sub {
     my $macaddr = $u->{'_meta'}->{'macaddr'};

     my $ds;
	
     do {
       $ds = Net::LDAP->new('ldap://ldap.internal.hawkerc.net');
       
        if (not $ds) {
          print "LDAP connection failed:  $@\n";
        }
   
      } until ($ds);

     
     my $basedn = "dc=hawkerc,dc=net";
     
     $ds->bind() || die "bind failed!";
     
     my $attrs = [ 'cn', 'macAddress' ];
     
     my $splitmac = join(':',split(/(..)/, lc($macaddr)));
     $splitmac =~ /\:(.*)/;
     $splitmac = $1;
     $splitmac =~ s/\:\:/\:/g;

     my $mesg;
     do {
       $mesg = $ds->search(
			    base => $basedn,
			    filter => "(&(&(objectClass=ieee802device)(macAddress=$splitmac))(cn=*))",
			    attrs => $attrs,
			    );
       if ($mesg->count != 1) {
	   print "Please enter the computer into LDAP first!\n I found mac address ". $splitmac." ".$mesg->count." times in ldap\n\n Press ENTER to retry:";
	   my $foo = <>;
       }
     } until ($mesg->count == 1); 
     return $mesg->entry(0)->get_value('cn');
 };

$u->{'_meta'}->{'middle'} = 
 sub {
     if ($u->{'UserData'}->{'ComputerName'} =~ /^ART/i) {
        return "graphics.bat;";
    }
     if ($u->{'UserData'}->{'ComputerName'} =~ /^TESTGRAPHICS/i) {
        return "graphics.bat;";
    }
     if ($u->{'UserData'}->{'ComputerName'} =~ /^DESIGN/i) {
	 $u->{'UserData'}->{'ComputerName'} =~ /^DESIGN(\d+)/i;
	 if ($1 >= 22 && $1 <= 25) {
	     return "design.bat; punch5in1.bat";
	 }
        return "design.bat";
    }
     if ($u->{'UserData'}->{'ComputerName'} =~ /^L84/i) {
        return "cisco.bat";
    }
     if ($u->{'UserData'}->{'ComputerName'} =~ /^L900/i) {
        return "myob.bat";
    }
     if ($u->{'UserData'}->{'ComputerName'} =~ /^L16/i) {
        return "myob.bat";
    }
     if ($u->{'UserData'}->{'ComputerName'} =~ /^SSC/i) {
        return "gis.bat";
    }

    return undef
    };

$u->{'Identification'}->{'DomainAdmin'} = 
    sub {
      return $u->{'_meta'}->{'z_user'};
  };
$u->{'Identification'}->{'DomainAdminPassword'} = 
    sub {
      return $u->{'_meta'}->{'z_password'};
  };


#$u->{'UserData'}->{'ComputerName'} =
#    sub {
#        my $addr = $u->{'_meta'}->{'ipaddr'};
#        defined $addr
#            or return undef;
#       my $host = gethostbyaddr (inet_aton ($addr));
#
#        if (!defined $host) {
#            warn "Unable to gethostbyaddr ($addr): $? $^E\n";
#            return undef;
#        }
#
#        my $name = $host->name ();
#        # Strip off domain portion
#        $name =~ s/\.(.*)//;
#        return $name;
#    };


#$u->{'UserData'}->{'ComputerName'} =
#    sub {
#        my $addr = $u->{'_meta'}->{'ipaddr'};
#        defined $addr
#            or return undef;
#       my $host = gethostbyaddr (inet_aton ($addr));
#
#        if (!defined $host) {
#            warn "Unable to gethostbyaddr ($addr): $? $^E\n";
#            return undef;
#        }
#
#        my $name = $host->name ();
#        # Strip off domain portion
#        $name =~ s/\.(.*)//;
#        return $name;
#    };


#
# PUT YOUR CHANGES HERE
#

# Make this file evaluate to "true".
1;
