// File: /workspace/github_c_makefile_star2_O1/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 0x2b5c
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 0x10e0
movl %eax, %ebx
testl %eax, %eax
jns 0x2b65
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 0x10e0
testl %eax, %eax
js 0x2b7a
movl %eax, %r14d
movl %eax, %edi
callq 0x1240
movq %rbx, %rdi
callq 0x43c0
movq %rax, %rbx
cmpb $0x0, (%rax)
jne 0x26f9
leaq 0x32b0(%rip), %rdi      # 0x599c
callq 0x11d0
movl %r14d, %edi
callq 0x1240
movl %r14d, -0x64(%rbp)
movl $0x8, %edi
callq 0x1180
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1140
leaq 0x2fa2(%rip), %rbx      # 0x56be
leaq -0x90(%rbp), %r15
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x1230
testq %rax, %rax
jne 0x2723
movq %r12, %rdi
callq 0x10a0
movq %rsp, -0x50(%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), %rbx
movq %r12, -0x70(%rbp)
movq %rsp, %r12
leaq 0x12(%rax), %r13
andq $-0x10, %r13
movq %r12, %r14
subq %r13, %r14
movq %r14, %rsp
negq %r13
movq %r14, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x10c0
movw $0x2067, (%r12,%r13)    # imm = 0x2067
movq -0x70(%rbp), %rcx
testq %r15, %r15
je 0x27df
movl $0x2, %eax
xorl %esi, %esi
movb (%rcx,%rsi), %dl
testb %dl, %dl
je 0x27d7
cltq
movb %dl, (%r14,%rax)
incl %eax
incq %rsi
cmpq %rsi, %r15
jne 0x27c8
leaq 0x2ba6(%rip), %rdi      # 0x538c
movq %r14, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1040
movq %r14, %rdi
callq 0x4202
movq %rax, %r15
leaq 0x2dbd(%rip), %rsi      # 0x55c2
movq %rax, %rdi
callq 0x11a0
testl %eax, %eax
je 0x2b31
leaq 0x2859(%rip), %rsi      # 0x5075
movq %r15, %rdi
callq 0x11a0
testl %eax, %eax
je 0x2b3a
leaq -0x78(%rbp), %rbx
movq %r15, (%rbx)
leaq 0x2c90(%rip), %r14      # 0x54ca
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1230
movq %rax, %rdi
callq 0x11f0
movl %eax, -0x34(%rbp)
movslq %eax, %r15
leaq (,%r15,4), %r12
movq %r12, %rdi
callq 0x1180
movq %rax, -0x48(%rbp)
movq %r12, %rdi
callq 0x1180
movq %rax, -0x40(%rbp)
shlq $0x3, %r15
movq %r15, %rdi
callq 0x1180
movq %rax, -0x58(%rbp)
movq %r15, %rdi
callq 0x1180
movq %rax, -0x60(%rbp)
movq (%rbx), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1230
testq %rax, %rax
je 0x2976
xorl %r12d, %r12d
leaq 0x2c19(%rip), %rbx      # 0x54cc
leaq -0x30(%rbp), %r14
movq %rax, -0x30(%rbp)
movq %rax, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x1230
testq %rax, %rax
je 0x2956
movq %rax, %r15
xorl %r13d, %r13d
cmpl $0x2, %r13d
je 0x290a
cmpl $0x1, %r13d
je 0x28f7
testl %r13d, %r13d
jne 0x291c
movq %r15, %rdi
callq 0x11f0
movq -0x40(%rbp), %rcx
jmp 0x2916
movq -0x40(%rbp), %rax
movslq (%rax,%r12,4), %rdi
callq 0x1180
movq -0x58(%rbp), %rcx
jmp 0x292d
movq %r15, %rdi
callq 0x11f0
movq -0x48(%rbp), %rcx
movl %eax, (%rcx,%r12,4)
jmp 0x293c
movq -0x48(%rbp), %rax
movslq (%rax,%r12,4), %rdi
callq 0x1180
movq -0x60(%rbp), %rcx
movq %rax, (%rcx,%r12,8)
movq %rax, %rdi
movq %r15, %rsi
callq 0x1140
incl %r13d
movq -0x30(%rbp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x1230
movq %rax, %r15
testq %rax, %rax
jne 0x28d8
incq %r12
movq -0x78(%rbp), %rdi
leaq 0x2b66(%rip), %rsi      # 0x54ca
leaq -0x78(%rbp), %rdx
callq 0x1230
testq %rax, %rax
jne 0x28b7
movl -0x34(%rbp), %eax
movl %eax, %r12d
testl %eax, %eax
jle 0x2adb
leaq -0x30(%rbp), %r15
xorl %ebx, %ebx
movq %r12, -0x80(%rbp)
movq %rsp, -0x88(%rbp)
movq -0x40(%rbp), %rax
movl (%rax,%rbx,4), %eax
addq $0xf, %rax
andq $-0x10, %rax
movq %rsp, %r14
subq %rax, %r14
movq %r14, %rsp
movq -0x58(%rbp), %rax
movq (%rax,%rbx,8), %rdi
movq %rdi, -0x30(%rbp)
leaq 0x2c35(%rip), %r12      # 0x55f5
movq %r12, %rsi
movq %r15, %rdx
callq 0x1230
movq %r14, %rdi
movq %rax, %rsi
callq 0x1140
movq -0x30(%rbp), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x1230
testq %rax, %rax
je 0x2ac4
movq %rax, %r12
movq %r12, %rdi
movl $0x2e, %esi
callq 0x1090
movq %rax, %r13
movq %r14, %rdi
callq 0x10a0
movw $0x2f, (%r14,%rax)
movq %r14, %rdi
movq %r12, %rsi
callq 0x1220
testq %r13, %r13
je 0x2a55
movq %r14, %rdi
movl $0x241, %esi            # imm = 0x241
movl $0x1ff, %edx            # imm = 0x1FF
xorl %eax, %eax
callq 0x10e0
movl %eax, %r12d
testl %eax, %eax
js 0x2a8f
movq -0x60(%rbp), %rax
movq (%rax,%rbx,8), %rsi
movq -0x48(%rbp), %rax
movslq (%rax,%rbx,4), %rdx
movl %r12d, %edi
callq 0x1080
jmp 0x2a9d
movq -0x70(%rbp), %rdi
callq 0x11c0
movq %rax, %r12
testq %rax, %rax
jne 0x2a85
movq %r14, %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x10b0
testl %eax, %eax
jns 0x2a85
leaq 0x2ab3(%rip), %rdi      # 0x5531
xorl %eax, %eax
callq 0x1040
movq %r12, %rdi
callq 0x1200
jmp 0x2aa5
leaq 0x2a38(%rip), %rdi      # 0x54ce
xorl %eax, %eax
callq 0x1040
movl %r12d, %edi
callq 0x1240
movq -0x30(%rbp), %rdi
leaq 0x2b45(%rip), %rsi      # 0x55f5
movq %r15, %rdx
callq 0x1230
movq %rax, %r12
testq %rax, %rax
jne 0x29f1
movq -0x88(%rbp), %rsp
incq %rbx
movq -0x80(%rbp), %r12
cmpq %r12, %rbx
jne 0x298e
movq -0x48(%rbp), %rdi
callq 0x1160
movq -0x40(%rbp), %rdi
callq 0x1160
cmpl $0x0, -0x34(%rbp)
movq -0x58(%rbp), %r14
movq -0x60(%rbp), %r15
jle 0x2b17
xorl %ebx, %ebx
movq (%r14,%rbx,8), %rdi
callq 0x1160
movq (%r15,%rbx,8), %rdi
callq 0x1160
incq %rbx
cmpq %rbx, %r12
jne 0x2afd
movq %r14, %rdi
callq 0x1160
movq %r15, %rdi
callq 0x1160
movl -0x64(%rbp), %edi
callq 0x1240
jmp 0x2b46
leaq 0x2ece(%rip), %rdi      # 0x5a06
jmp 0x2b41
leaq 0x2e76(%rip), %rdi      # 0x59b7
callq 0x11d0
movq -0x50(%rbp), %rax
movq %rax, %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3397(%rip), %rdi      # 0x5efa
jmp 0x2b81
leaq 0x2dcc(%rip), %rdi      # 0x5938
callq 0x11d0
movl %ebx, %edi
callq 0x1240
jmp 0x2b86
leaq 0x2ec5(%rip), %rdi      # 0x5a46
callq 0x11d0
movl $0x1, %edi
callq 0x1170