################################################################################
#
# pkg_info.cygpart - cygport package information functions
#
# Copyright (C) 2006-2020 Cygport authors
# Provided by the Cygwin project <https://cygwin.com/>
#
# cygport is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cygport is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cygport.  If not, see <https://www.gnu.org/licenses/>.
#
################################################################################

#****** Chapter 4/Information
#****

__pager() {
	local pager
	if defined PAGER
	then
		pager=${PAGER}
	elif check_prog less
	then
		pager="less"
	else
		pager="cat"
	fi
	[ -t 1 ] && ${pager} || cat
}

#****v* Information/HOMEPAGE
#  DESCRIPTION
#  The website URI of the package.  Besides being informational, this defines
#  what address will be displayed in BROWSER by the homepage command.
#****

#****v* Information/LICENSE
#  DESCRIPTION
#  A SPDX license expression (See https://spdx.dev/ids/) for the license(s) of
#  the package.
#****

__show_info() {
	__pager <<-_EOF
	Cygport package information
	    Package name:     ${PN}
	    Package version:  ${PV}
	    Cygwin release:   ${PR}
	    Homepage:         ${HOMEPAGE}
	    License:          ${LICENSE}
	    Summary:          ${SUMMARY:-${DESCRIPTION}}
	    Description:
${DESCRIPTION:-${SUMMARY}}
	_EOF
}

__list_files() {
	pushd ${D}
	find * -path 'usr/*/debug' -prune -o ! -type d -print | sed 's!^!/!' | sort | __pager
	popd
}

