// File: /workspace/github_c_makefile_star2_O3/evanwire[P]GitClone/server.c
// Function: create
void create(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 already exists
  if(dir){
    closedir(dir);
    strcpy(sendBuff, "x");
    printf("Message: -%s- size: %d\n", sendBuff, strlen(sendBuff));
    write(connfd, sendBuff, strlen(sendBuff));
    return;

  //project does not currently exist, so we create it
  }else if (ENOENT == errno){
    char command[strlen(project_name) + 6];
    strcpy(command,"mkdir ");
    strcat(command, project_name);
    system(command);

  //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;
  }

  //Create the .Manifest file
  int manifest_fd = open(".Manifest", O_WRONLY | O_TRUNC | O_CREAT, 0777);

    /*
    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);
  }

  int write_size = write(manifest_fd, "V 0\n", 4);
  if(write_size < 0){
    printf("\nError: Was unable to write to the .Manifest file. Please try again.\n");
    exit(1);
  }

  char filepath[10 + strlen(project_name)];
  strcpy(filepath, project_name);
  strcat(filepath, "/.Manifest");
  printf("\n%s\n", filepath);
  rename(".Manifest", filepath);
  /*
  TODO: Make the manifest file header more unique to each project
  */

  close(manifest_fd);
  //send manifest to client
  strcpy(sendBuff, "V 0\n");
  printf("Message: -%s- size: %d\n", sendBuff, strlen(sendBuff));
  write(connfd, sendBuff, strlen(sendBuff));
  //cleanup
  closedir(dir);


}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x400, %rsp            # imm = 0x400
movl %esi, %ebx
movq %rdi, %r15
callq 0x10b0
movq %rax, %r12
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
cmpq $0x3, %r12
jb 0x20cc
addq $-0x2, %r12
xorl %eax, %eax
movb 0x2(%r15,%rax), %cl
testb %cl, %cl
je 0x20c4
movb %cl, (%r14,%rax)
incq %rax
cmpq %rax, %r12
jne 0x20b7
leaq -0x420(%rbp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x10d0
movq %r14, %rdi
callq 0x1220
testq %rax, %rax
je 0x2106
movq %rax, %rdi
callq 0x1270
leaq -0x420(%rbp), %r14
movw $0x78, (%r14)
jmp 0x226a
callq 0x1030
cmpl $0x2, (%rax)
jne 0x2251
movq %r14, %rdi
callq 0x10b0
movq %rsp, %r12
movq %rsp, %rcx
addq $0x15, %rax
andq $-0x10, %rax
movq %rcx, %r15
subq %rax, %r15
movq %r15, %rsp
negq %rax
movl $0x69646b6d, (%rcx,%rax) # imm = 0x69646B6D
movl $0x207269, 0x3(%r15)    # imm = 0x207269
movq %r15, %rdi
movq %r14, %rsi
callq 0x1280
movq %r15, %rdi
callq 0x1140
movq %r12, %rsp
leaq 0x213c(%rip), %rdi      # 0x429e
movl $0x241, %esi            # imm = 0x241
movl $0x1ff, %edx            # imm = 0x1FF
xorl %eax, %eax
callq 0x1100
testl %eax, %eax
js 0x22a8
movl %eax, %r15d
leaq 0x2113(%rip), %rsi      # 0x4298
movl $0x4, %edx
movl %eax, %edi
callq 0x10a0
testl %eax, %eax
js 0x22b1
movq %r14, %rdi
callq 0x10b0
movq %rsp, %r12
addq $0x19, %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
movq %r12, %rdi
movq %r14, %rsi
callq 0x1160
movq %r12, %rdi
callq 0x10b0
movabsq $0x6566696e614d2e2f, %rcx # imm = 0x6566696E614D2E2F
movq %rcx, (%rax,%r12)
movl $0x747365, 0x7(%rax,%r12) # imm = 0x747365
leaq 0x20c5(%rip), %rdi      # 0x42a8
movq %r12, %rsi
xorl %eax, %eax
callq 0x1040
leaq 0x20aa(%rip), %rdi      # 0x429e
movq %r12, %rsi
callq 0x10e0
movl %r15d, %edi
callq 0x12c0
leaq -0x420(%rbp), %r14
movl $0xa302056, (%r14)      # imm = 0xA302056
movb $0x0, 0x4(%r14)
movq %r14, %rdi
callq 0x10b0
leaq 0x20b3(%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
xorl %edi, %edi
callq 0x1270
jmp 0x229b
leaq 0x2441(%rip), %rdi      # 0x4699
callq 0x1240
leaq -0x420(%rbp), %r14
movw $0x65, (%r14)
movq %r14, %rdi
callq 0x10b0
leaq 0x2060(%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
leaq 0x2404(%rip), %rdi      # 0x46b3
jmp 0x22b8
leaq 0x239c(%rip), %rdi      # 0x4654
callq 0x1240
movl $0x1, %edi
callq 0x11b0