// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x538, %rsp            # imm = 0x538
movq %rdi, %rbx
leaq 0x1c(%rsp), %rdi
callq 0x3270
movswl 0x4(%rbx), %esi
leaq 0x30(%rsp), %rdx
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
callq 0x3956
xorl %r13d, %r13d
leaq 0x42(%rsp), %rbx
leaq 0x26(%rsp), %r12
xorl %r15d, %r15d
movl $0x0, 0xc(%rsp)
xorl %r14d, %r14d
leaq -0xa(%rbx), %rsi
movq %r12, %rdi
callq 0x3270
movq %r12, %rdi
leaq 0x1c(%rsp), %rsi
callq 0x3370
movb $0x1, %bpl
testl %eax, %eax
jne 0x3fa9
movzwl (%rbx), %r14d
movswl %r14w, %esi
movq 0x10(%rsp), %rdi
leaq 0xb0(%rsp), %rdx
callq 0x3956
movb $0x1, %r15b
xorl %ebp, %ebp
movl %r13d, 0xc(%rsp)
incq %r13
addq $0xc, %rbx
cmpq $0xa, %r13
jne 0x3f65
movzbl %bpl, %eax
xorl %ebx, %ebx
movq %r15, %r12
testb $0x1, %r12b
cmovel %eax, %ebx
movswl %r14w, %esi
leaq 0x130(%rsp), %r15
movq 0x10(%rsp), %rdi
movq %r15, %rdx
callq 0x3956
cmpl $-0x1, (%r15)
je 0x4008
testb %bl, %bl
jne 0x40c3
movups 0x61a1(%rip), %xmm0     # 0xa195
movups %xmm0, 0x13f(%rsp)
movups 0x6183(%rip), %xmm0     # 0xa186
jmp 0x40bb
testb $0x1, %r12b
jne 0x4013
testb %bpl, %bpl
je 0x408d
cmpl $0x0, 0xb4(%rsp)
je 0x4025
testb %bl, %bl
je 0x40a5
testb $0x1, %r12b
je 0x40c3
movl 0xc(%rsp), %eax
leaq (%rax,%rax,2), %rbx
leaq 0x30(%rsp), %r15
movswl 0x12(%r15,%rbx,4), %esi
movq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x38f6
movb $0x0, 0x8(%r15,%rbx,4)
movw $0x0, 0x12(%r15,%rbx,4)
leaq 0x30(%rsp), %rdx
decl 0x4(%rdx)
movswl 0x4(%r14), %esi
movq %r14, %rdi
callq 0x395c
movabsq $0xa0d0a0d30203a, %rax  # imm = 0xA0D0A0D30203A
movq %rax, 0x13f(%rsp)
movups 0x60e4(%rip), %xmm0     # 0xa16f
jmp 0x40bb
movups 0x611c(%rip), %xmm0     # 0xa1b0
movups %xmm0, 0x13b(%rsp)
movups 0x6102(%rip), %xmm0     # 0xa1a5
jmp 0x40bb
movups 0x6122(%rip), %xmm0     # 0xa1ce
movups %xmm0, 0x13e(%rsp)
movups 0x6105(%rip), %xmm0     # 0xa1c0
movaps %xmm0, 0x130(%rsp)
movq 0x10(%rsp), %rax
movl 0x8(%rax), %edi
leaq 0x130(%rsp), %rsi
movl $0x400, %edx            # imm = 0x400
xorl %ecx, %ecx
callq 0x3180
addq $0x538, %rsp            # imm = 0x538
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop