#include "common.h"

typedef struct ftp_file
{
    char cmd[MAXLINE];  // FTP命令
    char filename[MAXLINE]; // 文件名参数
    char fileSize[MAXLINE]; // 文件大小参数
} ftp_file_t;


int main(int argc, char **argv) 
{
    int client_sock, port;
    char *host, buf[MAXLINE];
    rio_t rio;

    if (argc != 3) {
	    fprintf(stderr, "usage: %s <host><port>\n", argv[0]);
	    exit(1);
    }
    host = argv[1];
    port = atoi(argv[2]);

    client_sock = open_client_sock(host, port);
    if(client_sock==-1) {
        fputs("Error to connect the Server\n",stdout);
        exit(1);
    }
 
    while (fgets(buf, MAXLINE, stdin) != NULL) {
        ftp_file_t file;
        FILE *fp;
        long fileSize;

        char filePath[MAXLINE] = "./"; // 假设当前目录下有文件

        char *token = strtok(buf, " \t\n");  // 提取FTP命令
        if (strcmp(token, "get") == 0 || strcmp(token, "put") == 0) {
            strncpy(file.cmd, token, MAXLINE - 1);
            file.cmd[MAXLINE - 1] = '\0';
        } else {
            printf("Unsupported command: %s\n", token);
            continue; // 如果不是get或put命令，跳过处理
        }

        token = strtok(NULL, " \t\n");  // 提取文件名参数
        if (token != NULL) {
            strncpy(file.filename, token, MAXLINE - 1);
            file.filename[MAXLINE - 1] = '\0';
        } else {
            printf("Filename is required for command: %s\n", file.cmd);
            continue; // 如果没有提供文件名参数，跳过处理
        }

       

        if (strcmp(file.cmd,"put")==0)
        {
            strcat(filePath, file.filename); // 构建完整文件路径
            fp = fopen(filePath, "r");
            if (fp == NULL) {
                perror("Error opening file");
                continue;
            } else {
                fseek(fp, 0, SEEK_END);
                fileSize = ftell(fp);
                fclose(fp);
                snprintf(file.fileSize, MAXLINE, "%ld", fileSize); // 将文件大小转换为字符串并存储
            }

            send(client_sock, &file, sizeof(ftp_file_t), 0);
            fp = fopen(filePath, "r");
            if (fp == NULL) {
                perror("Error opening file for reading");
            } else {
                char *fileContent = malloc(fileSize);
                fread(fileContent, 1, fileSize, fp); // 从文件中读取内容到缓冲区
                send(client_sock, fileContent, fileSize, 0); // 发送文件内容
                free(fileContent);
                recv(client_sock, buf, MAXLINE, 0);
                fputs(buf, stdout);
                fclose(fp);
            }
        }
        else if (strcmp(file.cmd,"get")==0)
        {
            send(client_sock, &file, sizeof(ftp_file_t), 0);
            recv(client_sock, &file, sizeof(ftp_file_t), 0); // 接收文件信息
            int fileSize = atoi(file.fileSize);
            if (fileSize > 0) {
                char *fileContent = malloc(fileSize); // 根据文件大小分配内存
                recv(client_sock, fileContent, fileSize, 0); // 接收文件内容
                FILE *fp = fopen(file.filename, "w");
                if (fp != NULL) {
                    fwrite(fileContent, 1, fileSize, fp); // 将接收到的内容写入文件
                    fclose(fp);
                } else {
                    perror("Error creating file");
                }
                free(fileContent); // 释放内存
            }
            recv(client_sock, buf, MAXLINE, 0);
            fputs(buf, stdout);
        }
        
    }
    close(client_sock);
    exit(0);
}