// File: /workspace/github_c_makefile_star2_O0/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
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
callq 0x11b0
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x2e3b
leaq 0x48bd(%rip), %rdi      # 0x76e7
movb $0x0, %al
callq 0x1040
movl $0x1, %edi
callq 0x1170
movq -0x10(%rbp), %rdi
callq 0x11e0
movq -0x8(%rbp), %rdi
callq 0x10b0
movq %rax, %rdi
addq $0xa, %rdi
callq 0x1180
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
movq -0x8(%rbp), %rsi
callq 0x1140
movq -0x18(%rbp), %rdi
leaq 0x4f93(%rip), %rsi      # 0x7e08
callq 0x1200
movq -0x18(%rbp), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x10f0
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jl 0x2eb0
leaq 0x5394(%rip), %rdi      # 0x822b
movb $0x0, %al
callq 0x1040
movl -0x1c(%rbp), %edi
callq 0x1220
movl $0x1, %edi
callq 0x1170
movl -0x1c(%rbp), %edi
callq 0x1220
movq -0x8(%rbp), %rdi
callq 0x10b0
movq %rax, %rdi
addq $0x9, %rdi
callq 0x1180
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
movq -0x8(%rbp), %rsi
callq 0x1140
movq -0x28(%rbp), %rdi
leaq 0x4ec5(%rip), %rsi      # 0x7dae
callq 0x1200
movq -0x28(%rbp), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x10f0
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jge 0x2f1c
leaq 0x5385(%rip), %rdi      # 0x8290
movb $0x0, %al
callq 0x1040
movl $0x1, %edi
callq 0x1170
movl -0x2c(%rbp), %edi
callq 0x1220
movq -0x28(%rbp), %rdi
callq 0x5e50
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0x10b0
cmpq $0x0, %rax
jne 0x2f56
leaq 0x5389(%rip), %rdi      # 0x82d0
movb $0x0, %al
callq 0x1040
movl -0x2c(%rbp), %edi
callq 0x1220
movl $0x8, %edi
callq 0x1180
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x1140
movq -0x38(%rbp), %rdi
callq 0x10b0
movq %rax, %rdi
addq $0x1, %rdi
callq 0x1180
movq %rax, -0x58(%rbp)
movq -0x40(%rbp), %rdi
leaq 0x51b4(%rip), %rsi      # 0x8149
leaq -0x50(%rbp), %rdx
callq 0x1210
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
je 0x2fc7
movq -0x48(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x44, %eax
jne 0x2fc3
movq -0x58(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x1140
jmp 0x2fc5
jmp 0x2fc5
jmp 0x2f8a
movq -0x8(%rbp), %rdi
callq 0x10b0
movq %rax, %rcx
movq %rcx, %rax
addq $0x9, %rax
movq %rsp, %rdx
movq %rdx, -0x68(%rbp)
addq $0x18, %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x140(%rbp)
movq %rdi, %rsp
movq %rax, -0x70(%rbp)
leaq 0x5041(%rip), %rsi      # 0x8045
callq 0x1140
movq -0x140(%rbp), %rdi
movq -0x8(%rbp), %rsi
callq 0x1200
movq -0x140(%rbp), %rdi
callq 0x1120
movq -0x8(%rbp), %rdi
callq 0x10b0
movq %rax, %rdx
addq $0x3, %rdx
movq %rdx, -0x138(%rbp)
addq $0x12, %rax
andq $-0x10, %rax
movq %rsp, %rdi
subq %rax, %rdi
movq %rdi, -0x130(%rbp)
movq %rdi, %rsp
movq %rdx, -0x80(%rbp)
xorl %esi, %esi
callq 0x10d0
movq -0x130(%rbp), %rax
movb $0x67, (%rax)
movb $0x20, 0x1(%rax)
movl $0x2, -0x84(%rbp)
movl $0x0, -0x74(%rbp)
movslq -0x74(%rbp), %rax
movq %rax, -0x148(%rbp)
movq -0x8(%rbp), %rdi
callq 0x10b0
movq %rax, %rcx
movq -0x148(%rbp), %rax
cmpq %rcx, %rax
jae 0x30e7
movq -0x8(%rbp), %rax
movslq -0x74(%rbp), %rcx
cmpb $0x0, (%rax,%rcx)
je 0x30da
movq -0x130(%rbp), %rax
movq -0x8(%rbp), %rcx
movslq -0x74(%rbp), %rdx
movb (%rcx,%rdx), %dl
movslq -0x84(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x30dc
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x307e
movq -0x130(%rbp), %rsi
movq -0x138(%rbp), %rdx
movq -0x8(%rbp), %rcx
leaq 0x43e2(%rip), %rdi      # 0x74e2
movb $0x0, %al
callq 0x1040
movq -0x130(%rbp), %rdi
callq 0x5be0
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rdi
leaq 0x4ab2(%rip), %rsi      # 0x7bda
callq 0x1190
cmpl $0x0, %eax
jne 0x314f
leaq 0x481d(%rip), %rdi      # 0x7956
movb $0x0, %al
callq 0x1040
movl $0x1, -0x94(%rbp)
jmp 0x365e
movq -0x90(%rbp), %rdi
leaq 0x3f18(%rip), %rsi      # 0x7075
callq 0x1190
cmpl $0x0, %eax
jne 0x3184
leaq 0x4829(%rip), %rdi      # 0x7997
movb $0x0, %al
callq 0x1040
movl $0x1, -0x94(%rbp)
jmp 0x365e
jmp 0x3186
movq -0x90(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rdi
leaq 0x4845(%rip), %rsi      # 0x79e7
leaq -0xa8(%rbp), %rdx
callq 0x1210
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rdi
callq 0x11d0
movl %eax, -0xac(%rbp)
movslq -0xac(%rbp), %rdi
shlq $0x2, %rdi
callq 0x1180
movq %rax, -0xb8(%rbp)
movslq -0xac(%rbp), %rdi
shlq $0x2, %rdi
callq 0x1180
movq %rax, -0xc0(%rbp)
movslq -0xac(%rbp), %rdi
shlq $0x3, %rdi
callq 0x1180
movq %rax, -0xc8(%rbp)
movslq -0xac(%rbp), %rdi
shlq $0x3, %rdi
callq 0x1180
movq %rax, -0xd0(%rbp)
movl $0x0, -0xd4(%rbp)
movq -0xa8(%rbp), %rdi
leaq 0x47ac(%rip), %rsi      # 0x79e7
leaq -0xa8(%rbp), %rdx
callq 0x1210
movq %rax, -0xa0(%rbp)
cmpq $0x0, %rax
je 0x33c5
movq -0xa0(%rbp), %rax
movq %rax, -0xe8(%rbp)
movl $0x0, -0xec(%rbp)
movq -0xe8(%rbp), %rdi
leaq 0x476b(%rip), %rsi      # 0x79e9
leaq -0xe8(%rbp), %rdx
callq 0x1210
movq %rax, -0xe0(%rbp)
cmpq $0x0, %rax
je 0x33b1
cmpl $0x0, -0xec(%rbp)
jne 0x32c8
movq -0xe0(%rbp), %rdi
callq 0x11d0
movl %eax, %edx
movq -0xc0(%rbp), %rax
movslq -0xd4(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x339d
cmpl $0x1, -0xec(%rbp)
jne 0x3321
movq -0xc0(%rbp), %rax
movslq -0xd4(%rbp), %rcx
movslq (%rax,%rcx,4), %rdi
shlq $0x0, %rdi
callq 0x1180
movq %rax, %rdx
movq -0xc8(%rbp), %rax
movslq -0xd4(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0xc8(%rbp), %rax
movslq -0xd4(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0xe0(%rbp), %rsi
callq 0x1140
jmp 0x339b
cmpl $0x2, -0xec(%rbp)
jne 0x334b
movq -0xe0(%rbp), %rdi
callq 0x11d0
movl %eax, %edx
movq -0xb8(%rbp), %rax
movslq -0xd4(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x3399
movq -0xb8(%rbp), %rax
movslq -0xd4(%rbp), %rcx
movslq (%rax,%rcx,4), %rdi
shlq $0x0, %rdi
callq 0x1180
movq %rax, %rdx
movq -0xd0(%rbp), %rax
movslq -0xd4(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0xd0(%rbp), %rax
movslq -0xd4(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0xe0(%rbp), %rsi
callq 0x1140
jmp 0x339b
jmp 0x339d
movl -0xec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xec(%rbp)
jmp 0x3270
movl -0xd4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd4(%rbp)
jmp 0x322d
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0xac(%rbp), %eax
jge 0x35d7
movq -0xc0(%rbp), %rax
movslq -0x74(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq %rsp, %rcx
movq %rcx, -0xf8(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x150(%rbp)
movq %rcx, %rsp
movq %rax, -0x100(%rbp)
movq -0xc8(%rbp), %rax
movslq -0x74(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x110(%rbp)
movq -0x110(%rbp), %rdi
leaq 0x4419(%rip), %rsi      # 0x7852
leaq -0x110(%rbp), %rdx
callq 0x1210
movq -0x150(%rbp), %rdi
movq %rax, -0x108(%rbp)
movq -0x108(%rbp), %rsi
callq 0x1140
movq -0x110(%rbp), %rdi
leaq 0x43e5(%rip), %rsi      # 0x7852
leaq -0x110(%rbp), %rdx
callq 0x1210
movq %rax, -0x108(%rbp)
cmpq $0x0, %rax
je 0x35bf
movq -0x108(%rbp), %rdi
movl $0x2e, %esi
callq 0x10a0
cmpq $0x0, %rax
je 0x353e
movq -0x150(%rbp), %rdi
leaq 0x439f(%rip), %rsi      # 0x7852
callq 0x1200
movq -0x150(%rbp), %rdi
movq -0x108(%rbp), %rsi
callq 0x1200
movq -0x150(%rbp), %rdi
movl $0x241, %esi            # imm = 0x241
movl $0x1ff, %edx            # imm = 0x1FF
movb $0x0, %al
callq 0x10f0
movl %eax, -0x114(%rbp)
cmpl $0x0, -0x114(%rbp)
jge 0x3502
leaq 0x44f2(%rip), %rdi      # 0x79eb
movb $0x0, %al
callq 0x1040
jmp 0x3531
movl -0x114(%rbp), %edi
movq -0xd0(%rbp), %rax
movslq -0x74(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movq -0xb8(%rbp), %rax
movslq -0x74(%rbp), %rcx
movslq (%rax,%rcx,4), %rdx
callq 0x1090
movl %eax, -0x118(%rbp)
movl -0x114(%rbp), %edi
callq 0x1220
jmp 0x35ba
movq -0x150(%rbp), %rdi
leaq 0x4306(%rip), %rsi      # 0x7852
callq 0x1200
movq -0x150(%rbp), %rdi
movq -0x108(%rbp), %rsi
callq 0x1200
movq -0x8(%rbp), %rdi
callq 0x11b0
movq %rax, -0x120(%rbp)
cmpq $0x0, -0x120(%rbp)
jne 0x35ae
movq -0x150(%rbp), %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x10c0
movl %eax, -0x124(%rbp)
cmpl $0x0, -0x124(%rbp)
jge 0x35ac
leaq 0x44a9(%rip), %rdi      # 0x7a4e
movb $0x0, %al
callq 0x1040
jmp 0x35ae
movq -0x120(%rbp), %rdi
callq 0x11e0
jmp 0x345f
movq -0xf8(%rbp), %rax
movq %rax, %rsp
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x33cc
movq -0xb8(%rbp), %rdi
callq 0x1160
movq -0xc0(%rbp), %rdi
callq 0x1160
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0xac(%rbp), %eax
jge 0x3634
movq -0xc8(%rbp), %rax
movslq -0x74(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1160
movq -0xd0(%rbp), %rax
movslq -0x74(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1160
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x35f6
movq -0xc8(%rbp), %rdi
callq 0x1160
movq -0xd0(%rbp), %rdi
callq 0x1160
movl -0x2c(%rbp), %edi
callq 0x1220
movl $0x1, -0x94(%rbp)
movq -0x68(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopw (%rax,%rax)