#! /usr/bin/perl 
#
# 
# detect object type and distributed to correct service for analysis

#warnings are usually helpful
use strict;

use File::Basename;
use File::Copy;
use Digest::MD5 qw(md5 md5_hex);
use Sys::Syslog;
use File::LibMagic ':complete';


my $filename;
my $file_size;
my $line;
my $output_filename;
my $payload_dir;
my $i;
my $mime_type;
my $payload_path;
my $log_entry;
my $object_hash;
my $object_magic;
my $magic_handle;

my $destination_analyzer;
my %destinations;


my $unlink_object = 1;


my $archive_dir = "archive";


openlog("object_mux", "ndelay,pid", "local3");

$magic_handle = magic_open(0);
magic_load($magic_handle,"");

if ( @ARGV > 0 )
{
	$payload_dir = $ARGV[0];
}


while(<STDIN>)
{
	$filename = $_;
	chomp($filename);
	
	#gather some basic data about object
	$file_size = -s $filename;		
	
	if (open(PAYLOAD_FILE, $filename))
        {
		binmode(PAYLOAD_FILE);
		$object_hash = Digest::MD5->new->addfile(*PAYLOAD_FILE)->hexdigest;
		close(PAYLOAD_FILE);
		
		$object_magic = magic_file($magic_handle, $filename);
	
	} else
        {
		warn("couldn't open object for hashing: ".$filename);
		$object_hash = "-";
		$object_magic = "-";
        }
	

	#decide what to do with this file
	#right now, all get null analysis
	
	%destinations = ();
	
	
	if ( substr($object_magic, 0 , 3) eq "PDF" )
	{
		#$destination_analyzer = "pdf";	
		$destinations{'pdf'} = 1;
		$destinations{'archive'} = 1;
	} elsif ( substr($object_magic, 0 , 3) eq "Zip" )
	{
		#$destination_analyzer = "zip";	
		$destinations{'zip'} = 1;
	} elsif ( substr($object_magic, 0 , 4) eq "PE32" )
	{
		$destinations{'archive'} = 1;
		
	} else
	{
		$destinations{'null'} = 1;
		#$destination_analyzer = "null";
	}
	
	
	
	if ( exists($destinations{'archive'}) )
	{
		#copy( $filename, $archive_dir."/".basename($filename) ) or warn("couldn't archive file: ".$archive_dir."/".basename($filename));
		`archive-helper $filename`
	}
	
	if ( exists($destinations{'pdf'}) )
	{
		`pdf-helper $filename`
	}
	
	if ( exists($destinations{'zip'}) )
	{
		`zip-helper $filename`
	}
	
	$log_entry = basename($filename)." ".$file_size." ".$object_hash." ".join(',',keys %destinations)." ".$object_magic;
	syslog("info", "%s", $log_entry);
	
	
	#purge the temp files
	if ( $unlink_object )
	{
		unlink($filename);
	}
	
}

magic_close($magic_handle);