#!/usr/bin/perl
require 'config/paths.pl';
$flag = 0;
$cert = "";
$arg0 = $ARGV[0];
$arg1 = $ARGV[1];
if (! "$arg1") {
  print "usage format_cert.pl <filename> <vulnerability (VUxxxxx) or advisory (CA-xxxx-xx) number>\n\n";
  exit;
}
if ($arg1 =~ /VU/) {
  open(TCP_SCAN, "$arg0");
  while (<TCP_SCAN>) {
    $_ =~ s/\\n/\n/g;
    $cert .= $_;
  } 
  open (TMP,"> cert_tmp.$$");
  open (CERT, "> $arg1.html");
  print  TMP "$cert";
  close (TMP);

  open (TMP, "cert_tmp.$$");
  while (<TMP>) {
    $flag = 1 if (/\<H1\>Vulnerability Note VU\#/);
    $flag = 0 if (/If you have feedback, comments, or additional information /);
    if (/HREF=\/[A-Z][A-Z][A-Z][A-Z]-/) {
      $_ =~ s/A HREF="/A HREF="http:\/\/www.kb.cert.org\/vuls\/id\//g;
    }
    print CERT if $flag == 1;
    $printing = 1 if $flag == 1;
  }
    if ($printing == 1) {
      print CERT "<CENTER><A HREF=\"http://www.cert.org/legal_stuff/legal_stuff.html\">CERT Copyright Material</A></CENTER>\n";
      close (CERT);
    } else {
      print "CERT Vulnerability not found\n\n";
      close (CERT);
      unlink ("$arg1.html");
    }
  unlink("cert_tmp.$$");
} elsif ($arg1 =~ /CA-/ || $arg1 =~ /IN-/) {
  die "File not found ($arg1)" unless open(TCP_SCAN, "$arg0");
  while (<TCP_SCAN>) {
    $_ =~ s/\\n/\n/g;
    $cert .= $_;
  }
  open (TMP,"> cert_tmp.$$");
  open (CERT, "> $arg1.html");
  print  TMP "$cert";
  close (TMP);

  open (TMP, "cert_tmp.$$");
  while (<TMP>) {
    $flag = 1 if (/\<H1\>CERT\<SUP\>/);
    $flag = 0 if (/CERT\/CC Contact Information/);
    print CERT if $flag == 1;
    $printing = 1 if $flag == 1;
  }
    if ($printing == 1) {
      print CERT "<CENTER><A HREF=\"http://www.cert.org/legal_stuff/legal_stuff.html\">CERT Copyright Material</A></CENTER>\n";
      close (CERT);
    } else {
      print "CERT Vulnerability ($arg1.html) not found\n\n";
      close (CERT);
      unlink ("$arg1.html");
    }
  unlink("cert_tmp.$$");


} else {
        print "CERT Vulnerability prefix not found\n\n";
}
