#! /bin/bash

# les fichiers ne doivent pas contenir d'espaces ni de |

case $DEBUG in
  1) set -x
     ;;
  *)
     ;;
esac

usage() {
		error "usage : not_growing.sh [-h] -f 'files' -b dbfile -l logfile [-i $IFS]"
		error "example : not_growing.sh -f '*/*.jpg' -b /tmp/dbfile -l not_growing.log -i '\n'"
}

error() { # juste pour usage
		echo $1 1>&2
}

while true
do
  case $1 in
    -f)	files=$2
		shift 2
		;;
    -b) dbfile=$2
        shift 2
		;;
    -h) usage
		exit 0
		;;
	-l) logfile=$2
		shift 2
		;;
	-i) IFS=$2
		shift 2
		;;
     *)
		break
		;;
  esac
done

if [ -z "$files" -o -z "$dbfile" -o -z "$logfile" ]
then
		usage
		exit 1
fi

date=`date '+%d-%m-%Y'`
error() {
		echo "ERROR:not_growing.sh:$date: $1" 1>&2
		echo "ERROR:not_growing.sh:$date: $1" >> $logfile
}

TMPDIR=/tmp/`whoami`
mkdir -p ${TMPDIR}
if [ $? -ne 0 ]
then
	error "cannot mkdir -p ${TMPDIR}"
	exit 1
fi

for file in $files
do 
		if [ -s "$file" ] 
		then
				ls -lQ "$file"
		else
				rm -f "$file" 2> /dev/null
		fi
done | awk '{ print $5"\""$0 }' | awk -F'"' 'BEGIN { OFS="|" } { print $3,$1 }' | sort > ${TMPDIR}/not_growing_$$

if [ -f $dbfile ]
then
		join -t'
' $dbfile ${TMPDIR}/not_growing_$$ | awk -F'|' '{ print $1 }'
fi

mv -f ${TMPDIR}/not_growing_$$ $dbfile
if [ $? -ne 0 ]
then
		error "cannot mv -f ${TMPDIR}/not_growing_$$ $dbfile"
		exit 1
fi

