#include <stdio.h>
#include <string.h>

#define MAX 10000

int main(int argc, char *argv[])
{
	FILE *fp1;
	FILE *fp2;
	FILE *test_fp;
	FILE *testlist_fp;
	char str[MAX] = {0};
	char str2[MAX] = {0};
	char test_name[100] = {0};
	char *pch;
	char PID[20] = {0};
	char class[100] = {0};
	char pre_fold[50] = {0};
	char cur_fold[50]= {0};
	int hasprinted = 0;

	if(argc == 1){
		fprintf(stdout, "Usage ./split [source_file] [train_dile]\n");
		return 0;
	}

	if( (fp1 = fopen(argv[1], "r")) == NULL){
		fprintf(stderr, "file %s open error\n", argv[1]);
		return 0;
	}
	
	if( (fp2 = fopen(argv[2], "w")) == NULL){
		fprintf(stderr, "file %s open error\n", argv[2]);
		return 0;
	}
	
	if( (testlist_fp = fopen("test.list", "w")) == NULL){
		fprintf(stderr, "file test.list open error\n");
		return 0;
	}
	
	while(fgets(str, MAX, fp1) != NULL){
		pch = strtok(str, "\n\r");
		strcpy(str2, pch);
		if(str[0] == '>'){
			pch = strtok(str, " \t");		// PID, ex: >ld1aej
			strncpy(PID, pch+2, 6);
			pch = strtok(NULL, " \t"); 	// SCOP class ex: a.1.1.1
			strcpy(class, pch);
			pch = strtok(class, ".");
			strcpy(cur_fold, pch);
			pch = strtok(NULL, ".");
			strcat(cur_fold, pch);
			if( (strcmp(cur_fold, pre_fold)==0) && hasprinted == 0) {
				strcpy(test_name, PID);
				strcat(test_name, ".test");
				fprintf(stderr, "%s\n", test_name);
				if( (test_fp = fopen( test_name, "w")) == NULL){
					fprintf(stderr, "file %s open error\n", test_name);
					return 0;
				}
				fprintf(testlist_fp, "%s\n", PID);
				fprintf(test_fp, "%s\n", str2);
			}	 
			else if(strcmp(cur_fold, pre_fold) != 0) {
				hasprinted = 0;
				fprintf(fp2, "%s\n", str2);
			}
			else 
				fprintf(fp2, "%s\n", str2);
		}
		else{
         if( (strcmp(cur_fold, pre_fold)==0) && hasprinted == 0) {
				fprintf(test_fp, "%s\n", str2);
				hasprinted = 1;
				fclose(test_fp);	// close output stream
			}			
			else
				fprintf(fp2, "%s\n", str2);
			strcpy(pre_fold, cur_fold);
		}
	}
	return 1;
	
}

