!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
module cubetools_system_tool
  use gkernel_interfaces
  use cubetools_parameters
  !
  public :: cubetools_directory_create_for_file
  public :: cubetools_directory_create
  private
  !
contains
  !
  subroutine cubetools_directory_create_for_file(filename,error)
    !-------------------------------------------------------------------
    ! Create the directory for a file if it does not exist. The filename
    ! can be a full path or just a name. In the first case, the
    ! directory is created if it does not exist. In the second case,
    ! nothing is done.
    !-------------------------------------------------------------------
    character(len=*), intent(in)    :: filename
    logical,          intent(inout) :: error
    !
    character(len=base_l) :: basename
    character(len=file_l) :: dirname
    !
    call sic_parse_name(filename,basename,odir=dirname)
    if (dirname.ne.'') then
      call cubetools_directory_create(dirname,error)
      if (error) return
    endif
  end subroutine cubetools_directory_create_for_file
  !
  subroutine cubetools_directory_create(dirname,error)
    !-------------------------------------------------------------------
    ! Create a directory (mkdir -p) if it does not exist. If the
    ! directory already exists, nothing is done.
    !-------------------------------------------------------------------
    character(len=*), intent(inout) :: dirname
    logical,          intent(inout) :: error
    !
    integer(kind=4) :: nlength
    !
    nlength = len_trim(dirname)
    if (gag_inquire(dirname,nlength).ne.0) then
      call gag_mkdir(dirname,error)
      if (error) return
    endif
  end subroutine cubetools_directory_create
end module cubetools_system_tool
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
