#! /bin/sh

get_dcf_field () {
  ## Get one field including all continuation lines from a DCF file.
  ## Usage:
  ##   get_dcf_field FIELD FILE
  ws="[ 	]"		# space and tab
  ${SED-sed} -n "/^${1}:/,/^[^ ]/p" ${2} | \
    ${SED-sed} -n "1s/^${1}:${ws}*//p; /^${ws}/p" | \
    ${SED-sed} "/^${ws}/s/^${ws}*//; s/[ 	
]*$//"
    ## (Strip leading tag [first match], delete all additional tag
    ## lines, and remove leading whitespace from continuation lines.)
}

fmt () {
    echo "${1}" | sed 's/^/  /g' | sed ':a;N;$!ba;s/\n/\n  /g'
}    

dir=${1:-${HOME}/tmp/CRAN}

for f in ${dir}/*.Rcheck/*/DESCRIPTION; do
    package=`get_dcf_field Package ${f}`
    depends=`get_dcf_field Depends ${f}`
    imports=`get_dcf_field Imports ${f}`
    if test -n "${depends}${imports}"; then
	echo "Package: ${package}"
	test -n "${depends}" && fmt "Depends: ${depends}"
	test -n "${imports}" && fmt "Imports: ${imports}"
    fi
done    

### Local Variables: ***
### mode: sh ***
### sh-indentation: 2 ***
### End: ***