#!/bin/bash

MP="/opt/local"

hasq() {
        [[ " ${*:2} " == *" $1 "* ]]
}

checkdeps-list() {
	local BASE=$(otool -L "${@}" 2> /dev/null | egrep '/usr/X11/lib|/opt/local/lib' | awk '{print $1}' | sed 's:.*/::' | sort -u)
	local BASE_US=$(otool -D "${@}" 2> /dev/null | grep -v ':' | egrep '/usr/X11/lib|/opt/local/lib' | sed 's:.*/::' | sort -u)
	local f
	local l
	local lv
        local our_deps=""

	for f in ${BASE} ; do
		hasq $f ${BASE_US} && continue

		if [[ -f /usr/X11/lib/$f ]] ; then
			lv=${f%.dylib}
			l=${lv%.*}

			case $l in
				#libGL*)           our_deps="${our_deps} lib:$lv:mesa" ;;
				#libXaw)           our_deps="${our_deps} lib:$l:xorg-libXaw" ;;
				#libXss)           our_deps="${our_deps} lib:$lv:xorg-libXScrnSaver" ;;
				#libXmuu)          our_deps="${our_deps} lib:$lv:xorg-libXmu" ;;
				#libXrender)       our_deps="${our_deps} lib:$lv:xrender" ;;
				#libICE)           our_deps="${our_deps} lib:$lv:xorg-libice" ;;
				#libSM)            our_deps="${our_deps} lib:$lv:xorg-libsm" ;;
				#libXRes)          our_deps="${our_deps} lib:$lv:xorg-libXres" ;;
				#libXpm)           our_deps="${our_deps} lib:$lv:xpm" ;;
                                #libXft)           our_deps="${our_deps} lib:$lv:Xft2" ;;
				#libfreetype)      our_deps="${our_deps} port:freetype" ;;
				#libfontconfig)    our_deps="${our_deps} port:fontconfig" ;;
				#libpixman-1)      our_deps="${our_deps} lib:$lv:libpixman" ;;
				#libcairo)         our_deps="${our_deps} path:lib/pkgconfig/cairo.pc:cairo" ;;
				#libpng12)         our_deps="${our_deps} port:libpng" ;;
				#libpng)           our_deps="${our_deps} port:libpng" ;;
				#*)                our_deps="${our_deps} lib:$lv:xorg-$l" ;;
				libGL*)           our_deps="${our_deps} port:mesa" ;;
				libXss)           our_deps="${our_deps} port:xorg-libXScrnSaver" ;;
				libXmuu)          our_deps="${our_deps} port:xorg-libXmu" ;;
				libXrender)       our_deps="${our_deps} port:xrender" ;;
				libICE)           our_deps="${our_deps} port:xorg-libice" ;;
				libSM)            our_deps="${our_deps} port:xorg-libsm" ;;
				libXRes)          our_deps="${our_deps} port:xorg-libXres" ;;
				libXpm)           our_deps="${our_deps} port:xpm" ;;
                                libXft)           our_deps="${our_deps} port:Xft2" ;;
				libfreetype)      our_deps="${our_deps} port:freetype" ;;
				libfontconfig)    our_deps="${our_deps} port:fontconfig" ;;
				libpixman-1)      our_deps="${our_deps} port:libpixman" ;;
				libcairo)         our_deps="${our_deps} path:lib/pkgconfig/cairo.pc:cairo" ;;
				libpng12)         our_deps="${our_deps} port:libpng" ;;
				libpng)           our_deps="${our_deps} port:libpng" ;;
				*)                our_deps="${our_deps} port:xorg-$l" ;;
			esac
		fi
	done

	echo ${our_deps}
}

checkdeps-list-filtered() {
	local BASE=$(otool -L "${@}" 2> /dev/null | egrep '/usr/X11/lib|/opt/local/lib' | awk '{print $1}' | sed 's:.*/::' | sort -u)
	local BASE_US=$(otool -D "${@}" 2> /dev/null | grep -v ':' | egrep '/usr/X11/lib|/opt/local/lib' | sed 's:.*/::' | sort -u)
	local f
	local l
	local lv
	local filter_out=""
        local our_deps=$(checkdeps-list "${@}")

	for f in ${BASE} ; do
		hasq $f ${BASE_US} && continue

		if [[ -f /usr/X11/lib/$f ]] ; then
			filter_out="${filter_out} $(checkdeps-list /usr/X11/lib/$f)"
		fi
	done

	for f in ${our_deps} ; do
		if ! hasq ${f} ${filter_out} ; then
			echo -n "${f} "
		fi
	done
}

checkdeps() {
        echo -n "depends_lib"
	for dep in $(checkdeps-list-filtered "${@}") ; do
		echo ' \'
		echo -n "	${dep}"
	done
}

PORT=$(basename $(pwd))

if [[ -n "$1" ]] ; then
	PORT=$1
else 
	echo "Assuming port: $PORT" >&2
fi

DESTDIR=""

if [[ -n "$(port work $PORT)" && -d "$(port work $PORT)"/destdir ]] ; then
	DESTDIR="$(port work $PORT)"/destdir
elif [[ -d "${MP}/var/macports/software/${PORT}" ]] ; then
	DESTDIR="${MP}/var/macports/software/${PORT}/*"
fi

if [[ -z "${DESTDIR}" ]] ; then
	echo "Couldn't determine the destdir for the port." >&2
	exit 1
fi

echo "Using destdir: ${DESTDIR}"

checkdeps ${DESTDIR}/${MP}/{bin,lib}/*
