// File: /workspace/github_c_makefile_star2_O2/evanwire[P]GitClone/client.c
// Function: upgrade
void upgrade(char* projectName){
  //check to see if project exists on client side
  //Verify the project exists on the client side
  DIR* dir = opendir(projectName);
  if(!dir){
    printf("\nError: the project you specified does not seem to exist on the client side. Please create the project before running again.\n");
    exit(1);
  }
  closedir(dir);

  //check to see if .Conflict exists
  char* conflictFilePath=(char*)malloc(strlen(projectName) + 10);
  strcpy(conflictFilePath, projectName);
  strcat(conflictFilePath, "/.Conflict");
  int conflict_fd=open(conflictFilePath,O_RDONLY);
  if(!(conflict_fd<0)){
    printf("There is at least one conflict between the server and client. Please resolve, update and try again.\n");
    close(conflict_fd);
    exit(1);
  }
  close(conflict_fd);

  //check to make sure .Update exists
  char* updateFilePath=(char*)malloc(strlen(projectName) + 9);
  strcpy(updateFilePath, projectName);
  strcat(updateFilePath, "/.Update");
  int update_fd=open(updateFilePath,O_RDONLY);
  if(update_fd<0){
    printf(".Update file does not exist. Please call update and try again.\n");
    exit(1);
  }
  close(update_fd);
  //turn .Update to string
  char* update_contents=readFile(updateFilePath);

  //check to see if .Update is empty, in which case this is up to date
  if(strlen(update_contents)==0){
    printf("The project is up to date.\n");
    close(update_fd);
  }
  char* updateCopy=(char*)malloc(sizeof(strlen(update_contents)+1));
  strcpy(updateCopy,update_contents);
  //now we look through lines of .Update looking for delete. This is easiest.


  char* updateToken;
  char* updateSaveptr;
  char* updateLine=(char*)malloc(strlen(update_contents)+1);
  while(updateToken=strtok_r(updateCopy,"\n",&updateSaveptr)){
    //if it's a delete we can take care of it now
    if(updateToken[0]=='D'){
      char* innerUpdateSaveptr;
      strcpy(updateLine,updateToken);


    }

    //modify or delete. copy file path to send contents string.
    else{

    }


  }
  char command[strlen(projectName) + 9];
  strcpy(command,"rm -rf ");
  strcat(command, projectName);
  system(command);
  int i;
  char to_be_written[strlen(projectName) + 3];
  memset(to_be_written, 0, sizeof(to_be_written));
  to_be_written[0] = 'g';
  to_be_written[1] = ' ';
  int message_idx = 2;
  for(i = 0; i < strlen(projectName); i++){
    if(projectName[i]){
      to_be_written[message_idx] = projectName[i];
      message_idx++;
    }
  }
  printf("\nWritten to server: -%s- Size: %d passed in: -%s-\n", to_be_written, sizeof(to_be_written), projectName);

  //Connect to server, store the response
  char* response = connect_to_WTFserver(to_be_written);
  if(strcmp(response, "x") == 0){
    printf("\nError: the project you specified does not exist on the server.\n");
    return;
  }else if(strcmp(response, "e") == 0){
    printf("\nError: the server failed to open the project you specified. Please try again.\n");
    return;
  }

  //Prepare for tokenizing string and storing results
  char* token;
  char* saveptr = response;



  //Isolate the number of files
  token = strtok_r(saveptr, "$", &saveptr);
  int num_files = atoi(token);

  int* file_sizes = malloc(num_files * sizeof(int));
  int* file_name_sizes = malloc(num_files * sizeof(int));
  char** file_names = malloc(num_files * sizeof(char*));
  char** file_contents = malloc(num_files * sizeof(char*));

  int index = 0;

  //loop through the rest of the tokens
  while(token = strtok_r(saveptr, "$", &saveptr)){
    //for each token, take the specific pieces of information from each token
    char* inner_token;
    char* inner_saveptr = token;
    int data_type = 0;
    while(inner_token = strtok_r(inner_saveptr, "&", &inner_saveptr)){
      if(data_type == 0){
        file_name_sizes[index] = atoi(inner_token);
      }else if(data_type == 1){
        file_names[index] = malloc(file_name_sizes[index] * sizeof(char));
        strcpy(file_names[index], inner_token);
      }else if(data_type == 2){
        file_sizes[index] = atoi(inner_token);
      }else{
        file_contents[index] = malloc(file_sizes[index] * sizeof(char));
        strcpy(file_contents[index], inner_token);
      }
      data_type++;
    }
    index++;
  }

  //Tokenize each file name to open any necessary sub directories and create necessary files
  for(i = 0; i < num_files; i++){
    //To store the file path at a given moment
    char file_path_so_far[file_name_sizes[i]];

    //To tokenize the file path
    char* file_name_token;
    char* file_name_saveptr = file_names[i];

    //Get the current directory from path, add it to the path so far
    file_name_token = strtok_r(file_name_saveptr, "/", &file_name_saveptr);
    strcpy(file_path_so_far, file_name_token);


    //Loop through rest of file path, creating directories as needed
    while(file_name_token = strtok_r(file_name_saveptr, "/", &file_name_saveptr)){

      //if it has a '.' in it, it must not be a directory since we removed the current directory from the list of tokens already
      if(strchr(file_name_token, '.') != NULL){
        strcat(file_path_so_far, "/");
        strcat(file_path_so_far, file_name_token);
        int file_fd = open(file_path_so_far, O_WRONLY | O_TRUNC | O_CREAT, 0777);
        if(file_fd < 0){
          printf("\nError: was unable to create file %s. Will skip and continue replicating the rest of the project.\n");
        }else{
          int bytes_written = write(file_fd, file_contents[i], file_sizes[i]);
        }
        close(file_fd);
      //If its not a file, it must be a directory. Create it if the directory does not already exist, and add the directory's name to the current file path  
      }else{
          strcat(file_path_so_far, "/");
          strcat(file_path_so_far, file_name_token);
          DIR* dir = opendir(projectName);
          if(!dir){
            int result = mkdir(file_path_so_far, 0777);
            if(result < 0){
              printf("\nError: could not create directory %s. Will skip continue replicating the rest of the project.\n");
            }
          }
          closedir(dir);
      }
    }

  }


  free(file_sizes);
  free(file_name_sizes);
  for(i = 0; i < num_files; i ++){
    free(file_names[i]);
    free(file_contents[i]);
  }
  free(file_names);
  free(file_contents);

  

  close(update_fd);
  return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdi, %r12
callq 0x11c0
testq %rax, %rax
je 0x29e6
movq %rax, %rdi
callq 0x1200
movq %r12, %rdi
callq 0x10a0
leaq 0xa(%rax), %rdi
callq 0x1180
movq %rax, %rbx
movq %rax, %rdi
movq %r12, %rsi
callq 0x1140
movq %rbx, %rdi
callq 0x10a0
movabsq $0x696c666e6f432e2f, %rcx # imm = 0x696C666E6F432E2F
movq %rcx, (%rbx,%rax)
movl $0x746369, 0x7(%rbx,%rax) # imm = 0x746369
movq %rbx, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x10f0
movl %eax, %ebx
testl %eax, %eax
jns 0x29ef
movl %ebx, %edi
callq 0x1240
movq %r12, %rdi
callq 0x10a0
leaq 0x9(%rax), %rdi
callq 0x1180
movq %rax, %rbx
movq %rax, %rdi
movq %r12, %rsi
callq 0x1140
movq %rbx, %rdi
callq 0x10a0
movabsq $0x6574616470552e2f, %rcx # imm = 0x6574616470552E2F
movq %rcx, (%rbx,%rax)
movb $0x0, 0x8(%rbx,%rax)
movq %rbx, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x10f0
testl %eax, %eax
js 0x2a04
movl %eax, %r14d
movl %eax, %edi
callq 0x1240
movq %rbx, %rdi
callq 0x41b4
movq %rax, %rbx
cmpb $0x0, (%rax)
jne 0x25e1
leaq 0x33c8(%rip), %rdi      # 0x599c
callq 0x11d0
movl %r14d, %edi
callq 0x1240
movl %r14d, -0x5c(%rbp)
pushq $0x8
popq %rdi
callq 0x1180
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1140
leaq 0x30bc(%rip), %rbx      # 0x56be
leaq -0x88(%rbp), %r15
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x1230
testq %rax, %rax
jne 0x2609
movq %r12, %rdi
callq 0x10a0
movq %rsp, -0x78(%rbp)
movq %rsp, %rcx
addq $0x18, %rax
andq $-0x10, %rax
movq %rcx, %rbx
subq %rax, %rbx
movq %rbx, %rsp
negq %rax
movabsq $0x2066722d206d72, %rdx # imm = 0x2066722D206D72
movq %rdx, (%rcx,%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1220
movq %rbx, %rdi
callq 0x1120
movq %r12, %rdi
callq 0x10a0
movq %rax, %r15
leaq 0x3(%rax), %rdx
movq %r12, -0x68(%rbp)
movq %rsp, %r13
leaq 0x12(%rax), %rbx
andq $-0x10, %rbx
movq %r13, %r14
subq %rbx, %r14
movq %r14, %rsp
negq %rbx
xorl %r12d, %r12d
movq %r14, %rdi
xorl %esi, %esi
movq %rdx, -0x30(%rbp)
callq 0x10c0
movw $0x2067, (%r13,%rbx)    # imm = 0x2067
movq -0x68(%rbp), %rcx
pushq $0x2
popq %rax
cmpq %r12, %r15
je 0x26c4
movb (%rcx,%r12), %dl
testb %dl, %dl
je 0x26bf
cltq
movb %dl, (%r14,%rax)
incl %eax
incq %r12
jmp 0x26aa
leaq 0x2cc1(%rip), %rdi      # 0x538c
movq %r14, %rsi
movq -0x30(%rbp), %rdx
xorl %eax, %eax
callq 0x1040
movq %r14, %rdi
callq 0x401a
movzbl (%rax), %ecx
cmpl $0x65, %ecx
je 0x2700
cmpl $0x78, %ecx
jne 0x270a
cmpb $0x0, 0x1(%rax)
jne 0x270a
leaq 0x330b(%rip), %rdi      # 0x5a06
jmp 0x29ce
cmpb $0x0, 0x1(%rax)
je 0x29c7
leaq -0x70(%rbp), %rdx
movq %rax, (%rdx)
leaq 0x2db2(%rip), %rsi      # 0x54ca
movq %rax, %rdi
callq 0x1230
movq %rax, %rdi
callq 0x11f0
movq %rax, -0x58(%rbp)
movslq %eax, %r15
leaq (,%r15,4), %r12
movq %r12, %rdi
callq 0x1180
movq %rax, -0x30(%rbp)
movq %r12, %rdi
callq 0x1180
movq %rax, -0x48(%rbp)
shlq $0x3, %r15
movq %r15, %rdi
callq 0x1180
movq %rax, -0x40(%rbp)
movq %r15, %rdi
callq 0x1180
movq %rax, -0x50(%rbp)
xorl %r14d, %r14d
leaq 0x2d57(%rip), %r12      # 0x54cc
leaq -0x38(%rbp), %r13
movq -0x70(%rbp), %rdi
leaq 0x2d46(%rip), %rsi      # 0x54ca
leaq -0x70(%rbp), %rdx
callq 0x1230
testq %rax, %rax
je 0x2823
movq %rax, %rdi
movq %rax, -0x38(%rbp)
xorl %ebx, %ebx
movq %r12, %rsi
movq %r13, %rdx
callq 0x1230
testq %rax, %rax
je 0x281b
movq %rax, %r15
cmpl $0x2, %ebx
je 0x27e1
cmpl $0x1, %ebx
je 0x27ce
testl %ebx, %ebx
jne 0x27f3
movq %r15, %rdi
callq 0x11f0
movq -0x48(%rbp), %rcx
jmp 0x27ed
movq -0x48(%rbp), %rax
movslq (%rax,%r14,4), %rdi
callq 0x1180
movq -0x40(%rbp), %rcx
jmp 0x2804
movq %r15, %rdi
callq 0x11f0
movq -0x30(%rbp), %rcx
movl %eax, (%rcx,%r14,4)
jmp 0x2813
movq -0x30(%rbp), %rax
movslq (%rax,%r14,4), %rdi
callq 0x1180
movq -0x50(%rbp), %rcx
movq %rax, (%rcx,%r14,8)
movq %rax, %rdi
movq %r15, %rsi
callq 0x1140
incl %ebx
movq -0x38(%rbp), %rdi
jmp 0x279f
incq %r14
jmp 0x2779
xorl %eax, %eax
movq -0x58(%rbp), %r15
testl %r15d, %r15d
cmovlel %eax, %r15d
leaq -0x38(%rbp), %r12
xorl %ebx, %ebx
movq %r15, -0x58(%rbp)
cmpq %r15, %rbx
je 0x296f
movq %rsp, -0x80(%rbp)
movq -0x48(%rbp), %rax
movl (%rax,%rbx,4), %eax
addq $0xf, %rax
andq $-0x10, %rax
movq %rsp, %r13
subq %rax, %r13
movq %r13, %rsp
movq -0x40(%rbp), %rax
movq (%rax,%rbx,8), %rdi
movq %rdi, -0x38(%rbp)
leaq 0x2d83(%rip), %rsi      # 0x55f5
movq %r12, %rdx
callq 0x1230
movq %r13, %rdi
movq %rax, %rsi
callq 0x1140
movq -0x38(%rbp), %rdi
leaq 0x2d65(%rip), %rsi      # 0x55f5
movq %r12, %rdx
callq 0x1230
testq %rax, %rax
je 0x295f
movq %rax, %r15
movq %rax, %rdi
pushq $0x2e
popq %rsi
callq 0x1090
movq %rax, %r14
movq %r13, %rdi
callq 0x10a0
movw $0x2f, (%r13,%rax)
movq %r13, %rdi
movq %r15, %rsi
callq 0x1220
testq %r14, %r14
je 0x2907
movq %r13, %rdi
movl $0x241, %esi            # imm = 0x241
movl $0x1ff, %edx            # imm = 0x1FF
xorl %eax, %eax
callq 0x10f0
movl %eax, %r15d
testl %eax, %eax
js 0x2944
movq -0x50(%rbp), %rax
movq (%rax,%rbx,8), %rsi
movq -0x30(%rbp), %rax
movslq (%rax,%rbx,4), %rdx
movl %r15d, %edi
callq 0x1080
jmp 0x2952
movq -0x68(%rbp), %rdi
callq 0x11c0
movq %rax, %r15
testq %rax, %rax
jne 0x2937
movq %r13, %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x10b0
testl %eax, %eax
jns 0x2937
leaq 0x2c01(%rip), %rdi      # 0x5531
xorl %eax, %eax
callq 0x1040
movq %r15, %rdi
callq 0x1200
jmp 0x2885
leaq 0x2b83(%rip), %rdi      # 0x54ce
xorl %eax, %eax
callq 0x1040
movl %r15d, %edi
callq 0x1240
jmp 0x2885
movq -0x80(%rbp), %rsp
incq %rbx
movq -0x58(%rbp), %r15
jmp 0x283a
movq -0x30(%rbp), %rdi
callq 0x1160
movq -0x48(%rbp), %rdi
callq 0x1160
xorl %ebx, %ebx
movl -0x5c(%rbp), %r14d
cmpq %rbx, %r15
je 0x29ab
movq -0x40(%rbp), %rax
movq (%rax,%rbx,8), %rdi
callq 0x1160
movq -0x50(%rbp), %rax
movq (%rax,%rbx,8), %rdi
callq 0x1160
incq %rbx
jmp 0x2987
movq -0x40(%rbp), %rdi
callq 0x1160
movq -0x50(%rbp), %rdi
callq 0x1160
movl %r14d, %edi
callq 0x1240
jmp 0x29d3
leaq 0x2fe9(%rip), %rdi      # 0x59b7
callq 0x11d0
movq -0x78(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x350d(%rip), %rdi      # 0x5efa
jmp 0x2a0b
leaq 0x2f42(%rip), %rdi      # 0x5938
callq 0x11d0
movl %ebx, %edi
callq 0x1240
jmp 0x2a10
leaq 0x303b(%rip), %rdi      # 0x5a46
callq 0x11d0
pushq $0x1
popq %rdi
callq 0x1170