// File: /workspace/github_c_makefile_star2_O1/evanwire[P]GitClone/server.c
// Function: commit
void commit(char* command, int connfd){
  //Parse project name from the message received from the client
  char project_name[strlen(command) - 1];
  memset(project_name, 0, sizeof(project_name));
  int i;
  for(i = 2; i < strlen(command); i++){
      if(command[i]){
        project_name[i - 2] = command[i];
      }

  }

  //Set up buffer for responding to client
  char sendBuff[1024];
  memset(sendBuff, '\0', sizeof(sendBuff));

  //Attempt to open directory with the name specified by the client
  DIR* dir = opendir(project_name);

  //Check if it exists, report an error if not
  if(!dir){
    closedir(dir);
    strcpy(sendBuff, "e");
    printf("Message: -%s- size: %d\n", sendBuff, strlen(sendBuff));
    write(connfd, sendBuff, strlen(sendBuff));
    return;
  }

  closedir(dir);

  //Open the needed Manifest file
  char manifestPath[sizeof(project_name) + 11];
  strcpy(manifestPath,project_name);
  strcat(manifestPath,"/.Manifest");

  int manifest_fd = open(manifestPath, O_RDONLY);

    /*
    TODO: Communicate errors like these to clients in an efficient way
        -Make a method for only communicating errors
    */

   //check if manifest exists, report an error if not
  if(manifest_fd < 0){
    close(manifest_fd);
    strcpy(sendBuff, "x");
    printf("Message: -%s- size: %d\n", sendBuff, strlen(sendBuff));
    write(connfd, sendBuff, strlen(sendBuff));
    return;
  }

  close(manifest_fd);
  char* fileContents=readFile(manifestPath);

  printf("Message: -%s- size: %d", fileContents, strlen(fileContents));
  write(connfd, fileContents, strlen(fileContents));


}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x400, %rsp            # imm = 0x400
movl %esi, %ebx
movq %rdi, %r12
callq 0x10b0
movq %rax, %r14
leaq -0x1(%rax), %rdx
movq %rsp, %r15
addq $0xe, %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq %r15, %rdi
xorl %esi, %esi
callq 0x10d0
cmpq $0x3, %r14
jb 0x2b33
leaq -0x2(%r14), %rax
xorl %ecx, %ecx
movb 0x2(%r12,%rcx), %dl
testb %dl, %dl
je 0x2b2b
movb %dl, (%r15,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0x2b1e
leaq -0x420(%rbp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x10d0
movq %r15, %rdi
callq 0x1210
testq %rax, %rax
je 0x2bcd
movq %rax, %rdi
callq 0x1260
movq %rsp, %r12
addq $0x19, %r14
andq $-0x10, %r14
subq %r14, %r12
movq %r12, %rsp
movq %r12, %rdi
movq %r15, %rsi
callq 0x1150
movq %r12, %rdi
callq 0x10b0
movabsq $0x6566696e614d2e2f, %rcx # imm = 0x6566696E614D2E2F
movq %rcx, (%rax,%r12)
movl $0x747365, 0x7(%rax,%r12) # imm = 0x747365
movq %r12, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x10f0
movl %eax, %r14d
movl %eax, %edi
callq 0x12b0
testl %r14d, %r14d
js 0x2be3
movq %r12, %rdi
callq 0x333a
movq %rax, %r14
movq %rax, %rdi
callq 0x10b0
leaq 0x1726(%rip), %rdi      # 0x42f1
jmp 0x2bff
xorl %edi, %edi
callq 0x1260
leaq -0x420(%rbp), %r14
movw $0x65, (%r14)
jmp 0x2bf0
leaq -0x420(%rbp), %r14
movw $0x78, (%r14)
movq %r14, %rdi
callq 0x10b0
leaq 0x16da(%rip), %rdi      # 0x42d9
movq %r14, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1040
movq %r14, %rdi
callq 0x10b0
movl %ebx, %edi
movq %r14, %rsi
movq %rax, %rdx
callq 0x10a0
leaq -0x20(%rbp), %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq