#!/bin/sh
# @(#) $Id: updateimgsize 52 2009-04-10 07:29:02Z leres $ (XSE)
#
# updateimgsize - image image size in html files
#

prog=`basename $0`

t1=/tmp/${prog}.1.$$
t2=/tmp/${prog}.2.$$

trap 'rm -f ${t1} ${t2}; exit 1' 1 2 3 15

while [ $# -gt 0 ]; do
	f=$1
	s=`basename $1`
	d=`dirname $1`
	if [ "${d}" = "${f}" ]; then
		d="."
	fi
	shift
	if [ ! -f ${f} ]; then
		echo "${prog}":" ${f} doesn't exist"
		continue
	fi
	if [ -h ${f} ]; then
		echo "${prog}":" ${f} is a symlink"
		continue
	fi

	(cd ${d} ; imgfilt ${s}) > ${t1}
	tstatus=$?
	if [ ${tstatus} != 0 ]; then
		rm -f ${t1} ${t2}
		exit ${tstatus}
	fi
	if [ ! -s ${t1} ]; then
		echo "${prog}":" temp file is empty!"
		continue
	fi

	c="diff ${f} ${t1}"
	${c} > ${t2} 2>&1
	tstatus=$?
	if [ ${tstatus} = 0 ]; then
		#echo "${prog}":" ${f} not modified"
		:
	elif [ ! -w ${f} ]; then
		echo ${c}
		cat ${t2}
		echo "${prog}":" ${f} not not writable"
	else
		echo ${c}
		cat ${t2}
		c="cp ${t1} ${f}"
		echo ${c}
		${c}
	fi
	rm -f ${t1} ${t2}
done
