// File: /workspace/github_c_makefile_star2_O2/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, %r12d
movq %rdi, %r15
callq 0x10b0
movq %rax, %rbx
leaq -0x1(%rax), %rdx
movq %rsp, %r14
addq $0xe, %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
movq %r14, %rdi
xorl %esi, %esi
callq 0x10d0
pushq $0x2
popq %rax
cmpq %rax, %rbx
jbe 0x2bb6
movb (%r15,%rax), %cl
testb %cl, %cl
je 0x2bb1
movb %cl, -0x2(%r14,%rax)
incq %rax
jmp 0x2b9f
leaq -0x450(%rbp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x10d0
movq %r14, %rdi
callq 0x1210
movq %rax, %r15
callq 0x1030
cmpl $0x2, (%rax)
jne 0x2bf7
movq %r15, %rdi
callq 0x1260
leaq -0x450(%rbp), %rbx
movw $0x78, (%rbx)
jmp 0x2dfc
testq %r15, %r15
je 0x2de4
movq %r15, -0x40(%rbp)
movl %r12d, -0x30(%rbp)
addq $0xa, %rbx
movq %rbx, %rdi
callq 0x11d0
movq %rax, %rbx
movq %rax, %rdi
movq %r14, %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 0x1100
movl %eax, -0x2c(%rbp)
testl %eax, %eax
js 0x2e3d
movq %rbx, -0x38(%rbp)
movq %rbx, %rdi
callq 0x320a
movq %rax, %r12
pushq $0x2
popq %r14
movq %r14, %rdi
callq 0x11d0
movq %rax, %r13
andq $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 (%r12,%r14), %al
cmpb $0xa, %al
je 0x2cc3
movb %al, (%r13)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1270
incq %r14
jmp 0x2ca7
movq %r12, %rax
addq $0x2, %rax
movq %rax, -0x48(%rbp)
leaq -0x450(%rbp), %rdi
callq 0x10b0
movw $0xa, -0x450(%rbp,%rax)
leal 0x1(%r14), %eax
cltq
cmpb $0x0, (%r12,%rax)
je 0x2d7f
movslq %r14d, %rbx
leal 0x2(%rbx), %r15d
movq -0x48(%rbp), %rax
leaq (%rax,%rbx), %r14
incq %rbx
movb (%r12,%rbx), %al
cmpb $0x7e, %al
je 0x2d2d
movb %al, (%r13)
leaq -0x450(%rbp), %rdi
movq %r13, %rsi
callq 0x1270
incl %r15d
incq %rbx
incq %r14
jmp 0x2d07
leaq -0x450(%rbp), %rdi
callq 0x10b0
movw $0x7e, -0x450(%rbp,%rax)
movslq %r15d, %rbx
decq %rbx
movb (%r14), %al
cmpb $0x7e, %al
je 0x2d6b
movb %al, (%r13)
leaq -0x450(%rbp), %rdi
movq %r13, %rsi
callq 0x1270
incq %r14
incq %rbx
jmp 0x2d49
leaq 0x1(%rbx), %r14
cmpb $0xa, 0x1(%r12,%rbx)
movq %r14, %rbx
jne 0x2d6b
jmp 0x2cce
leaq -0x450(%rbp), %rbx
movq %rbx, %rdi
callq 0x10b0
leaq 0x1544(%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 0x2e2e
leaq 0x18ae(%rip), %rdi      # 0x4699
callq 0x1230
leaq -0x450(%rbp), %rbx
movw $0x65, (%rbx)
movq %rbx, %rdi
callq 0x10b0
leaq 0x14ce(%rip), %rdi      # 0x42d9
movq %rbx, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1040
movq %rbx, %rdi
callq 0x10b0
movl %r12d, %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 0x186f(%rip), %rdi      # 0x46b3
callq 0x1230
pushq $0x1
popq %rdi
callq 0x11a0