#include "comn.h"

void checkCommand(char* msg_buf);
int main(int argc, char* argv[]) {
	// Socket to talk to clients
	context = zmq_ctx_new();
	responder = zmq_socket(context, ZMQ_REP);
	
	if(argv[1] != NULL) {
		char port[20];
		memset(port, 0, sizeof(port));
		sprintf(port, "tcp://*:%s", argv[1]);

		int rc = zmq_bind(responder, port);
		assert(rc == 0);
		while(1) {
        	        char buffer[50];
	                zmq_recv(responder, buffer, sizeof(buffer), 0);
			checkCommand(buffer);
	        }

	} else {
		printf("### enter the command ###\n");
	}

	return 0;

}

void checkCommand(char *msg_buf) {
	char *command[20];
	char *msg = strtok(msg_buf, " ");
	int i = 0;
	memset(command, 0, sizeof(command));

        while(msg != NULL) {
            command[i] = msg;
            i++;
            msg = strtok(NULL, " ");
        }

	sleep(1);
	
        if(strcmp(command[1], "file") == 0) {
                if(strcmp(command[0], "create") == 0) {
                	printf("## file create ##\n");
                        FILE *file;
			file = fopen(command[2], "w");
                        if(file != NULL) {
				fclose(file);
				zmq_send(responder, "0", 1, 0);
			} else {
				zmq_send(responder, "1", 1, 0);
			}
			
                } else if(strcmp(command[0], "remove") == 0) {
			int result = access(command[2], 0);
                        if(result == 0) {
                        	printf("## file remove ##\n");
                                remove(command[2]);
				zmq_send(responder, "0", 1, 0);
                        } else {
                        	printf("### no such file ###\n");
				zmq_send(responder, "1", 1, 0);
                        }
                } else if(strcmp(command[0], "append") == 0) {
			FILE *file;
                        if (command[3] != NULL) {
                        	int result = access(command[2], 0);
                                if(result == 0) {
                                	printf("## file append ##\n");
                                        file = fopen(command[2], "a");
                                        fputs(command[3], file);
                                        fclose(file);
					zmq_send(responder, "0", 1, 0);
                                } else {
                                	printf("### no such file ###\n");
					zmq_send(responder, "1", 1, 0);
                                }
                       	} else {
                        	printf("### plz enter contents ###\n");
				zmq_send(responder, "1", 1, 0);
			}
                } else {
                        printf("### wrong error ###\n");
			zmq_send(responder, "1", 1, 0);
                }
        } else if (command[1] != NULL) {
                if(strcmp(command[0], "lookup") == 0) {
                        printf("## file lookup ##\n");

			char file_list[1000];
			int i = 0;
			memset(file_list, 0, sizeof(file_list));
			char type[50] = {0, };	
			DIR *dir;
			struct dirent *ent;
			dir = opendir(command[1]);
//			dir = opendir("./");
			strcpy(file_list, command[1]);
			strcat(file_list, ",");
			if(dir != NULL) {
				while((ent = readdir(dir)) != NULL) {
					if(ent->d_type == 4) {
						strcpy(type, "D  ");
					} else if(ent->d_type == 8) {
                                                strcpy(type, "F  ");
                                        }
					strcat(type, ent->d_name);
//					file_list[i] = (char*)malloc(sizeof(type)*(strlen(type)+1));
					strcat(type, ",");
					strcat(file_list, type);
					i++;
				}
				closedir(dir);
			}
			zmq_send(responder, file_list, sizeof(file_list), 0);
                } else {
			printf("### wrong error ###\n");
		}       
        } else {
                printf("### plz enter command ###\n");
        }
}
