#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <dirent.h>
#include <mpi.h>

main(int argc, char* argv[]){

DIR *d;
struct dirent *de;
char file_name[1000], command[1000];
MPI_Status status;
int rank, size,i;

MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);

if(rank==0){
d=opendir("/tandem/bin/tmp");
i=1;
for (de=readdir(d);de != NULL;de=readdir(d)){
  if (strncmp(de->d_name, ".",10) && strncmp(de->d_name, "..",10)){
  strncpy(file_name, de->d_name,100);
  MPI_Send(file_name, 1000, MPI_CHAR,i,7,MPI_COMM_WORLD); i++;}}
}

else{
  MPI_Recv(file_name,1000,MPI_CHAR,0,7,MPI_COMM_WORLD,&status);
  strncpy(command, " LD_LIBRARY_PATH=/usr/local/lib;export LD_LIBRARY_PATH;/tandem/bin/tandem.exe '/tandem/bin/tmp/",1000);
  strncat(command, file_name, 1000);
  strncat(command, "' > output.txt", 1000);
  printf ("SLAVE %d: %s \n", rank, command);
  system(command);
}

MPI_Barrier(MPI_COMM_WORLD);
MPI_Finalize();

}

