#!/bin/sh

FILE_2_DESCRIBE="$1"
DESCRIPTION_FILE="$2"

if test -z "${DESCRIPTION_FILE}" ; then
	DESCRIPTION_FILE=$(mktemp)
fi


# file
echo "---- file ----" > ${DESCRIPTION_FILE}
file "${FILE_2_DESCRIBE}" >> "${DESCRIPTION_FILE}" 2>> "${DESCRIPTION_FILE}"
echo "" >> "${DESCRIPTION_FILE}"


# exiftool
BIN=`which exiftool`
if [ "x${BIN}x" != "xx" ]; then
	exiftool -g1 -ee -m -u "${FILE_2_DESCRIBE}" >> "${DESCRIPTION_FILE}" 2>> "${DESCRIPTION_FILE}"
	echo "" >> "${DESCRIPTION_FILE}"
fi


# PDF ?
file "${FILE_2_DESCRIBE}" | grep -q 'PDF'
if [ $? -eq 0 ] ; then
	# pdfinfo
	BIN=`which pdfinfo`
	if [ "x${BIN}x" != "xx" ]; then
		echo "---- pdfinfo ----" > "${DESCRIPTION_FILE}.pdfinfo"
		pdfinfo -box "${FILE_2_DESCRIBE}" >> "${DESCRIPTION_FILE}.pdfinfo" 2>> "${DESCRIPTION_FILE}.pdfinfo"
		if [ $? -eq 0 ] ; then
			cat "${DESCRIPTION_FILE}.pdfinfo" >> "${DESCRIPTION_FILE}"
			rm -f "${DESCRIPTION_FILE}.pdfinfo"
			echo "" >> "${DESCRIPTION_FILE}"
		fi
		pdfinfo -meta "${FILE_2_DESCRIBE}" >> "${DESCRIPTION_FILE}.pdfinfo" 2>> "${DESCRIPTION_FILE}.pdfinfo"
		if [ $? -eq 0 ] ; then
			cat "${DESCRIPTION_FILE}.pdfinfo" >> "${DESCRIPTION_FILE}"
			rm -f "${DESCRIPTION_FILE}.pdfinfo"
			echo "" >> "${DESCRIPTION_FILE}"
		fi
		pdfinfo -struct "${FILE_2_DESCRIBE}" >> "${DESCRIPTION_FILE}.pdfinfo" 2>> "${DESCRIPTION_FILE}.pdfinfo"
		if [ $? -eq 0 ] ; then
			cat "${DESCRIPTION_FILE}.pdfinfo" >> "${DESCRIPTION_FILE}"
			rm -f "${DESCRIPTION_FILE}.pdfinfo"
			echo "" >> "${DESCRIPTION_FILE}"
		fi
		rm -f "${DESCRIPTION_FILE}.pdfinfo"
	fi
	# pdfdetach (embedded files)
	BIN=`which pdfdetach`
	if [ "x${BIN}x" != "xx" ]; then
		echo "---- pdfdetach ----" > "${DESCRIPTION_FILE}.pdfdetach"
		pdfdetach -list "${FILE_2_DESCRIBE}" >> "${DESCRIPTION_FILE}.pdfdetach" 2>> "${DESCRIPTION_FILE}.pdfdetach"
		if [ $? -eq 0 ] ; then
			cat "${DESCRIPTION_FILE}.pdfdetach" >> "${DESCRIPTION_FILE}"
			echo "" >> "${DESCRIPTION_FILE}"
		fi
		rm -f "${DESCRIPTION_FILE}.pdfdetach"
	fi
	# pdfsig (signatures)
	BIN=`which pdfsig`
	if [ "x${BIN}x" != "xx" ]; then
		echo "---- pdfsig ----" > "${DESCRIPTION_FILE}.pdfsig"
		pdfsig "${FILE_2_DESCRIBE}" >> "${DESCRIPTION_FILE}.pdfsig" 2>> "${DESCRIPTION_FILE}.pdfsig"
		cat "${DESCRIPTION_FILE}.pdfsig" >> "${DESCRIPTION_FILE}"
		echo "" >> "${DESCRIPTION_FILE}"
		rm -f "${DESCRIPTION_FILE}.pdfsig"
	fi
	# extractpdfmark
	BIN=`which extractpdfmark`
	if [ "x${BIN}x" != "xx" ]; then
		echo "---- extractpdfmark ----" > "${DESCRIPTION_FILE}.extractpdfmark"
		extractpdfmark "${FILE_2_DESCRIBE}" >> "${DESCRIPTION_FILE}.extractpdfmark" 2>> "${DESCRIPTION_FILE}.extractpdfmark"
		cat "${DESCRIPTION_FILE}.extractpdfmark" >> "${DESCRIPTION_FILE}"
		echo "" >> "${DESCRIPTION_FILE}"
		rm -f "${DESCRIPTION_FILE}.extractpdfmark"
	fi
	# pdfimage
	BIN=`which pdfimages`
	if [ "x${BIN}x" != "xx" ]; then
		echo "---- pdfimages ----" > "${DESCRIPTION_FILE}.pdfimages"
		pdfimages -list "${FILE_2_DESCRIBE}" >> "${DESCRIPTION_FILE}.pdfimages" 2>> "${DESCRIPTION_FILE}.pdfimages"
		cat "${DESCRIPTION_FILE}.pdfimages" >> "${DESCRIPTION_FILE}"
		echo "" >> "${DESCRIPTION_FILE}"
		rm -f "${DESCRIPTION_FILE}.pdfimages"
	fi
## not a PDF
#else
#	echo "not PDF"
#	echo "---- identify ----" >> "${DESCRIPTION_FILE}"
#	# takes a long time on large images / PDF files / movies
#	identify -verbose "${FILE_2_DESCRIBE}" >> "${DESCRIPTION_FILE}" 2>> "${DESCRIPTION_FILE}"
#	echo "" >> "${DESCRIPTION_FILE}"
fi


# ffprobe
BIN=`which ffprobe`
if [ "x${BIN}x" != "xx" ]; then
	echo "---- ffprobe ----" > "${DESCRIPTION_FILE}.ffprobe"
	ffprobe -v verbose -hide_banner -pretty "${FILE_2_DESCRIBE}" >> "${DESCRIPTION_FILE}.ffprobe" 2>> "${DESCRIPTION_FILE}.ffprobe"
	if [ $? -eq 0 ] ; then
		cat "${DESCRIPTION_FILE}.ffprobe" >> "${DESCRIPTION_FILE}"
		echo "" >> "${DESCRIPTION_FILE}"
	fi
	rm -f "${DESCRIPTION_FILE}.ffprobe"
fi


# sfinfo
BIN=`which sfinfo`
	if [ "x${BIN}x" != "xx" ]; then
	echo "---- sfinfo ----" >> "${DESCRIPTION_FILE}"
	sfinfo "${FILE_2_DESCRIBE}" >> "${DESCRIPTION_FILE}" 2>> "${DESCRIPTION_FILE}"
fi


# pipe interface ?
if test -z "$2" ; then
	cat "${DESCRIPTION_FILE}"
	rm "${DESCRIPTION_FILE}"
fi

exit 0
