#!/bin/sh

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

if test -z "${DESCRIPTION_FILE}" ; then
	DESCRIPTION_FILE="${FILE_2_DESCRIBE}.txt"
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

#echo "---- identify ----" >> "${DESCRIPTION_FILE}"
# takes a long time on larger images / PDF files
#identify -verbose "${FILE_2_DESCRIBE}" >> "${DESCRIPTION_FILE}" 2>> "${DESCRIPTION_FILE}"
#echo "" >> "${DESCRIPTION_FILE}"

# 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

# 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

exit 0
