/* Copyright (c) 2001-2009 by SoftIntegration, Inc. All Rights Reserved */
/* Copyright (c) 2001-2004 by SoftIntegration, Inc. All Rights Reserved */
/************************************************************************
 * This function comments out special functions in "filename"
 * based on functions in .c files under directory "dirname".
 ************************************************************************/
#include <string.h>
#include <stdio.h>
#include <unistd.h>

int processcfile(char * dirname, char * filename){
    char *start_ptr, *stop_ptr, *ptr;
    string_t fullname, tmpfilename;
    string_t token, chdlfile, chdlfile2;
    string_t specialfunclist;
    string_t tmp_path, fullpath;
    int retval = 0;
    
    mkdir _tmp
    cp $dirname/*.c _tmp

    /* get the full path from dirname and filename */
    /* this part of the code is required for Windows. No harm for Unix */
    /* 
       for processcfile("hand_made", "../dir/xmlstring_chdl.c" 
       tmp_path will be "../dir/"
       ptr will be "xmlstring_chdl.c" 
       fullpath will be the full dir of 
       "/root_dir_before_hand_made/hand_made/../dir" without .. 
          such as "/root_dir_before_hand_made/dir" 
    */
    ptr = filename+strlen(filename);
    while((*ptr!='/') && (*ptr!='\\') && (ptr!=filename))
      ptr--;
    tmp_path = strncpy(tmp_path, filename, ptr-filename);
    fullpath = stradd(dirname, "/", tmp_path);
    tmp_path = _cwd;
    cd $fullpath
    fullpath = _cwd;
    cd $tmp_path

    if (ptr!=filename)
      ptr++;
    fullname = stradd(fullpath, "/", ptr);
    chdlfile = ``cat "$fullname"``;

    /*
       check if chdl file exists in hand made directory,
       if so, delete it
    */
    tmpfilename = stradd(tmpfilename, "_tmp/", ptr);
    if(access( tmpfilename, F_OK)==0)
    {
       /* printf("%s exists in %s, delete ...\n", ptr, dirname); */
       rm -f $(tmpfilename)
    }
    /* 
       check chdl file exists
    */
    if(access( fullname, F_OK)!=0)
    {
       printf("%s does not exists!\n",fullname);
       exit(0);
    }
    if(`ls _tmp` == NULL) {   // no special function
      rmdir("_tmp");
      printf(" No hand made functions\n");
      return retval;
    }
    specialfunclist = ``grep EXPORTCH _tmp/*.c``;
    if(specialfunclist == NULL) { // no special function
      rm -f _tmp/*.c
      rmdir("_tmp");
      return retval;
    }

    /* find each func prototype from the specal func list  */
    foreach(token;specialfunclist;NULL;" ") {
        if(!strstr(token, "_chdl("))
           continue;
        else  // use " funcname_chdl(void" 
           token = stradd(" ", token);
        ptr = *(char **)&chdlfile; // get address of string
        start_ptr = strstr(ptr, token);
        if(!start_ptr){
	   //printf("Error: cannot find string '%s' in file %s!\n", token, filename);
           retval = -1;
	   continue;
	}
        else
        {
           ;
	   // printf("Found string '%s' in file %s!\n", token, filename);
        }

	while(*start_ptr != '\n') { // Find '\n' in  "\nEXPORTCH"
	   start_ptr--;
	}
	start_ptr++;
	strncpy(chdlfile2, ptr, start_ptr-ptr);
	strcat(chdlfile2, "/*\n");
	stop_ptr = strstr(start_ptr+1, "EXPORTCH"); // find next "EXPORTCH"
	if(stop_ptr) {
	    if(*(stop_ptr-3)=='/' && *(stop_ptr-2)=='*') // /#\nEXPORTCH
	      stop_ptr -= 3;
	    strncat(chdlfile2, start_ptr, stop_ptr-start_ptr);
	    strcat(chdlfile2, "*/\n");
	    strcat(chdlfile2, stop_ptr);
        }
	else{ /* last one */
	    strcat(chdlfile2, start_ptr);
	    strcat(chdlfile2, "*/\n");
        }
	chdlfile = chdlfile2;
    }
    rm -f _tmp/*.c
    rmdir("_tmp");

    FILE *stream;
    if(!(stream = fopen(fullname, "w"))) {
        perror(fullname);
        return -1;
    }
    fprintf(stream, "%s", chdlfile2);
    fclose(stream);
    return retval;
}