__list_debug_files() {
	pushd ${D}
	find usr/*/debug ! -type d 2>/dev/null | sed 's!^!/!' | sort | __pager
	popd
}

__list_deps() {
	local d f ext
	local alldeps expldeps shdep girdep jar jpkg ladep luadep luapaths pcdep pctgt pidep
	local mldep mldir mllib
	local phpdep phpmoddir pldep pldirs plver
	local pydep pydirs pyexts pyscr pysite pyver
	local rbdep rbdirs rbver gemdir gemextdir gemspecdir
	local tcldep tclpkgs vapidep vapidir
	local tcgcclibdir tcgccsysroot
	local dlltool="dlltool"
	local os_path="${PATH}"
	local deps_prune
	local objdump=${OBJDUMP}

	# if file list is empty, there are obviously no dependencies
	(( $# > 0 )) || return 0

	if __host_is_embedded
	then
		return 0
	fi

	if cross_compiling && check_prog ${CTARGET}-dlltool
	then
		dlltool="${CTARGET}-dlltool"
	fi

	pushd ${D}

#****v* Information/DEPS_PATH
#  DESCRIPTION
#  A colon-separated list of directories not ordinarily in PATH in which to
#  search for linked dependencies.  Given directories are searched both
#  on the system and under $D.
#****

	# This prevents a package from listing itself as a dependency if
	# already installed
	unset PATH

	for d in ${DEPS_PATH//:/ }
	do
		PATH+="${D}${d}:${d}:"
		if inherited cross
		then
			PATH+="${D}${CROSS_SYSROOT}${d}:${CROSS_SYSROOT}${d}:"
		fi
	done

#****v* Information/REQUIRES_EXCLUDE_FROM
#  DESCRIPTION
#  A colon-separated list of pathname globs, which are excluded from automatic
#  dependency detection.
#****

	for d in ${REQUIRES_EXCLUDE_FROM//:/ }
	do
		deps_prune+=" -o -path ${d#/} -prune"
	done

	if inherited toolchain
	then
		PATH+="${D}${TOOLCHAIN_BINDIR}:${D}${TOOLCHAIN_LIBDIR}:"
	fi

	export PATH+="${D}$(__host_prefix)/bin:${os_path}";

	# only on Cygwin: assure Windows\System32 is present for w32api DLLs
	if check_prog /bin/cygpath
	then
		PATH+=":$(/bin/cygpath -S)"
	fi

	if (cross_compiling || [ "${CHOST}" != "${CTARGET}" ]) && ! __target_is_embedded && check_target_tool gcc
	then
		tcgcclibdir=$(dirname $(${CTARGET}-gcc --print-file-name=libgcc.a))
		tcgccsysroot=$(${CTARGET}-gcc -print-sysroot)
		PATH+=":${D}${tcgcclibdir#${D}}:${tcgcclibdir#${D}}"

		# FIXME: thoroughout this section, CTARGET to get libgcc et. al. deps,
		#        or CHOST to get canadian-cross deps
		case ${CTARGET} in
		${CBUILD%%-*}-*-mingw*|${CBUILD%%-*}-*-msys*)
			PATH+=":${D}/usr/${CTARGET}/sys-root/$(__target_prefix)/bin:${tcgccsysroot}$(__target_prefix)/bin"
			# native binutils handles both cygwin and mingw binaries
			;;
		*-*-cygwin*|*-*-mingw*|*-*-msys*)
			PATH+=":${D}/usr/${CTARGET}/sys-root/$(__target_prefix)/bin:${tcgccsysroot}$(__target_prefix)/bin"
			;;
		*-*-darwin*)
			PATH+=":${D}/usr/${CTARGET}/sys-root/$(__target_prefix)/lib:${tcgccsysroot}$(__target_prefix)/lib"
			alldeps+=" "$(find "${@//^_^/ }" -type f -executable -exec ${CTARGET}-otool -L '{}' + 2>/dev/null | \
				sed -e '1d;s|^\t\([^ ]*\).*|\1|g' | sort -u | xargs -r which || true )
			;;
		# FIXME: any other non-ELF systems?
		*)	# ELF system; libc in /lib, other libs in /usr/lib
			PATH+=":${D}/usr/${CTARGET}/sys-root/lib:${tcgccsysroot}/lib"
			PATH+=":${D}/usr/${CTARGET}/sys-root/$(__target_prefix)/lib:${tcgccsysroot}$(__target_prefix)/lib"
			alldeps+=" "$(find "${@//^_^/ }" -type f -executable -exec ${CTARGET}-objdump -p '{}' + 2>/dev/null | \
				sed -n -e 's| *NEEDED *\([^ ]*\).*|\1|gp' | \
				sort -u | xargs -r which || true )
			;;
		esac
	fi

	# *.so: Apache2 modules, OCaml stublibs, Ruby modules
	# *.oct: Octave modules
	# *.mex: Octave MATLAB-compatible MEX modules
	# *.cmxs: OCaml natdynlink modules: http://gallium.inria.fr/~frisch/ndl.txt
	alldeps+=" "$(LC_ALL=C find "${@//^_^/ }" -path 'usr/share/doc/*' -prune \
			${deps_prune} \
			-o -type f -executable \( -name '*.exe' -o -name '*.dll' -o -name '*.so' -o -name '*.oct' -o -name '*.mex' -o -name '*.cmxs' \) \
			-exec ${objdump} -p '{}' + 2>/dev/null \
			| grep "DLL Name:" | sort -u | awk '{print $3}' \
			| xargs -r which || true)

	# shebang script interpreters
	for shdep in $(find "${@//^_^/ }" -path 'usr/share/doc/*' -prune \
			${deps_prune} \
			-o -path 'usr/lib/perl/*' -prune \
			-o -path 'usr/lib/python[23].*' -prune \
			-o -path 'usr/share/gems/*' -prune \
			-o -path 'usr/share/help/*' -prune \
			-o -type f -executable ! -name '*.exe' ! -name '*.dll' ! -name '*.so' ! -name '*.cmxs' ! -name '*.oct' ! -name '*.mex' -print \
			| xargs -r sed -sne '1{/^#!/s|/env *|/|g;s|^#! *\([^ ]*\).*|\1|gp}' \
			| sort -u)
	do
		if [ -f ${D}${shdep} ]
		then
			alldeps+=" "${D}${shdep};
		elif [ -f ${shdep} ]
		then
			# python{,3} are alternatives symlinks, which 'cygcheck
			# -f' can't translate to a package name, so we explicitly
			# translate to a package name ourself.
			case ${shdep} in
				/usr/bin/python|/usr/bin/python3)
					expldeps+=" python3";
				;;
			esac
			alldeps+=" "${shdep};
		fi
	done

	# implib devel->dll subpackage dependencies
	alldeps+=" "$(find "${@//^_^/ }" -type f -name '*.dll.a' -exec ${dlltool} -I '{}' \; 2>/dev/null \
			| sort -u | xargs -r which)

	for ladep in $(find "${@//^_^/ }" -type f -name '*.la' \
			| xargs -r sed -ne "s|^dependency_libs='\(.*\)'|\1|gp" \
			| sed -e 's| -[LR][^ ]*||g;s| -l\([^ ]*\)| lib\1|g')
	do
		for f in ${ladep} $(__host_prefix)/lib/${ladep}.{dll.a,so,a}
		do
			if [ -f ${D}${f} ]
			then
				alldeps+=" "${D}${f};
				break
			elif [ -f ${f} ]
			then
				alldeps+=" "${f};
				break
			fi
		done
	done

	for pcdep in $(find "${@//^_^/ }" -type f -name '*.pc')
	do
		pctgt=
		case $pcdep in
		usr/lib/pkgconfig/*|usr/share/pkgconfig/*)
			alldeps+=" /usr/bin/pkg-config"
			;;
		usr/*/sys-root/*/*/pkgconfig/*)
			pctgt=${pcdep#usr/}
			pctgt=${pctgt%%/*}
			alldeps+=" /usr/bin/${pctgt}-pkg-config"
			;;
		esac
	done

	# pkg-config dependencies on other devel packages
	for pcdep in $(find "${@//^_^/ }" -type f -name '*.pc' | xargs -r grep -h '^Requires' | cut -d' ' -f2- | sed -e 's/,/ /g')
	do
		# avoid version comparison tokens
		case ${pcdep} in [A-Za-z]*) ;; *) continue ;; esac
		for d in ${D}$(__host_prefix)/lib/pkgconfig ${D}$(__host_prefix)/share/pkgconfig ${D}/usr/share/pkgconfig \
			 $(__host_prefix)/lib/pkgconfig $(__host_prefix)/share/pkgconfig /usr/share/pkgconfig
		do
			if [ -f ${d}/${pcdep}.pc ]
			then
				alldeps+=" "${d}/${pcdep}.pc;
				break;
			fi
		done
	done

	# pkg-config dependencies on other libraries
	for pcdep in $(find "${@//^_^/ }" -type f -name '*.pc' \
			| xargs -r grep -h '^Libs:' \
			| sed -e "s|^[^ ]*||g;s| \+-[^l][^ ]*||g;s| *-l\([^ ]*\)| lib\1|g")
	do
		for f in $(__host_prefix)/lib/${pcdep}.{dll.a,so,a}
		do
			if [ -f ${D}${f} ]
			then
				alldeps+=" "${D}${f};
				break
			elif [ -f ${f} ]
			then
				alldeps+=" "${f};
				break
			fi
		done
	done

	if check_prog monodis
	then
		alldeps+=" "$(for x in $(monodis --assemblyref $(find "${@//^_^/ }" -type f -perm 644 \( -name '*.exe' -o -name '*.dll' \) ) 2>/dev/null \
			| sed -e ':a;N;$!ba;s|\n\t| |g;s|\n0x00000000: ||g' | grep 'Public Key:' \
			| sed -e 's|.* Version=\([1-9]\).* Name=\(mscorlib\) .*|usr/lib/mono/\1.0/\2.dll|g' \
			-e 's|.* Version=\([0-9\.]*\) Name=\([^ ]*\).* Public Key:\([0-F ]*\)|usr/lib/mono/gac/\2/\1__\L\3\E/\2.dll|g' -e 's| ||g' \
			| sort -u); do \
			if ! test -f ./$x ; then echo /$x ; fi ; done)
		alldeps+=" "$(find "${@//^_^/ }" \( -name '*.dll.config' -o -name '*.exe.config' \) \
			| xargs -r sed -n -e 's|.*target="*\([^ "]*\.dll\).*|\1|gp' \
			| sort -u | xargs -r which || true)
	fi

	alldeps+=" "$(find "${@//^_^/ }" -path 'usr/share*/gir-1.0/*.gir' | xargs -r sed -n -e '/shared-library/s/,/ /g;s/.*shared-library="\([^"]*\).*/\1/gp' | xargs -r which | sort -u)
	for girdep in $(find "${@//^_^/ }" -path 'usr/share*/gir-1.0/*.gir' \
			| xargs -r sed -n -e 's/.*<include *name="\([^"]*\)" *version="\([^"]*\)".*/\1-\2.gir/gp' \
			| sort -u)
	do
		if [ -f ${D}/usr/share/gir-1.0/${girdep} ]
		then
			alldeps+=" "${D}/usr/share/gir-1.0/${girdep}
		else
			alldeps+=" "/usr/share/gir-1.0/${girdep}
		fi
	done

	vapidir=/usr/share/vala/vapi
	if [ -d ${D}${vapidir} ]
	then
		for vapidep in $(find "${@//^_^/ }" -path "${vapidir#/}/*.deps" | xargs -r cat | sort -u)
		do
			if [ -f ${D}${vapidir}/${vapidep}.vapi ]
			then
				alldeps+=" "${D}${vapidir}/${vapidep}.vapi
			else
				alldeps+=" "${vapidir}/${vapidep}.vapi
			fi
		done
	fi

	if [ $(find "${@//^_^/ }" -type f -name '*.jar' | wc -l) -gt 0 ] && check_prog jdepend jar
	then
		> ${T}/.jardb
		for jar in $(find ${D} /usr/share/java -type f -name '*.jar' ! -name 'libgcj-*.jar')
		do
			echo $jar: $(jar tf $jar | grep '\.class$' | sed -e 's|/[^/]*.class$||g;s|/|.|g' | sort -u)" " >> ${T}/.jardb
		done
		for jpkg in $(find "${@//^_^/ }" -type f -name '*.jar' | xargs -r jdepend | sed -ne 's/^\([^,]*\),[0-9].*/\1/gp')
		do
			alldeps+=" "$(sed -ne "/ $jpkg /s|^\(.*\):.*|\1|1p" ${T}/.jardb)
		done
	fi

	if check_prog lua
	then
		luapaths=($(lua -e 'print(package.path..";"..package.cpath)' | tr '\;' ' '))
		luapaths+=" ${DEPS_PATH//:/ }"
		for luadep in $(find "${@//^_^/ }" -path 'usr/share/doc/*' -prune \
				${deps_prune} \
				-o -path 'usr/share/help/*' -prune \
				-o -name '*.lua' -print \
				-o -type f ! -name '*.*' -executable -exec sed -sne '1{/^#!.*lua/F}' '{}' + \
				| xargs -r sed -ne "s|^[^#]*require[( ]*['\"]\([^'\"]*\)['\"].*|\1|gp" \
				| sort -u)
		do
			for f in ${luapaths[*]/\//${D}/} ${luapaths[*]}
			do
				if [ -f ${f/\?/${luadep//\.//}} ]
				then
					alldeps+=" "${f/\?/${luadep//\.//}};
					break;
				fi
			done
		done
	fi

	if check_prog ocamlc.opt
	then
		mldir=$(ocamlc.opt -where)
		sys_mldir=$mldir
		if [ ! -d ${sys_mldir} ]
		then
			sys_mldir=""
		fi
		if [ -d ${D}${mldir} ]
		then
			for cma in $(find ${D}${mldir} ${sys_mldir} -name '*.cma')
			do
				ocamlobjinfo $cma | sed -nr "s#(Unit|Module) name: #$cma:#p"
			done > ${T}/.cmaobjinfo.out
			for cmxa in $(find ${D}${mldir} ${sys_mldir} -name '*.cmxa')
			do
				ocamlobjinfo $cmxa | sed -nr "s#(Unit|Module) name: #$cmxa:#p"
			done > ${T}/.cmxaobjinfo.out
			alldeps+=" "$(find "${@//^_^/ }" -type f -name '*.cma' | xargs -r ocamlobjinfo \
					| grep -Eo '[0-9a-f]{32}[[:space:]]+[A-Za-z0-9_]+' \
					| sort -u \
					| while read md5sum module
					  do grep -m 1 ":$module$" ${T}/.cmaobjinfo.out | sed -e 's/:.*//'
					  done | sort -u)
			alldeps+=" "$(find "${@//^_^/ }" -type f -name '*.cmxa' | xargs -r ocamlobjinfo \
					| grep -Eo '[0-9a-f]{32}[[:space:]]+[A-Za-z0-9_]+' \
					| sort -u \
					| while read md5sum module
					  do grep -m 1 ":$module$" ${T}/.cmxaobjinfo.out | sed -e 's/:.*//'
					  done | sort -u)
		fi
		for mllib in $(find "${@//^_^/ }" -type f -name '*.cmxa')
		do
			for mldep in $(ocamlobjinfo ${mllib} | sed -nr '/^Extra C object files:/s/(.*: )?-l([^ ]*)/lib\2/gp')
			do
				if [ -f ${mllib%/*}/${mldep}.a ]
				then
					alldeps+=" "${mllib%/*}/${mldep}.a
				elif [ -f ${D}/usr/lib/${mldep}.dll.a ]
				then
					alldeps+=" "${D}/usr/lib/${mldep}.dll.a
				else
					alldeps+=" "/usr/lib/${mldep}.dll.a
				fi
			done
		done
		if check_prog ocamlfind && [ ${PN} != "ocaml-findlib" ]
		then
			alldeps+=" "$(find "${@//^_^/ }" -path "${mldir#/}/*/META" \
					| xargs -r grep -h 'requires *=' \
					| sed -e 's|.*requires *= *"\([^"]*\)".*|\1|g;s/,/ /g;' \
					| OCAMLPATH=${D}${mldir} xargs -n 1 -r ocamlfind query -predicates byte -format '%d/%a')
		fi
	fi

	if check_prog perl
	then
		pldirs=($(perl -e 'my $inc=(exists $ENV{PERL_NO_VENDOR_DEPS}?"(vendor|site)":"(site)")."_perl";print join(" ",reverse grep !/$inc/,@INC)'))
		pldirs+=" ${DEPS_PATH//:/ }"
		for pldep in $(find "${@//^_^/ }" -path 'usr/share/doc/*' -prune \
				${deps_prune} \
				-o -path 'usr/share/help/*' -prune \
				-o \( -name '*.pl' -o -name '*.pm' \) -print \
				-o -type f ! -name '*.*' -executable -exec sed -sne '1{/^#!.*perl.*/F}' '{}' + \
				| xargs -r sed -ne "s/^[^#]*use \(base\|parent\) ['\"]*\(qw( *\)*\([A-Z][^-)'\";]*\).*/\3/gp;s/^[^#]*\(use\|require\) ['\"]*\([A-Z][^ '\";]*\).*/\2/gp" \
				| sort -u)
		do
			for d in ${pldirs[*]/\//${D}/} ${pldirs[*]}
			do
				if [ -f ${d}/${pldep//:://}.pm ]
				then
					alldeps+=" "${d}/${pldep//:://}.pm;
					break;
				fi
			done
		done
	fi

	plver=( $(find "${@//^_^/ }" -path "usr/*/perl5/vendor_perl/5.[0-9][0-9]/*" \
	        | sed -E 's!usr/.*/perl5/vendor_perl/5.([0-9][0-9])/.*!perl5_0\1!' \
	        | sort -ru) )
	if [ "${#plver[@]}" -gt 1 ]
	then
		warning "More than one targeted Perl version: ${plver[*]},"
		warning "using only the latest as dependency: ${plver[0]}."
	fi
	if [ "${#plver[@]}" -gt 0 ] && [ "${PN}" != "perl_base" ]
	then
		echo "${plver[0]}"
	fi

	if check_prog php-config
	then
		phpmoddir=$(php-config  --extension-dir)
		for phpdep in $(find "${@//^_^/ }" -path 'usr/share/doc/*' -prune \
				${deps_prune} \
				-o -path 'usr/share/help/*' -prune \
				-o -name '*.php' -print \
				-o -type f ! -name '*.*' -executable -exec sed -sne '1{/^#!.*php*/F}' '{}' + \
				| xargs -r sed -ne "s/^ *\(include\|require\)\(_once\)* '\([^'\"]*\).*\;/\3/gp;s/.*loadExtension *( *[^$]\([^)'\"]*\).*/\1.dll/gp" \
				| sort -u)
		do
			case ${phpdep} in
			*.php)	if [ -f ${D}$(__host_prefix)/share/pear/${phpdep} ]
				then
					alldeps+=" "${D}$(__host_prefix)/share/pear/${phpdep}
				else
					alldeps+=" "$(__host_prefix)/share/pear/${phpdep}
				fi ;;
			*.dll)	if php -mn | grep -q ^${phpdep%.dll}$
				then
					alldeps+=" "$(which php)
				elif [ -f ${D}${phpmoddir}/${phpdep} ]
				then
					alldeps+=" "${D}${phpmoddir}/${phpdep}
				else
					alldeps+=" "${phpmoddir}/${phpdep}
				fi ;;
			esac
		done
	fi

	for pysite in ${D}/usr/lib/python[23].*/site-packages
	do
		[ -d "$pysite" ] || continue
		pyver=$(echo $pysite | sed -e 's|.*lib/python\([23]\.[0-9]*\)/site-packages|\1|')
		pydirs=($(python${pyver} -s -c 'import sys; print("\n".join(sys.path))' | grep 'site-packages'))
		case ${pyver} in
		3.*)	pyexts=$(python${pyver} -s -c 'import importlib.machinery; print(*importlib.machinery.all_suffixes())')
			tkimp=
			;;
		2.*)	pyexts=$(python${pyver} -s -c 'import imp
for suffix, mode, type in imp.get_suffixes(): print(suffix)')
			tkimp=$(python${pyver} -s -c "import imp; print(imp.find_module('Tkinter')[1])" 2>/dev/null)
			;;
		esac
		for pydep in $(find "${@//^_^/ }" -path 'usr/share/doc/*' -prune \
				${deps_prune} \
				-o -path "${pysite#${D}/}/*" -name '*.py' -print \
				| xargs -r grep -h '^[^#]*import ' \
				| sed -e 's/^from \([^ ]*\) import .*/\1/g;s/^import //g;s/ as [^ ]*//g;s/, */\n/g;s/ *#.*//g;/ /d' \
				| sort -u)
		do
			alldeps+=" "$(which python${pyver})
			for d in ${pydirs[*]/\//${D}/} ${pydirs[*]} ${tkimp%/*}
			do
				for ext in ${pyexts} /__init__.py /__pycache__/__init__.pyc /__init__.pyc
				do
					if [ -f ${d}/${pydep//\.//}${ext} ]
					then
						alldeps+=" "${d}/${pydep//\.//}${ext};
						break 2;
					fi
				done
			done
		done
	done

	for pyscr in $(find "${@//^_^/ }" -path 'usr/share/doc/*' -prune \
			${deps_prune} \
			-o -path 'usr/lib/pypy/*' -prune -o -path 'usr/lib/python3.*' -prune \
			-o -path 'usr/lib/python[23].*' -prune \
			-o -path 'usr/share/gdb/*' -prune -o -name '*-gdb.py' -prune \
			-o -path 'usr/share/help/*' -prune \
			-o -type f -executable -exec sed -sne '1{/^#!.*python/F}' '{}' +)
	do
		pyver=$($(sed -sne '1{/^#!/s|/env *|/|g;s|^#! *\([^ ]*\).*|\1|gp}' $pyscr) -c 'from distutils.sysconfig import get_python_version; print(get_python_version())')
		case ${pyver} in
		3.*)	pyexts=$(python${pyver} -s -c 'import importlib.machinery; print(*importlib.machinery.all_suffixes())')
			tkimp=
			;;
		2.*)	pyexts=$(python${pyver} -s -c 'import imp
for suffix, mode, type in imp.get_suffixes(): print(suffix)')
			tkimp=$(python${pyver} -s -c "import imp; print(imp.find_module('Tkinter')[1])" 2>/dev/null)
			;;
		esac
		pydirs=($(python${pyver} -s -c 'import sys; print("\n".join(sys.path))' | grep 'site-packages'))
		pydirs+=" ${DEPS_PATH//:/ }"
		for pydep in $(grep -h '^[^#]*import ' $pyscr \
				| sed -e 's/^from \([^ ]*\) import .*/\1/g;s/^import //g;s/ as [^ ]*//g;s/, */\n/g;s/ *#.*//g;/ /d' \
				| sort -u)
		do
			for d in ${pydirs[*]/\//${D}/} ${pydirs[*]} ${tkimp%/*}
			do
				for ext in ${pyexts} /__init__.py /__init__.pyc
				do
					if [ -f ${d}/${pydep//\.//}${ext} ]
					then
						alldeps+=" "${d}/${pydep//\.//}${ext};
						break 2;
					fi
				done
			done
		done
	done

	if [ "${PN}" != "ruby" ] && check_prog ruby && check_prog gem
	then
		rbdirs=($(ruby -e 'print($:.join(" "))'))
		rbdirs+=" ${DEPS_PATH//:/ }"
		gemdir=$(ruby -e 'print Gem.default_dirs[:system][:gem_dir]')
		gemextdir=$(ruby -e 'print Gem.default_dirs[:system][:ext_dir]')
		gemspecdir=${gemdir}/specifications
		# gems depend only on other gems and stdlib
		if [ ! -d ${D}${gemspecdir} ]
		then
			for d in ${gemdir}/gems/*/lib ${gemextdir}/ruby/*/*
			do
				rbdirs[${#rbdirs[*]}]=${d}
			done
		fi

		alldeps+=" "$(find "${@//^_^/ }" -path "${gemspecdir#/}/*.gemspec" -print \
			      | xargs -r sed -ne "s|.*add_runtime_dependency(%q<\([^>]*\)>.*|${gemspecdir}/\1-[0-9]*.gemspec|gp")

		for rbdep in $(find "${@//^_^/ }" -path 'usr/share/doc/*' -prune \
				${deps_prune} \
				-o -path 'usr/share/help/*' -prune \
				-o -path "${gemdir#/}/*" -prune \
				-o -name '*.rb' -print \
				-o -type f ! -name '*.*' -executable -exec sed -sne '1{/^#!.*ruby/F}' '{}' + \
				| xargs -r sed -ne "s|^[^#]*require ['\"]\([^'\"]*\)['\"].*|\1|gp" \
				| sort -u)
		do
			for d in ${rbdirs[*]/\//${D}/} ${rbdirs[*]}
			do
				for f in ${rbdep}.rb ${rbdep}.so ${rbdep}
				do
					if [ -f ${d}/${f} ]
					then
						alldeps+=" "${d}/${f};
						break 2;
					fi
				done
			done
		done

		rbver=( $(find "${@//^_^/ }" -path "usr/*/ruby/vendor_ruby/[0-9].[0-9]/*" \
				| sed -E 's!usr/.*/ruby/vendor_ruby/([0-9]).([0-9])/.*!ruby_\1\2!' \
				| sort -ru) )
		if [ "${#rbver[@]}" -gt 1 ]
		then
			warning "More than one targeted Ruby version: ${rbver[*]},"
			warning "using only the latest as dependency: ${rbver[0]}."
		fi
		if [ "${#rbver[@]}" -gt 0 ]
		then
			echo "${rbver[0]}"
		fi
	fi

	if [ $(find "${@//^_^/ }" -path 'usr/share/doc/*' -prune \
		${deps_prune} \
		-o -path 'usr/share/help/*' -prune \
		-o -type f -name '*.tcl' ! -name 'pkgIndex.tcl' -print \
		-o -type f ! -name '*.*' -executable -exec sed -sne '1{/^#!.*\(tcl\|wi\)sh*/F};1,5{/^[[:space:]]*exec[[:space:]]\+\(tcl\|wi\)sh*/F}' '{}' + \
		| wc -l) -gt 0 ]
	then
		tclpkgs=$(find ${D} /usr/lib/*/ -type f -name 'pkgIndex.tcl')
		for tcldep in $(find "${@//^_^/ }" -path 'usr/share/doc/*' -prune \
				${deps_prune} \
				-o -path 'usr/share/help/*' -prune \
				-o -name '*.tcl' ! -name 'pkgIndex.tcl' -print \
				-o -type f ! -name '*.*' -executable -exec sed -sne '1{/^#!.*\(tcl\|wi\)sh*/F};1,5{/^[[:space:]]*exec[[:space:]]\+\(tcl\|wi\)sh*/F}' '{}' + \
				| xargs -r sed -n -e 's|^[^#]*package require \([^ }]*\).*|\1|gp' \
				| sort -u)
		do
			alldeps+=" "$(grep -l "package ifneeded $tcldep " $tclpkgs || true)
		done
	fi

	# commands called in postinstall/preremove scripts
	for pidep in $(find "${@//^_^/ }" -path 'etc/postinstall/*.sh' -o -path 'etc/preremove/*.sh' \
			| xargs -r sed -ne 's|^\(/[^ ]*\).*|\1|gp' | sort -u)
	do
		if [ -x ${D}${pidep} ]
		then
			alldeps+=" "${D}${pidep}
		else
			alldeps+=" "${pidep}
		fi
	done

	# symlinks, possibly to other (sub)packages
	# FIXME: could be improved in the case of relative links where
	# the target is from another package
	alldeps+=" "$(find "${@//^_^/ }" -type l | xargs -r readlink -f)

	# add perl5_0xy requirement if linked with cygperl5_xy.dll
	# add ruby_xy requirement if linked with cygrubyxy0.dll
	# add php_x_y requirement if linked with cygphp-x-y.dll
	for dll in $(echo ${alldeps} | tr \\n ' ' ) ; do
	    case "${dll##*/}" in
		cygperl5_[0-9][0-9].dll)
		    plver="${dll##*cygperl5_}"
		    plver="${plver%%.dll}"
		    echo "perl5_0${plver}"
		    ;;
		cygruby[0-9][0-9]0.dll)
		    rbver="${dll##*cygruby}"
		    rbver="${rbver%%0.dll}"
		    echo "ruby_${rbver}"
		    ;;
		cygphp-[0-9]-[0-9].dll)
		    phpver="${dll##*cygphp-}"
		    phpver="${phpver%%.dll}"
		    phpver="${phpver//-/_}"
		    echo "php_${phpver}"
		    ;;
	    esac
	done

	if [ "${CBUILD##*-}" = "cygwin" ]
	then
	    if [ -n "$expldeps" ]
	    then
	       echo $expldeps | sort -u;
	    fi
	    echo $alldeps | sort -u | xargs -r /bin/cygcheck -f;
	else
	    # cannot determine Cygwin package dependencies, but we can give
	    # some guidance by listing the necessary libraries
	    echo $alldeps | sort -fu;
	fi

	find ${T} -maxdepth 1 -name '.*.lst' \
		| xargs -r grep -l $(echo $alldeps | sed -e "s|${D}/\([^ ]*\)|\1|g;s| /[^ ]*||g;s/\([^ ]*\)/-e ^\1$/g") \
		| sort -u | sed -e "s|${T}/\.\(.*\)\.lst|\1-${PVR}|";

	popd

	PATH="${os_path}"
}

__show_deps() {
	pushd ${D}
	__list_deps * | sort -fu | sed "/^${PN}-[0-9].*$/d" | __pager
	popd
}

__show_web() {
	local mybrowser mywww

	if defined HOMEPAGE
	then
		mywww=${HOMEPAGE}
	else
		warning "HOMEPAGE undefined, searching Google for ${ORIG_PN:-${PN}}"
		mywww="https://www.google.com/search?q=${ORIG_PN:-${PN}}"
	fi

	for prog in "${BROWSER}" xdg-open cygstart false
	do
		if check_prog ${prog%% *}
		then
			mybrowser=${prog};
			break;
		fi
	done

	${mybrowser} ${mywww} &>/dev/null &
}

__show_vars() {
	for v in "$@"
	do
		if [ -v ${v} ]
		then
		    declare -p "${v}"
		fi
	done
}

readonly -f __list_files __list_debug_files __list_deps \
	    __show_deps __show_info __show_web __pager \
	    __show_vars
