// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x2
popq %rax
cmpq %rax, %r12
jbe 0x2056
movb (%r15,%rax), %cl
testb %cl, %cl
je 0x2051
movb %cl, -0x2(%r14,%rax)
incq %rax
jmp 0x203f
leaq -0x420(%rbp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x10d0
movq %r14, %rdi
callq 0x1210
testq %rax, %rax
je 0x2090
movq %rax, %rdi
callq 0x1260
leaq -0x420(%rbp), %r14
movw $0x78, (%r14)
jmp 0x21f2
callq 0x1030
cmpl $0x2, (%rax)
jne 0x21d9
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 0x1270
movq %r15, %rdi
callq 0x1130
movq %r12, %rsp
leaq 0x21b2(%rip), %rdi      # 0x429e
movl $0x241, %esi            # imm = 0x241
movl $0x1ff, %edx            # imm = 0x1FF
xorl %eax, %eax
callq 0x1100
testl %eax, %eax
js 0x2230
movl %eax, %r15d
leaq 0x2189(%rip), %rsi      # 0x4298
pushq $0x4
popq %rdx
movl %eax, %edi
callq 0x10a0
testl %eax, %eax
js 0x2239
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 0x1150
movq %r12, %rdi
callq 0x10b0
movabsq $0x6566696e614d2e2f, %rcx # imm = 0x6566696E614D2E2F
movq %rcx, (%rax,%r12)
movl $0x747365, 0x7(%rax,%r12) # imm = 0x747365
leaq 0x213d(%rip), %rdi      # 0x42a8
movq %r12, %rsi
xorl %eax, %eax
callq 0x1040
leaq 0x2122(%rip), %rdi      # 0x429e
movq %r12, %rsi
callq 0x10e0
movl %r15d, %edi
callq 0x12b0
leaq -0x420(%rbp), %r14
movl $0xa302056, (%r14)      # imm = 0xA302056
movb $0x0, 0x4(%r14)
movq %r14, %rdi
callq 0x10b0
leaq 0x212b(%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 0x1260
jmp 0x2223
leaq 0x24b9(%rip), %rdi      # 0x4699
callq 0x1230
leaq -0x420(%rbp), %r14
movw $0x65, (%r14)
movq %r14, %rdi
callq 0x10b0
leaq 0x20d8(%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 0x247c(%rip), %rdi      # 0x46b3
jmp 0x2240
leaq 0x2414(%rip), %rdi      # 0x4654
callq 0x1230
pushq $0x1
popq %rdi
callq 0x11a0