#! /bin/bash

# Define parameters
VERS=1.4
SRC="src/tkimg${VERS}.tar.gz"
SRCURL="http://sourceforge.net/projects/tkimg/files/tkimg/${VERS}/tkimg${VERS}.tar.gz/download"
BUILDDIR="tkimg${VERS}"
export VERS SRC SRCURL BUILDDIR

# Load common functions
source ../common.sh

# Do not build if the "tcl" platform has been requested
not_platforms 'tcl'

# Download source
download_src

# We require linking against Tk, so find Tk 8.4 and use it.  If it can't be found, abort.
tcl_plat_build_dir="$(basename "${TCLBUILDDIR}")"
tkdir="$(echo "$(pwd)/../Tk84/build"/tk*/${tcl_plat_build_dir})"
if [ ! -d "${tkdir}" -o ! -f "${tkdir}/tkConfig.sh" ]; then
	echo "Unable to find Tk 8.4 directory, please build Tk84 before Tkimg" >&2

	exit 1
fi

# Determine installation root

INSTROOT="$(pwd)/out"
export INSTROOT

# Build package
(
	rm -rf build
	mkdir -p "${INSTDIR}" >/dev/null 2>/dev/null
	mkdir build

	cd build/ || exit 1

	gzip -dc "../${SRC}" | tar -xf -
	cd "${BUILDDIR}" || exit 1

	INSTDIR="${INSTROOT}/__TMP__/${PLATFORM}"

	bash ./configure --enable-shared --with-tcl="${TCLCONFIGDIR}" --with-tk="${tkdir}" --prefix="${INSTDIR}" --libdir="${INSTDIR}" --bindir="${INSTDIR}" ${CONFIGUREEXTRA}

	${MAKE:-make} || exit 1

	${MAKE:-make} install || exit 1

	mv "${INSTDIR}/Img${VERS}"/* "${INSTDIR}/"
	rm -rf "${INSTDIR}/Img${VERS}"

	rm -f "${INSTDIR}"/*.a "${INSTDIR}"/*.sh
	rm -rf "${INSTDIR}/include" "${INSTDIR}/share"
) || exit 1

# Split package into pieces
(
	cd "${INSTROOT}/__TMP__/${PLATFORM}" || exit 1

	if [ ! -f "pkgIndex.tcl" ]; then
		echo "Unable to find pkgIndex.tcl in $(pwd), aborting."

		exit 1
	fi

	cat pkgIndex.tcl | while read package ifneeded pkg vers extra; do
		if [ "${package}" != "package" -o "${ifneeded}" != "ifneeded" ]; then
			continue
		fi

		INSTDIR="${INSTROOT}/${pkg}-${vers}/${PLATFORM}"

		mkdir -p "${INSTDIR}" || exit 1

		# Handle this meta-package
		if [ "${pkg}" = "Img" -a "${extra}" = "{" ]; then
			pkgfile=''

			echo "package ifneeded ${pkg} ${vers} {" > "${INSTDIR}/pkgIndex.tcl"

			while read line; do
				if [ "${line}" = "}" ]; then
					break
				fi

				echo "        ${line}" >> "${INSTDIR}/pkgIndex.tcl"
			done

			echo "}" >> "${INSTDIR}/pkgIndex.tcl"

			pkgdeps="$(cat "${INSTDIR}/pkgIndex.tcl" | grep 'package require' | sed 's@^.*package require @@' | tr "\n" ',')"

			"${BUILDSYSROOT}/create_teapot" "${INSTDIR}/teapot.txt" "${pkg}" "${vers}" "${SRCURL}" "${PLATFORM}" "Tcl 8.4,${pkgdeps}" "The Tkimg meta-package.  This package enhances Tk, adding support for many other Image formats: BMP, XBM, XPM, GIF (with transparency, but without LZW), PNG, JPEG, TIFF and postscript."

			continue
		fi

		pkgfile="$(echo "${extra}" | sed 's@^.*\$dir @@;s@\]*$@@')"

		if [ -z "${pkgfile}" -o ! -f "${pkgfile}" ]; then
			continue
		fi

		# Copy package files into package directory
		cp "${pkgfile}" "${INSTDIR}/"

		# Create metadata
		## Tcl pkgIndex file
		echo "package ifneeded ${pkg} ${vers} [list load [file join \$dir \"${pkgfile}\"]]" > "${INSTDIR}/pkgIndex.tcl"
	done
) || exit 1

rm -rf "${INSTROOT}/__TMP__"

# Create teapot metadata -- need to have all packages in place prior to this
(
	cd "${INSTROOT}" || exit 1

	for dir in */; do
		(
			cd "${dir}" || exit 1

			cd "${PLATFORM}" || exit 1

			if [ ! -f "pkgIndex.tcl" ]; then
				exit 1
			fi

			if [ -f "teapot.txt" ]; then
				exit 0
			fi

			pkg="$(echo "${dir}" | cut -f 1 -d '-')"
			vers="$(echo "${dir}" | cut -f 2 -d '-' | sed 's@//*$@@')"

			pkgdeps=''
			for deppkg in $(find . -type f | xargs "${NM:-nm}" 2>/dev/null | grep StubsPtr | sed 's@^.* \([^ ]*\)StubsPtr *[^ ]*$@\1@;s@^tkimg$@img::base@'); do

				if [ "${deppkg}" = "${pkg}" ]; then
					continue
				fi

				deppkgdir="$(echo ../../${deppkg}-*)"
				if [ ! -d "${deppkgdir}" ]; then
					continue
				fi

				pkgdeps="${pkgdeps},${deppkg}"
			done

			"${BUILDSYSROOT}/create_teapot" "teapot.txt" "${pkg}" "${vers}" "${SRCURL}" "${PLATFORM}" "Tcl 8.4${pkgdeps}" "Part of the Tkimg package"
		)
	done
) || exit 1

exit 0
