     program demo
	   logical, external :: ac_del
	   logical :: test
	   test = ac_del('junk')
     stop
     end	 
 !
    !
    !
    !  JandC Marking:
    !                   EAR99
    !  JandClog:   SH Lamson  4/11/2016  no technical data;  ear99
    !*******************************************************************************
    !
    !*******************************************************************************
    recursive logical function ac_del(filename)
    use ifport
    use messages
    !
    IMPLICIT NONE
    character(len=*), intent(in) :: filename
    ! local variables
    integer, automatic                        :: ierr, result
    integer(kind=2), automatic                :: count
    character(len=SIZEOFFILENAME), automatic  :: filestr, subdir
    logical, automatic :: file_exists, delresult, wildcard
    
    TYPE (FILE$INFO), automatic               :: info
    INTEGER(KIND=INT_PTR_KIND( )), automatic  :: handle

    character(len=8)   :: rout = 'ac_del: '
    logical            :: local_debug = .true.
    !
    !  AUTHOR: SH Lamson
    !
    !  CREATION DATE: May 1, 2014
    !
    !  COMPONENT NAME: ac_del
    !
    !  COMPONENT'S FUNCTION:
    !  delete a file if it exists, supports recursive directory deletion and wildcards
    !
    if ( local_debug ) print *,rout,'Enter routine for filename=',trim(filename)
    ac_del = .true.
    wildcard = ( index(filename,'*') + index(filename,'?') ) > 0
    ! wildcard logic
    if ( wildcard ) then
        filestr = filename
        call slashify(filestr)
        count = DELFILESQQ(filestr)
        if ( local_debug ) print *,rout,'wildcard DELFILESQQ count=',count
        ! now recursively delete subdirectories
        handle = FILE$FIRST
        do while (.true.)
            if ( local_debug ) print *,rout,' wildcard loop on filestr=',trim(filestr)
            result = GETFILEINFOQQ (filestr,info,handle)
            if ( result > 0 ) then
                if ( info%name == '.' .or. info%name == '..' ) cycle
                subdir =  trim(info%name)
                if ( local_debug ) print *,rout,' subdirectory name=',trim(subdir)
                delresult = ac_del(subdir)
            else
                if (local_debug ) print *,rout,' wildcard no files from GETFILEINFOQQ ',result,' from ',trim(filestr)
                exit
            end if
            if ( handle == FILE$LAST ) exit
        end do
    else      ! non-wildcard logic
        inquire(file=filename,exist=file_exists)
        if ( file_exists ) then    ! simple file
            ierr = unlink(filename)
            if ( ierr > 0 ) then
                call addmessage('ac_del  ERROR: CANNOT delete file '//trim(filename))
                ac_del = .false.
            end if
        else                       ! directory
            inquire(DIRECTORY=filename,exist=file_exists)
            if ( file_exists ) then
                if ( local_debug ) print *,rout,' try to delete directory ',trim(filename)
                delresult = DELDIRQQ (filename)
                if ( local_debug ) print *,rout,'   DELDIRQQ status ',delresult
            else
                if ( wildcard ) then
                    delresult = .false.
                else
                    delresult = .true.
                end if
            end if

            if (  .not. delresult ) then ! failed to delete directory; must not be empty
                ! delete files in directory filename but will not delete subdirectories
                filestr = trim(filename) // SLASH // '*'
                if ( local_debug ) print *,rout,' filestr=',trim(filestr)
                count = DELFILESQQ(filestr)
                if ( local_debug ) print *,rout,'DELFILESQQ count=',count
                ! now recursively delete subdirectories
                handle = FILE$FIRST
                do while (.true.)
                    if ( local_debug ) print *,rout,' loop on filestr=',trim(filestr)
                    result = GETFILEINFOQQ (filestr,info,handle)
                    if ( result > 0 ) then
                        if ( local_debug ) print *,rout,' loop on filename=',trim(info%name)
                        if ( info%name == '.' .or. info%name == '..' ) cycle
                        subdir = trim(filename) // SLASH // trim(info%name)
                        if ( local_debug ) print *,rout,' subdirectory name=',trim(subdir)
                        delresult = ac_del(subdir)
                    else
                        if (local_debug ) print *,rout,' no files from GETFILEINFOQQ ',result,' from ',trim(filestr)
                        exit
                    end if
                    if ( handle == FILE$LAST ) exit
                end do
                ! should be able to delete directory now that it should be empty
                if ( local_debug ) print *,rout,' try to delete empty directory ',trim(filename)
                ac_del = DELDIRQQ (filename)
                if ( .not. ac_del ) then
                    if ( local_debug ) then
                        print *,rout,' failed to delete directory ',trim(filename)
                        call pwsystem('ls -l ' // trim(filename) )
                    end if
                end if
            end if
        end if
    end if

    if ( local_debug ) print *,rout,'Exit routine status=',ac_del
    RETURN
    END function ac_del

