#
#  Req.pm
#  --make the req about the mchange
#
#


package Mchange::Req;

use strict;
use warnings;

use lib '/afs/.cs.wisc.edu/s/mchange-2.2/common/perl5';
use lib '/s/req/lib';

use Req;
use Date::Calc qw( :all );
use Carp;
use Mail::Header;
use Mail::Internet;
use Tie::IxHash;

sub new {

  my ( $class, %params ) = @_;

  print "Connecting to Req database...\n";
  
  my $req = new Req "$ENV{HOME}/.reqrc";
  
  my $ref = { req => $req,
              parent_req => $params{parent_req},
              machine => $params{machine} };
              
              
  return bless $ref, $class;

}

sub DESTROY {

  my ( $self ) = @_;
  $self->{req}->disconnect if $self->{req};
  
}

#
#  send 
#  --send the req about the mchange throught mail
#

sub send {

  my ( $self, $data, $type ) = @_;

  my $req_data = $self->gen_req( $data, $type );

  my $header = new Mail::Header;
  $header->add( 'To', 'lab@cs.wisc.edu' );
  $header->add( 'From', getpwuid( $< ) );
  $header->add( 'Subject', $req_data->{subject} );
  $header->add( 'X-Request-Do', "give $req_data->{give}" ) if $req_data->{give};
  $header->add( 'X-Request-Parent', $self->{parent_req} )
    unless $self->{parent_req} eq 'None';

  my $mail = new Mail::Internet( Header => $header );
  $mail->body( $req_data->{body} );

  print "Error sending mail\n" unless $mail->smtpsend;
}



#
#  send2
#  --send the req about the mchange through req proper
#
 
sub send2 {

  my ( $self, $data, $type ) = @_;

  my $req_data =  $self->gen_req( $data, $type );

  my $date = Date_to_Text( Today() );
  my $requester = getpwuid( $< );
  chomp $requester;
  
  my $message = "Subject: $req_data->{subject}\nFrom: $requester\nDate: $date\n\n$req_data->{body}\n";

  print "$message\n";
  return;
  
  my $request = $self->{req}->create( $req_data->{subject} );
  $self->{req}->add_requester( $request, $requester );
  $self->{req}->add_message( $request, $message, "open" );

  unless ( $self->{parent_req} eq 'None' ) {
    my ( $parent ) = $self->{req}->get_requests( number => $self->{parent_req} );
    my $ret = $self->{req}->add_child( $parent, $request );
  }

  $self->{req}->give( $request, $self->give_to( $data->{req_type} ) );
  
  print "[CSL #" . $request->get_number . " ] created\n";

  $self->{req}->disconnect;

  return $request->get_number;
}

#
#  gen_req 
#  --generate a req based on it's type
#

sub gen_req {

  my ( $self, $data, $type ) = @_;

  my ( $subject, $body );
  
  if ( $type eq 'invent' ) {
    $subject = "Mchange for $self->{machine}";
    $body = $self->gen_invent_req( $data );
  }
  elsif ( $type eq 'dns' ) {
    $subject = "DNS update for $self->{machine}";
    $body = $self->gen_dns_req( $data );
  }
  else {
    print "Error: invalid req_type attribute--fix your code\n";
    $subject = "Generic Mchange for some machine";
    $body = "This is not good.  Someone needs to fix their code";
  }

  return {subject => $subject, body => $body, give => $self->give_to( $type )};
  
}

#
#  gen_invent_req
#  --make the body of an invent req
#

sub gen_invent_req {

  my ( $self, $data ) = @_;

  my $body = "";

  while ( my ( $key, $value ) = each( %$data ) ) {

    if ( $key eq 'names' ) {
      $body .= "\nNAME CHANGES:\n====================\n";
      foreach my $name ( @{$data->{names}} ) {
        $body .= "$name->{machine} is becoming $name->{value}\n";
      }
    }
    elsif ( $key eq 'parts' ) {
      $body .= "\nPART CHANGES:\n====================\n";
      foreach my $part ( @{$data->{parts}} ) {
        $body .= "$part->{invnum} ( $part->{mdesc} ) is moving to $part->{machine}\n";
      }
    }
    elsif ( $key eq 'validos' ) {
      $body .= "\nVALIDOS CHANGES:\n====================\n";
      foreach my $validos ( @{$data->{validos}} ) {
        $body .= "$validos->{machine} is adding $validos->{value} as a valid OS\n";
      }
    }
    elsif ( $key eq 'attrib' ) {
      $body .= "\nATTRIBUTE CHANGES:\n====================\n";
      foreach my $attrib ( @{$data->{attrib}} ) {
        $body .= "$attrib->{machine} is changing $attrib->{attrib} to $attrib->{value}\n";
      }
    }
    elsif ( $key eq 'office' ) {
      $body .= "\nOFFICE CHANGES:\n====================\n";
      foreach my $office ( @{$data->{office}} ) {
        $body .= "$office->{user}\'s office changed to $office->{office}\n";
      }
    }
    elsif ( $key eq 'splat_add' ) {
      $body .= "\nSPLAT PART ADDS:\n====================\n";
      foreach my $add ( @{$data->{splat_add}} ) {
        $body .= "$add->{part} MAC $add->{mac} interface $add->{int} added to splat db\n";
      }
    }
    elsif ( $key eq 'splat_names' ) {
      $body .= "\nSPLAT NAME CHANGES:\n====================\n";
      foreach my $name ( @{$data->{splat_names}} ) {
        $body .= "$name->{curname} changed to $name->{newname} in splat db\n";
      }
    }
    elsif ( $key eq 'splat_attach' ) {
      $body .= "\nSPLAT ATTACHES:\n====================\n";
      foreach my $attach ( @{$data->{splat_attach}} ) {
        $body .= "$attach->{hostname} was attached to $attach->{jack}\n";
      }
    }
  }
  
  return $body;
  
}

#
#  gen_dns_req 
#  --generate a dns req based on the changes
#

sub gen_dns_req {

  my ( $self, $data ) = @_;

  my $body = "";

  $body .= "Please update DNS to reflect the following info:\n\n";

  while ( my ($mname, $info) = each( %{$data->{dns_add}} ) ) {
    $body .= "Add $info->{newname} to DNS.  It is going into room $info->{room}\n";
  }

  $body .= "MAKE SURE TO RESOLVE WHEN DONE--OTHERWISE MCHANGE SCRIPT WON'T GO\n";
  
  return $body;
  
}


#
#  give_to
#  --detemine who to give the req to based on req type
#

sub give_to {

  my ( $self, $req_type ) = @_;

  my $file = "/afs/.cs.wisc.edu/s/mchange-2.2/common/configs/req_type";
  open( REQTYPE, "<$file" )
    or die "Couldn't open file $file\n";

  while ( my $line = <REQTYPE> ) {
    chomp $line;
    next unless $line;
    next if $line =~ /^\#/;

    $line =~ /^(.*):(.*)$/;
    next unless $1 eq $req_type;

    return $2;
  }
}

#
#  get_reqs
#  --get the current reqs in a persons queue
#

sub get_reqs {
  
  my ( $self, $user ) = @_;

  print "Finding Req reqs for $user\n";
  my @list = $self->{req}->get_requests( owner => $user, status => ['active', 'stalled'], 'allow_kids' => '1' );
  
  my $ret;

  tie %$ret, "Tie::IxHash";
  
  map { $ret->{"(" . $_->get_number . ") " . $_->get_subject} = $_->get_number } @list;
  
  return $ret;  
}

1;
