#!/usr/bin/env perl

use strict;
use warnings;

use File::Copy;

# gem5 details
my $gem5_binary = '/p/vertical/afs-scratch/isa-wars/gem5-mips/build/MIPS/gem5.fast';
my $gem5_script = '/p/vertical/afs-scratch/isa-wars/gem5-mips/configs/example/se.py';
my $gem5_stats_dir  = 'm5out';
my $gem5_stats_file = 'stats.txt';

my $pin_binary = '/home/menon/pin/pin';

my %speccmds_int = ('perlbench_1' => 'input.program 5',
                    'bzip2_1'     => 'input.program 5',
                    'bzip2_2'     => 'dryer.jpg 2',
                    'gcc'         => 'cccp.i -o cccp.s',
                    'mcf'         => 'inp.in',
                    'gobmk'       => 'FIXME',
                    'hmmer'       => '--fixed 0 --mean 325 --num 45000 --sd 200 --seed 0 bombesin.hmm',
                    'sjeng'       => 'test.txt',
                    'libquantum'  => '33 5',
                    'h264ref'     => '-d foreman_test_encoder_baseline.cfg',
                    'omnetpp'     => 'omnetpp.ini',
                    'astar'       => 'lake.cfg');

my %speccmds_fp = ('bwaves'       => ' ',
                   'gamess'       => ' ',
                   'milc'         => ' ',
                   'zeusmp'       => ' ',
                   'gromacs'      => '-silent -deffnm gromacs -nice 0',
                   'cactusADM'    => 'benchADM.par',
                   'leslie3d'     => ' ',
                   'namd'         => '--input namd.input --iterations 1 --output namd.out',
                   'soplex'       => '-m10000 test.mps',
                   'povray'       => 'SPEC-benchmark-test.ini',
                   'calculix'     => '-i beampic',
                   'GemsFDTD'     => ' ',
                   'tonto'        => ' ',
                   'lbm'          => '20 reference.dat 0 1 100_100_130_cf_a.of',
                   'sphinx3'      => 'ctlfile . args.an4');


sub create_dirs
{
    map { mkdir $_ } keys %speccmds_int;
    map { mkdir $_ } keys %speccmds_fp;
}

sub form_int_benchmark_runcmds
{
    while (my ($b_name, $b_args) = each %speccmds_int)
    {
        print $gem5_binary . " " . $gem5_script . " -c ./" . $b_name . "_base.mipsel_gcc -o " . "\" $b_args \" \n";
    }
}

sub form_fp_benchmark_runcmds
{
    while (my ($b_name, $b_args) = each %speccmds_fp)
    {
        print $gem5_binary . " " . $gem5_script . " -c ./" . $b_name . "_base.mipsel_gcc -o " . "\" $b_args \" \n";
    }
}

# Arg. 1 - file suffix
sub backup_gem5_stats
{
    my ($suffix) = @_;

    map { copy("./$_/$gem5_stats_dir/$gem5_stats_file", "./$_/$gem5_stats_dir/$gem5_stats_file.$suffix") } keys %speccmds_int;
    map { copy("./$_/$gem5_stats_dir/$gem5_stats_file", "./$_/$gem5_stats_dir/$gem5_stats_file.$suffix") } keys %speccmds_fp;
}

sub emit_pin_cmdline
{
    while (my ($b_name, $b_args) = each %speccmds_int)
    {
        print $pin_binary . " -t /home/menon/pin/source/tools/MemOperandInsnMix/obj-ia32/movcount.so -- ./" . $b_name . "_base.atom_gcc " . " $b_args \n";
    }
    while (my ($b_name, $b_args) = each %speccmds_fp)
    {
        print $pin_binary . " -t /home/menon/pin/source/tools/MemOperandInsnMix/obj-ia32/movcount.so  -- ./" . $b_name . "_base.atom_gcc " . " $b_args \n";
    }
}

# backup_gem5_stats("insns");

form_int_benchmark_runcmds();
form_fp_benchmark_runcmds();

