#!/home/cyd7997/bin/ksh93.att
################################################################
#### Program: find_k93
#### 
#### Description: Emulation of the Unix "find" command.
#### 
#### Currently only supports the -name, -type, and -print options.
#### The valid types supported with the -type option are d, f, and l.
#### 
#### Author: Dana French (dfrench@mtxia.com)
#### 
#### Date: 07/27/2004
#### 
################################################################
function find_k93 {
  typeset TRUE="0"
  typeset FALSE="1"
  typeset BASEDIR="${PWD}"
  typeset   NAME="${FALSE}"
  typeset   TYPE="${FALSE}"
  typeset  PRINT="${TRUE}"
  typeset   EXEC="${EXEC}"

  ARGS=( "${@}" )
  OPTIND="0"
  DIRS=( "${@:%%[[:blank:]]-*}" )
  [[ "_${DIRS[0]}" = "_" ]] && DIRS[0]="."

  while getwordopts_k93 "name:,type:,print,?" OPTION ARGS
  do
    case "${OPTION}" in
       'name' ) (( NAME = TRUE ))
                NAMEPAT="${OPTARG}";;
       'type' ) (( TYPE = TRUE ))
                TYPEPAT="${OPTARG}";;
      'print' ) (( PRINT = TRUE ));;
      'exec' ) (( EXEC = TRUE ));;
      '?' ) print -u 2 "Syntax: find_k93 [DIRECTORY]... [-name PATTERN] [-type d|f|l] [-print]";;
    esac
  done

  for DIR in "${DIRS[@]}"
  do
      findfiles_k93 "${BASEDIR}" "${DIR}" | while read -r -- LINE
      do
          SHOWLINE="${TRUE}"

          ((  NAME == TRUE )) &&
              [[ "_${LINE##*/}" != _${NAMEPAT} ]] &&
              SHOWLINE="${FALSE}"

          ((  TYPE == TRUE )) &&
              [[ "_${TYPEPAT}" = "_f" ]] &&
              [[ ! -f "${LINE}" ]] &&
              SHOWLINE="${FALSE}"

          ((  TYPE == TRUE )) &&
              [[ "_${TYPEPAT}" = "_d" ]] &&
              [[ ! -d "${LINE}" ]] &&
              SHOWLINE="${FALSE}"

          ((  TYPE == TRUE )) &&
              [[ "_${TYPEPAT}" = "_l" ]] &&
              [[ ! -h "${LINE}" ]] &&
              SHOWLINE="${FALSE}"

          (( SHOWLINE == TRUE )) && print -r -- "${LINE}"

      done
  done
}

################################################################
function findfiles_k93 {
    typeset BASEDIR="${1}"
    typeset DIR="${2}"
    typeset FNAME

    if [[ -d "${BASEDIR}" ]]
    then
        cd "${BASEDIR}"
        if [[ -d "${DIR}" ]]
        then
            print -r -- "${DIR}"
            if cd "${DIR}" > /dev/null 2>&1
            then
                for FNAME in .+([!.]) *
                do
                  [[ "_${FNAME}" = '_.+([!.])' ]] && continue
                  [[ "_${FNAME}" = '_*' ]] && continue
                  if [[ -d "${FNAME}" ]]
                  then
                      findfiles_k93 "${BASEDIR}" "${DIR}/${FNAME}"
                      cd "${BASEDIR}" > /dev/null 2>&1
                      cd "${DIR}" > /dev/null 2>&1
                  else
                      print -r -- "${DIR}/${FNAME}"
                  fi
                done
            else
                print -u 2 -r -- "find_k93: cannot change directory to <${DIR}>:"
                print -u 2 -r -- "  : The file access permissions do not allow the specified action."
            fi
        fi
    fi
    return 0
}
################################################################
getwordopts_k93 ()
{
    typeset TRUE=0
    typeset FALSE=1
    IFS=",${IFS}" OPTIONLIST=( ${1} )
    OPTION=""
    OPTARG=""

    while (( OPTIND < ${#ARGS[*]} ))
    do
      CLN="${ARGS[OPTIND]}"
      if [[ "_${CLN}" == _-* ]]
      then
        for OPT in "${OPTIONLIST[@]}"    
        do
          OPTION="${OPT%+(:)}"
          if [[ "_${CLN#-}" == "_${OPTION}" ]]
          then
              LAST="${#OPTION}"
              if [[ "_${OPT:LAST:1}" == "_:" ]]
              then
                  if [[ "_${ARGS[OPTIND+1]}" == _-* ]]
                  then
                      print -u 2 -- "${CLN}: invalid argument"
                      (( OPTIND = OPTIND + 1 ))
                      return ${TRUE}
                  else
                      OPTARG="${ARGS[OPTIND+1]}"
                      (( OPTIND = OPTIND + 2 ))
                      return ${TRUE}
                  fi
              fi
             (( OPTIND = OPTIND + 1 ))
              return ${TRUE}
          fi
        done
        print -u 2 -- "${CLN}: invalid option"
        (( OPTIND = OPTIND + 2 ))
        return ${TRUE}
      fi
    (( OPTIND = OPTIND + 1 ))
    done
    return ${FALSE}
}
################################################################

find_k93 "${@}"