// File: dmonaldo[P]simple-network-file-system/FileSys.cpp
// Function: FileSys::rmdir(char const*)
void FileSys::rmdir(const char *name)
{
  struct dirblock_t curr_block_ptr;
  char file_name[MAX_FNAME_SIZE + 1];
  char curr_file_name[MAX_FNAME_SIZE + 1];
  strcpy(file_name, name);
  bfs.read_block(curr_dir, (void*)&curr_block_ptr);

  bool error = false;
  char buffer[1024];
  short found_block = 0;
  unsigned int found_index = 0;
  bool found = false;
  dirblock_t found_dir_ptr;
  //finds the directory to remove wthin the current directory, sets found
  //bool to true if it exists in the directory
  for (unsigned int i = 0; i < MAX_DIR_ENTRIES; i++){
    strcpy(curr_file_name, curr_block_ptr.dir_entries[i].name);
    if (strcmp(curr_file_name, file_name) == 0){
      error = false;
      found = true;
      found_index = i;
      found_block = curr_block_ptr.dir_entries[found_index].block_num;
      bfs.read_block(found_block, (void*)&found_dir_ptr);
    }else{
      error = true;
    }
  }
  if(found){
    error = false;
  }
  //ensures the found directory is a directory and not a file
  if(!is_directory(found_block)){
    if(!error){
      error = true;
      strcpy(buffer, "500: File is not a directory\r\n");
    }
  }
  //if the found bool was not set to true then the filename passed in does
  //not exist and the error message is wrote into the buffer
  else if(!found && !error){
    error = true;
    strcpy(buffer ,"503: File does not exsit\r\n");
  }
  //if the found directory is a directory but still contains files it cannot
  //be removed and this error is wrote into the buffer
  else if(found_dir_ptr.num_entries != 0 && !error){
    error = true;
    strcpy(buffer, "507: Directory is not empty\r\n");
  }
  else{
    //if error is still set to false then remove the directory and set all
    //values back to null/0 as well as reclaiming its block num
    if(found && !error){
      bfs.reclaim_block(curr_block_ptr.dir_entries[found_index].block_num);
      curr_block_ptr.dir_entries[found_index].name[0] = '\0';
      curr_block_ptr.dir_entries[found_index].block_num = 0;
      curr_block_ptr.num_entries--;
      bfs.write_block(curr_dir, (void*)&curr_block_ptr);
      strcpy(buffer, "200 OK\r\n Length: 0\r\n\r\n");
    }
  }
  send(fs_sock, buffer, sizeof(buffer), 0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x550, %rsp            # imm = 0x550
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x548(%rbp)
leaq -0x9a(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x3320
movq -0x548(%rbp), %rax
leaq -0x90(%rbp), %rdx
movq %rax, %rdi
movswl 0x4(%rax), %esi
callq 0x3c20
movb $0x0, -0xa5(%rbp)
movw $0x0, -0x4b2(%rbp)
movl $0x0, -0x4b8(%rbp)
movb $0x0, -0x4b9(%rbp)
movl $0x0, -0x540(%rbp)
cmpl $0xa, -0x540(%rbp)
jae 0x4652
leaq -0xa4(%rbp), %rdi
leaq -0x90(%rbp), %rsi
addq $0x8, %rsi
movl -0x540(%rbp), %eax
imulq $0xc, %rax, %rax
addq %rax, %rsi
callq 0x3320
leaq -0xa4(%rbp), %rdi
leaq -0x9a(%rbp), %rsi
callq 0x3480
cmpl $0x0, %eax
jne 0x4635
movq -0x548(%rbp), %rdi
movb $0x0, -0xa5(%rbp)
movb $0x1, -0x4b9(%rbp)
movl -0x540(%rbp), %eax
movl %eax, -0x4b8(%rbp)
leaq -0x90(%rbp), %rax
addq $0x8, %rax
movl -0x4b8(%rbp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movw 0xa(%rax), %ax
movw %ax, -0x4b2(%rbp)
leaq -0x53c(%rbp), %rdx
movswl -0x4b2(%rbp), %esi
callq 0x3c20
jmp 0x463c
movb $0x1, -0xa5(%rbp)
jmp 0x463e
movl -0x540(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x540(%rbp)
jmp 0x4593
testb $0x1, -0x4b9(%rbp)
je 0x4662
movb $0x0, -0xa5(%rbp)
movq -0x548(%rbp), %rdi
movswl -0x4b2(%rbp), %esi
callq 0x4470
testb $0x1, %al
jne 0x46a1
testb $0x1, -0xa5(%rbp)
jne 0x469c
movb $0x1, -0xa5(%rbp)
leaq -0x4b0(%rbp), %rdi
leaq 0x7aac(%rip), %rsi      # 0xc143
callq 0x3320
jmp 0x47c0
testb $0x1, -0x4b9(%rbp)
jne 0x46d2
testb $0x1, -0xa5(%rbp)
jne 0x46d2
movb $0x1, -0xa5(%rbp)
leaq -0x4b0(%rbp), %rdi
leaq 0x7a9a(%rip), %rsi      # 0xc162
callq 0x3320
jmp 0x47be
cmpl $0x0, -0x538(%rbp)
je 0x4703
testb $0x1, -0xa5(%rbp)
jne 0x4703
movb $0x1, -0xa5(%rbp)
leaq -0x4b0(%rbp), %rdi
leaq 0x7a84(%rip), %rsi      # 0xc17d
callq 0x3320
jmp 0x47bc
testb $0x1, -0x4b9(%rbp)
je 0x47ba
testb $0x1, -0xa5(%rbp)
jne 0x47ba
movq -0x548(%rbp), %rdi
leaq -0x90(%rbp), %rax
addq $0x8, %rax
movl -0x4b8(%rbp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movswl 0xa(%rax), %esi
callq 0x3b70
movq -0x548(%rbp), %rax
leaq -0x90(%rbp), %rcx
addq $0x8, %rcx
movl -0x4b8(%rbp), %edx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
movb $0x0, (%rcx)
leaq -0x90(%rbp), %rcx
addq $0x8, %rcx
movl -0x4b8(%rbp), %edx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
movw $0x0, 0xa(%rcx)
movl -0x8c(%rbp), %ecx
addl $-0x1, %ecx
movl %ecx, -0x8c(%rbp)
leaq -0x90(%rbp), %rdx
movq %rax, %rdi
movswl 0x4(%rax), %esi
callq 0x3c50
leaq -0x4b0(%rbp), %rdi
leaq 0x7977(%rip), %rsi      # 0xc12c
callq 0x3320
jmp 0x47bc
jmp 0x47be
jmp 0x47c0
movq -0x548(%rbp), %rax
movl 0x8(%rax), %edi
leaq -0x4b0(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
xorl %ecx, %ecx
callq 0x31d0
addq $0x550, %rsp            # imm = 0x550
popq %rbp
retq
nopw %cs:(%rax,%rax)