// File: /workspace/github_c_makefile_star2_O1/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 $0x428, %rsp            # imm = 0x428
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 0x2c90
leaq -0x2(%rbx), %rax
xorl %ecx, %ecx
movb 0x2(%r14,%rcx), %dl
testb %dl, %dl
je 0x2c88
movb %dl, (%r12,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0x2c7b
leaq -0x450(%rbp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x10d0
movq %r12, %rdi
callq 0x1210
movq %rax, %r14
callq 0x1030
cmpl $0x2, (%rax)
jne 0x2cd1
movq %r14, %rdi
callq 0x1260
leaq -0x450(%rbp), %rbx
movw $0x78, (%rbx)
jmp 0x2f00
testq %r14, %r14
je 0x2ee8
movq %r14, -0x40(%rbp)
movl %r15d, -0x30(%rbp)
addq $0xa, %rbx
movq %rbx, %rdi
callq 0x11d0
movq %rax, %rbx
movq %rax, %rdi
movq %r12, %rsi
callq 0x1150
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 0x10f0
movl %eax, -0x2c(%rbp)
testl %eax, %eax
js 0x2f41
movq %rbx, -0x38(%rbp)
movq %rbx, %rdi
callq 0x333a
movq %rax, %r12
movl $0x2, %edi
callq 0x11d0
movq %rax, %r13
movq $0x0, (%rax)
movb (%r12), %al
movb %al, (%r13)
leaq -0x450(%rbp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1150
movb 0x1(%r12), %al
movb %al, (%r13)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1270
movb 0x2(%r12), %al
movl $0x3, %r14d
cmpb $0xa, %al
je 0x2db1
movl $0x3, %r14d
movb %al, (%r13)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1270
movb (%r12,%r14), %al
incq %r14
cmpb $0xa, %al
jne 0x2d97
leaq -0x450(%rbp), %rdi
callq 0x10b0
movw $0xa, -0x450(%rbp,%rax)
movslq %r14d, %rax
cmpb $0x0, (%r12,%rax)
je 0x2e83
leaq 0x1(%r12), %rax
movq %rax, -0x48(%rbp)
leaq -0x450(%rbp), %rbx
movslq %r14d, %r15
movb (%r12,%r15), %al
cmpb $0x7e, %al
je 0x2e10
addq -0x48(%rbp), %r15
movb %al, (%r13)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1270
movb (%r15), %al
incl %r14d
incq %r15
cmpb $0x7e, %al
jne 0x2df4
movq %rbx, %rdi
callq 0x10b0
movw $0x7e, -0x450(%rbp,%rax)
movslq %r14d, %r14
movb 0x1(%r12,%r14), %al
cmpb $0x7e, %al
jne 0x2e33
incq %r14
jmp 0x2e51
movb %al, (%r13)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1270
movb 0x2(%r12,%r14), %al
incq %r14
cmpb $0x7e, %al
jne 0x2e33
incl %r14d
movslq %r14d, %rax
addq %r12, %rax
incl %r14d
cmpb $0xa, (%rax)
leaq 0x1(%rax), %rax
jne 0x2e57
movq %rbx, %rdi
callq 0x10b0
movw $0xa, -0x450(%rbp,%rax)
movslq %r14d, %rax
cmpb $0x0, (%r12,%rax)
jne 0x2de5
leaq -0x450(%rbp), %rbx
movq %rbx, %rdi
callq 0x10b0
leaq 0x1440(%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 -0x40(%rbp), %rdi
callq 0x1260
movl -0x2c(%rbp), %edi
callq 0x12b0
movq -0x38(%rbp), %rdi
callq 0x1190
movq %r13, %rdi
callq 0x1190
movq %r12, %rdi
callq 0x1190
jmp 0x2f32
leaq 0x17ad(%rip), %rdi      # 0x469c
callq 0x1230
leaq -0x450(%rbp), %rbx
movw $0x65, (%rbx)
movq %rbx, %rdi
callq 0x10b0
leaq 0x13ca(%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 0x176e(%rip), %rdi      # 0x46b6
callq 0x1230
movl $0x1, %edi
callq 0x11a0