/*
 * 28/11/2010 17:14
 * md5check.c ()
 * @author Mr Bertelle Nicolas, Nicoweb, 2010
 */
#include <stdio.h>
#include <stdlib.h>

#define ARGV_LIMIT 5

int main(int argc, char *argv[])
{
    FILE *pp;
    char buf[256];
    char* filename = NULL;
    char* md5sum = NULL;
    char* md5sum_computed = NULL;
    char *ptc;
    int a = 0;
    char cmd[256] = "md5sum ";
    char cmd_buf[256] = "";
    char *mycmd = "";
    int debugmode = 0;
    
    int tocancel[ARGV_LIMIT] = {};
    
    // cmd.getArguments()
    int i;
    int cpt = 0;

    if (argc > 1)
    // debugmode
    for (i = 1; i < argc; i++){
        if(argv[i][0]=='-'){
          if(argv[i][1]=='-'){ //printf("(--) --%s : %s\n",argv[i]+2,argv[i+1]);
              if((strcmp(argv[i]+2,"debugmode")==0)&&(strcmp(argv[i+1],"on")==0)){
               debugmode = 1; if(debugmode==1) printf("(--) debugmode : %d\n",debugmode);
              }
              if(strcmp(argv[i]+2,"pathmd5sum")==0){ 
               strcat(cmd_buf,argv[i+1]); if(debugmode==1) printf("(--pathmd5sum) cmd_buf(0) : %s\n",cmd_buf);
               strcat(cmd_buf,cmd); if(debugmode==1) printf("(--pathmd5sum) cmd_buf(1) : %s\n",cmd_buf);
               strcpy(cmd,cmd_buf); if(debugmode==1) printf("(--pathmd5sum) cmd : %s\n",cmd);
              }
          }
        }
    }
    // others arguments
    for (i = 1; i < argc; i++){
     // - or --
     if(argv[i][0]=='-'){
          if(argv[i][1]=='-'){ //printf("(--) --%s : %s\n",argv[i]+2,argv[i+1]);
              if(strcmp(argv[i]+2,"filename")==0){ 
               filename = argv[i+1]; if(debugmode==1) printf("(--) filename : %s\n",filename);
               strcat(cmd,argv[i+1]); if(debugmode==1) printf("cmd : %s\n",cmd);
              }
              if(strcmp(argv[i]+2,"md5tocheck")==0){ 
               md5sum = argv[i+1]; if(debugmode==1) printf("(--) md5tocheck : %s\n",md5sum);
              }
          }
          else{ //printf("(-) -%s : %s\n",argv[i]+1,argv[i+1]);
              if(strcmp(argv[i]+1,"f")==0){ 
               filename = argv[i+1]; if(debugmode==1) printf("(-f) filename : %s\n",filename);
               strcat(cmd,argv[i+1]); if(debugmode==1) printf("cmd : %s\n",cmd);
              }
              if(strcmp(argv[i]+1,"m")==0){ 
               md5sum = argv[i+1]; if(debugmode==1) printf("(-m) md5tocheck : %s\n",md5sum);
              }      
          }
          tocancel[cpt] = i + 1;
          cpt++;
     }
     /*else{
          int boo = 0;
          int u = 0;
          for(u=0;u<ARGV_LIMIT;a++){
                    if(tocancel[u]==i) boo = 1;
          }
          if(boo==0) printf("(else) %s\n",argv[i]);
     }*/
    }
    
    // system.execCmd()
    (char*) mycmd =  cmd; if(debugmode==1) printf("mycmd : %s\n",mycmd);
    if ((pp = popen(mycmd, "r")) != NULL) {
        while (fgets(buf, sizeof buf, pp))
        fputs(buf, stdout);
    }
    else{
        perror("popen");
        exit(EXIT_FAILURE);
    }
    pclose(pp); if(debugmode==1) printf("cmdresult : %s",buf);
    
    // system.readFromCmd()
    (FILE *) ptc = strtok (buf," ");
    while (ptc != NULL){
     if(a==0) md5sum_computed = ptc; //printf("md5sum_computed : %s\n",ptc);
     a++;
     (FILE *) ptc = strtok (NULL, " ");
    } if(debugmode==1) printf("md5sum_computed : %s\n",md5sum_computed);

    //
    if(strcmp(md5sum,md5sum_computed)==0) printf("SUCCESS :: le fichier précisé correspond bien à la signature MD5.");
    else printf("FAIL :: le fichier précisé ne correspond pas à la signature MD5.");
    
    return 0;
}


