#!/bin/bash

tkis_handle_documentation(){
  local doc_type=$1
  local classification=$2
  local documentation=$(echo $* | sed -e "s|^$doc_type[[:space:]]*$classification[[:space:]]*||")
  local test_case=
  local doc_type_list="man, doc, info, html, kevux"
  local classification_list="core, standard, network, servers, multimedia, games, xorg, compilation, applications"
  local i=
  local what_to_do=

  if [[ $doc_type == "" ]] ; then
    error "tkis_handle_documentation(): $color_reset${color_notice}tkis_handle_documentation$color_error requires a doc_type: $doc_type_list"
  else
    test_case="fail"

    for i in $(echo $doc_type_list | sed -e 's|,||g') ; do
      if [[ $doc_type == $i ]] ; then
        test_case="pass"
        break
      fi      
    done

    if [[ $test_case == "fail" ]] ; then
      error "tkis_handle_documentation(): You specified $color_reset${color_notice}$doc_type$color_error for doc_type, but doc_type can only be one of the following: $doc_type_list"
    fi
  fi

  if [[ $classification == "" ]] ; then
    error "tkis_handle_documentation(): $color_reset${color_notice}tkis_handle_documentation$color_error requires a classification: $classification_list"
  else
    test_case="fail"

    for i in $(echo $classification_list | sed -e 's|,||g'); do
      if [[ $classification == $i ]] ; then
        test_case="pass"
        break
      fi      
    done

    if [[ $test_case == "fail" ]] ; then
      error "tkis_handle_documentation(): You specified $color_reset${color_notice}$classification$color_error for classification, but classification can only be one of the following: $classification_list"
    fi
  fi

  unset test_case

  # Make sure that nothing dangerous is present in the $documentation variable
  for i in $documentation ; do
    tkis_security_check_remove $i
  done

  if [[ $(echo $desired_documentation_doc_types | grep -s "[[:space:]]*$doc_type[[:space:]]*") == "" ]] ; then
    debug "The doc_type '$doc_type' was not found in the desired_documentation_doc_types '$desired_documentation_doc_types'"
    what_to_do="delete"
  else
    debug "The doc_type '$doc_type' is in '$desired_documentation_doc_types'"
    what_to_do="keep"
  fi

  if [[ $what_to_do != "delete" ]] ; then
    if [[ $(echo $desired_documentation_classifications | grep -s "[[:space:]]*$classification[[:space:]]*") == "" ]] ; then
      debug "The classification '$classification' was not found in the desired_documentation_classifications '$desired_documentation_classifications'"
      what_to_do="delete"
    else
      debug "The classification '$classification' is in '$desired_documentation_classifications'"
      what_to_do="keep"
    fi
  fi

  # To delete or not to delete
  if [[ $what_to_do == "delete" ]] ; then
    rm -Rf $documentation
  else
    mkdir -vp ${documentation_directory}${doc_type}

    # If the documentation_directory cannot be written to, just quit and consider this a non-error
    # Also remove the documentation that was to be moved
    if [ $? -ne 0 ] ; then
      warning "The directory '$color_reset$color_notice$documentation_directory$doc_type$color_reset$color_warning' could not be created."
      rm -Rf $documentation
      return 0;
    fi

    for i in $documentation ; do
      # Do not attempt to copy from the same directory to the same directory
      # Do no attempt to do anything if the documentation_directory is a subdirectory of what we are told to handle
      if [[ $i != $documentation_directory && $(echo $documentation_directory | grep -s "^$i/") == "" ]] ; then
        if [[ $doc_type == "man" && $(echo $i | grep -s "/man[/]*$") != "" ]] ; then
          cp -vR $i/* ${documentation_directory}${doc_type}
          rm -Rf $i
        elif [[ $doc_type == "doc" ]] ; then
          if [[ $(echo $i | grep -s "/doc[/]*$") != "" || $(echo $i | grep -s "/docs[/]*$") != "" || $(echo $i | grep -s "/gtk-doc[/]*$") != "" ]] ; then
            cp -vR $i/* ${documentation_directory}${doc_type}
          else
            cp -vR $i ${documentation_directory}${doc_type}
          fi
          rm -Rf $i
        elif [[ $doc_type == "info" && $(echo $i | grep -s "/info[/]*$") != "" ]] ; then
          cp -vR $i/* ${documentation_directory}${doc_type}
          rm -Rf $i
        elif [[ $doc_type == "html" ]] ; then
          if [[ $(echo $i | grep -s "/html[/]*$") != "" || $(echo $i | grep -s "/htm[/]*$") != "" ]] ; then
            cp -vR $i/* ${documentation_directory}${doc_type}
          else
            cp -vR $i ${documentation_directory}${doc_type}
          fi
          rm -Rf $i
        elif [[ $doc_type == "kevux" && $(echo $i | grep -s "/kevux[/]*$") != "" ]] ; then
          cp -vR $i/* ${documentation_directory}${doc_type}
          rm -Rf $i
        else
          cp -vR $i ${documentation_directory}${doc_type}
          rm -Rf $i
        fi
      fi
    done

    chown 0:d_documentation -R $documentation_directory
    chmod o-rx -R $documentation_directory
  fi
}
