// File: /workspace/github_c_makefile_star2_O3/evanwire[P]GitClone/server.c
// Function: currentVersion
void currentVersion(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);

  //Report an error assuming the project does not exist
  if(ENOENT == errno){
    closedir(dir);
    strcpy(sendBuff, "x");
    printf("Message: -%s- size: %d\n", sendBuff, strlen(sendBuff));
    write(connfd, sendBuff, strlen(sendBuff));
    return;

  //project does exist, so continue
  }else if (dir){
    //do nothing and continue

  //opendir() failed
  }else{
    printf("\nERROR: opendir() failed.\n");
    strcpy(sendBuff, "e");
    printf("Message: -%s- size: %d\n", sendBuff, strlen(sendBuff));
    write(connfd, sendBuff, strlen(sendBuff));
    return;
  }

  //Open the needed Manifest file
  char* manifestPath = (char*)malloc(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
    */
  if(manifest_fd < 0){
    printf("\nError: Was unable to open/create the .Manifest file. Please try again.\n");
    exit(1);
  }
  char* fileContents=readFile(manifestPath);
  int j=0;
  //read current project version
  char* currentChar=(char*)malloc(sizeof(char)*2);
  memset(currentChar,'\0',sizeof(currentChar));
  currentChar[0]=fileContents[j];
  strcpy(sendBuff,currentChar);
  j++;
  currentChar[0]=fileContents[j];
  strcat(sendBuff,currentChar);
  j++;
  while (fileContents[j]!='\n'){
      currentChar[0]=fileContents[j];
      strcat(sendBuff,currentChar);
      j++;
  }
  strcat(sendBuff,"\n");
  j++;

  //now we read all filenames and their version numbers. each iteration is one line
  // of .Manifest

  while(fileContents[j]!='\0'){
      //read and store in sendBuff until first ~
        while(fileContents[j]!='~'){
            currentChar[0]=fileContents[j];
            strcat(sendBuff,currentChar);
            j++;
        }
        j++;
        strcat(sendBuff,"~");

      //read and store in sendBuff until second ~
      while(fileContents[j]!='~'){
            currentChar[0]=fileContents[j];
            strcat(sendBuff,currentChar);
            j++;
        }


      //read and DON'T store until end of line except \n
      while(fileContents[j]!='\n'){
            j++;
        }
        strcat(sendBuff,"\n");
        j++;

  }


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

  //cleanup
  closedir(dir);
  close(manifest_fd);
  free(manifestPath);
  free(currentChar);
  free(fileContents);


}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x438, %rsp            # imm = 0x438
movl %esi, %r15d
movq %rdi, %r14
callq 0x10b0
movq %rax, %rbx
leaq -0x1(%rax), %rdx
movq %rsp, %r12
addq $0xe, %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
movq %r12, %rdi
xorl %esi, %esi
callq 0x10d0
cmpq $0x3, %rbx
jb 0x2c77
leaq -0x2(%rbx), %rax
xorl %ecx, %ecx
movb 0x2(%r14,%rcx), %dl
testb %dl, %dl
je 0x2c6f
movb %dl, (%r12,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0x2c62
leaq -0x460(%rbp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x10d0
movq %r12, %rdi
callq 0x1220
movq %rax, %r14
callq 0x1030
cmpl $0x2, (%rax)
jne 0x2cb8
movq %r14, %rdi
callq 0x1270
leaq -0x460(%rbp), %rbx
movw $0x78, (%rbx)
jmp 0x2f17
testq %r14, %r14
je 0x2eff
movq %r14, -0x48(%rbp)
movl %r15d, -0x30(%rbp)
addq $0xa, %rbx
movq %rbx, %rdi
callq 0x11e0
movq %rax, %rbx
movq %rax, %rdi
movq %r12, %rsi
callq 0x1160
movq %rbx, %rdi
callq 0x10b0
movabsq $0x6566696e614d2e2f, %rcx # imm = 0x6566696E614D2E2F
movq %rcx, (%rbx,%rax)
movl $0x747365, 0x7(%rbx,%rax) # imm = 0x747365
movq %rbx, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x1100
testl %eax, %eax
js 0x2f58
movl %eax, -0x2c(%rbp)
movq %rbx, -0x40(%rbp)
movq %rbx, %rdi
callq 0x3333
movq %rax, %r12
movl $0x2, %edi
callq 0x11e0
movq %rax, %r13
movq $0x0, (%rax)
movb (%r12), %al
movb %al, (%r13)
leaq -0x460(%rbp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1160
movb 0x1(%r12), %al
movb %al, (%r13)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1280
movb 0x2(%r12), %al
movl $0x3, %r14d
cmpb $0xa, %al
je 0x2d98
movl $0x3, %r14d
movb %al, (%r13)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1280
movb (%r12,%r14), %al
incq %r14
cmpb $0xa, %al
jne 0x2d7e
leaq -0x460(%rbp), %rdi
callq 0x10b0
movw $0xa, -0x460(%rbp,%rax)
movslq %r14d, %rax
movb (%r12,%rax), %al
testb %al, %al
je 0x2e9a
movabsq $0x100000000, %r15      # imm = 0x100000000
leaq 0x1(%r12), %rcx
movq %rcx, -0x58(%rbp)
leaq 0x2(%r12), %rcx
movq %rcx, -0x50(%rbp)
leaq -0x460(%rbp), %rbx
movq %r12, -0x38(%rbp)
cmpb $0x7e, %al
je 0x2e0c
movslq %r14d, %r12
addq -0x58(%rbp), %r12
movb %al, (%r13)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1280
movb (%r12), %al
incl %r14d
incq %r12
cmpb $0x7e, %al
jne 0x2def
movq %rbx, %rdi
callq 0x10b0
movw $0x7e, -0x460(%rbp,%rax)
movslq %r14d, %r14
movq -0x38(%rbp), %r12
movb 0x1(%r12,%r14), %al
cmpb $0x7e, %al
jne 0x2e33
incq %r14
jmp 0x2e67
movq -0x50(%rbp), %rcx
leaq (%rcx,%r14), %r12
shlq $0x20, %r14
addq %r15, %r14
movb %al, (%r13)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1280
movb (%r12), %al
addq %r15, %r14
incq %r12
cmpb $0x7e, %al
jne 0x2e42
sarq $0x20, %r14
movq -0x38(%rbp), %r12
movq %r14, %rax
addq %r12, %rax
incl %r14d
cmpb $0xa, (%rax)
leaq 0x1(%rax), %rax
jne 0x2e6d
movq %rbx, %rdi
callq 0x10b0
movw $0xa, -0x460(%rbp,%rax)
movslq %r14d, %rax
movb (%r12,%rax), %al
testb %al, %al
jne 0x2de4
leaq -0x460(%rbp), %rbx
movq %rbx, %rdi
callq 0x10b0
leaq 0x1429(%rip), %rdi      # 0x42d9
movq %rbx, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1040
movq %rbx, %rdi
callq 0x10b0
movl -0x30(%rbp), %edi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x10a0
movq -0x48(%rbp), %rdi
callq 0x1270
movl -0x2c(%rbp), %edi
callq 0x12c0
movq -0x40(%rbp), %rdi
callq 0x11a0
movq %r13, %rdi
callq 0x11a0
movq %r12, %rdi
callq 0x11a0
jmp 0x2f49
leaq 0x1793(%rip), %rdi      # 0x4699
callq 0x1240
leaq -0x460(%rbp), %rbx
movw $0x65, (%rbx)
movq %rbx, %rdi
callq 0x10b0
leaq 0x13b3(%rip), %rdi      # 0x42d9
movq %rbx, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1040
movq %rbx, %rdi
callq 0x10b0
movl %r15d, %edi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x10a0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1754(%rip), %rdi      # 0x46b3
callq 0x1240
movl $0x1, %edi
callq 0x11b0