﻿#!/usr/bin/perl
use MIME::Base64;

#Set the file
$FileName = @ARGV[0];

#Read in the file & Strip \r
open(FILE, $FileName);
while($file_line = <FILE>){
  $file_line =~ s/\r\n/\n/;
  $file_contents .= $file_line;
}
close(FILE);

#Check for file
if(!$file_contents){
  print "Usage: mht2html.pl FILENAME - converts Microsoft MHT files to the html equivalent";
  exit;
}


#Parse out the MIME sections in the file
while($file_contents =~ /------=_NextPart(.*?)\n------=_NextPart/sg){

  $mime_section = $1;
  
  #Parse out the filename and content
  $mime_section =~ /Content-Location(.*)\/(.*?)\nContent/sg;
  
  $file_name = $2;
  $mime_section =~ /Content-Type(.*?)\n\n(.*)\n/sg;

  $content = $2;

  if ($file_name =~ m/htm/){

  	#Check for base64
  	if($mime_section =~ /base64/){
    		$content = decode_base64($content);
  	}else{
    		#Strip the MS garbage from the file
    		$content =~ s/<!--\[(.*?)\]>(.*?)<!\[endif\]-->//sig;
    		$content =~ s/<!\[(.*?)\]>//sig;
    		$content =~ s/=3D/=/g;
    		$content =~ s/=\n//gm;
    		$content =~ s/src="(.*?)\/(.*?)"/src="$2"/sig;
  	}

  	#Write the new file

  	print $content;


  }

  
}

