// File: /workspace/github_cpp_makefile_star2_O1/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 0x26(%rsp), %r14
movq %r14, %rdi
callq 0x3270
movswl 0x4(%rbx), %esi
leaq 0x30(%rsp), %rdx
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
callq 0x3924
xorl %r12d, %r12d
leaq 0x42(%rsp), %rbx
leaq 0x1c(%rsp), %r15
xorl %r13d, %r13d
movl $0x0, 0xc(%rsp)
xorl %ebp, %ebp
leaq -0xa(%rbx), %rsi
movq %r15, %rdi
callq 0x3270
movq %r15, %rdi
movq %r14, %rsi
callq 0x3370
movb $0x1, %cl
testl %eax, %eax
jne 0x3f62
movzwl (%rbx), %ebp
movswl %bp, %esi
movq 0x10(%rsp), %rdi
leaq 0xb0(%rsp), %rdx
callq 0x3924
movb $0x1, %r13b
xorl %ecx, %ecx
movl %r12d, 0xc(%rsp)
incq %r12
addq $0xc, %rbx
cmpq $0xa, %r12
jne 0x3f23
movzbl %cl, %eax
xorl %ebx, %ebx
testb $0x1, %r13b
cmovel %eax, %ebx
movswl %bp, %esi
leaq 0x130(%rsp), %r14
movq 0x10(%rsp), %rdi
movq %r14, %rdx
callq 0x3924
cmpl $-0x1, (%r14)
je 0x3fbc
testb %bl, %bl
jne 0x4076
movups 0x61a1(%rip), %xmm0     # 0xa149
movups %xmm0, 0x13f(%rsp)
movups 0x6183(%rip), %xmm0     # 0xa13a
jmp 0x406e
testb $0x1, %r13b
jne 0x3fc6
testb %bl, %bl
je 0x4040
cmpl $0x0, 0xb4(%rsp)
je 0x3fd8
testb %bl, %bl
je 0x4058
testb $0x1, %r13b
je 0x4076
testb %bl, %bl
jne 0x4076
movl 0xc(%rsp), %eax
leaq (%rax,%rax,2), %rbx
movswl 0x42(%rsp,%rbx,4), %esi
movq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x38c4
movb $0x0, 0x38(%rsp,%rbx,4)
movw $0x0, 0x42(%rsp,%rbx,4)
leaq 0x30(%rsp), %rdx
decl 0x4(%rdx)
movswl 0x4(%r14), %esi
movq %r14, %rdi
callq 0x392a
movabsq $0xa0d0a0d30203a, %rax  # imm = 0xA0D0A0D30203A
movq %rax, 0x13f(%rsp)
movups 0x60e5(%rip), %xmm0     # 0xa123
jmp 0x406e
movups 0x611d(%rip), %xmm0     # 0xa164
movups %xmm0, 0x13b(%rsp)
movups 0x6103(%rip), %xmm0     # 0xa159
jmp 0x406e
movups 0x6123(%rip), %xmm0     # 0xa182
movups %xmm0, 0x13e(%rsp)
movups 0x6106(%rip), %xmm0     # 0xa174
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