#!/usr/bin/perl

if ( $#ARGV < 0 ) {
   showUsage();
   exit( 1 );
}
foreach my $topic ( @ARGV ) { 
  pushToMantis( $topic );
}
exit( 0 );

#=========================
sub showUsage
{
  print "Utility to push a CSV file into mantis,\n";
  print "% push-to-manstis file.csv\n";
}

#=========================
sub pushToMantis
{
  my ( $file ) = @_;

  my @lines = split(/[\n\r]+/, readFile( $file ) );
  my @fields = map{
      $_ = lc( $_ );
      s/ /_/g;
      s/^reporter$/reporter_id/;
      s/^assigned_to$/handler_id/;
      s/^category$/category_id/;
      s/^os_version$/os_build/;
      s/^view_status$/view_state/;
      $_
    }
    split(/,/, shift @lines );

  my %user_id = (
    administrator => 1,
    PeterThoeny => 2,
    # etc...
  );
  my %priority = (
    'none' => 10,
    'low' => 20,
    'normal' => 30,
    'high' => 40,
    'urgent' => 50,
    'immediate' => 60,
  );
  my %severity = (
    'feature' => 10,
    'trivial' => 20,
    'text' => 30,
    'tweak' => 40,
    'minor' => 50,
    'major' => 60,
    'crash' => 70,
    'block' => 80,
  );
  my %reproducability = (
    'always' => 10,
    'sometimes' => 30,
    'random' => 50,
    'have not tried' => 70,
    'unable to duplicate' => 90,
    'N/A' => 100,
  );
  my %category_id = (
    General => 1,
    Android => 3,
    Web => 4,
    Server => 5,
    Iphone => 6,
    Design => 7,
  );
  my %view_state = (
    'public' => 10,
    'private' => 50,
  );
  my %status = (
    'new' => 10,
    'feedback' => 20,
    'acknowledged' => 30,
    'confirmed' => 40,
    'assigned' => 50,
    'resolved' => 80,
    'closed' => 90,
  );
  my %resolution = (
    'open' => 10,
    'fixed' => 20,
    'reopened' => 30,
    'unable to duplicate' => 40,
    'not fixable' => 50,
    'duplicate' => 60,
    'not a bug' => 70,
    'suspended' => 80,
    "won't fix" => 90,
  );

  # login
  `curl -s -c mantis.cookies -d 'username=administrator&password=*****' https://example.com/mantisbt/login.php`;

  foreach my $line ( @lines ) {
    print "processing: $line\n";
    my $cmd = 'curl -s -b mantis.cookies -c mantis.cookies -F "project_id=1"';
    my @items = map{
        s/""/"/;
        $_
      }
      split(/,/, $line );
    my $i = -1;
    foreach my $item ( @items ) {
      $i++;
      next if( ( $i == 0 ) || ( $i == 13 ) );
      if( ( $i == 1 ) || ( $i == 2 ) ) {
        if( $user_id{$item} ) {
          $item = $user_id{$item};
        } else {
          $item = 1;
        }

      } elsif( $i == 3 ) {
        if( $priority{$item} ) {
          $item = $priority{$item};
        } else {
          $item = 30;
        }

      } elsif( $i == 4 ) {
        if( $severity{$item} ) {
          $item = $severity{$item};
        } else {
          $item = 50;
        }

      } elsif( $i == 5 ) {
        if( $reproducibility{$item} ) {
          $item = $reproducibility{$item};
        } else {
          $item = 10;
        }

      } elsif( $i == 7 ) {
        if( $category_id{$item} ) {
          $item = $category_id{$item};
        } else {
          $item = 1;
        }

      } elsif( $i == 12 ) {
        if( $view_state{$item} ) {
          $item = $view_state{$item};
        } else {
          $item = 10;
        }

      } elsif( $i == 16 ) {
        if( $status{$item} ) {
          $item = $status{$item};
        } else {
          $item = 10;
        }

      } elsif( $i == 17 ) {
        if( $resolution{$item} ) {
          $item = $resolution{$item};
        } else {
          $item = 10;
        }

      }

      if( $item =~ /"/ ) {
        $item =~ s/'//g;
        $cmd .= " -F '" . $fields[$i] . '=' . $item . "'";
      } else {
        $cmd .= ' -F "' . $fields[$i] . '=' . $item . '"';
      }
    }
    $cmd .= ' https://example.com/mantisbt/bug_report.php';
    print "===> execute cmd: $cmd ...";
    `$cmd`;
    print "...done\n\n";

  }
}

#=========================
sub readFile {
  my $name = shift;
  my $data = '';
  unless ( open( IN_FILE, "<$name" ) ) {
    print "  ERROR: Can't read file $name - $!\n";
    return;
  }
  local $/ = undef; # set to read to EOF
  $data = <IN_FILE>;
  close( IN_FILE );
  $data = '' unless $data; # no undefined
  return $data;
}

#=========================
