#!/bin/bash
#
# this sample script can be used as a jpeg pruner for PIMPPA. With
# the current parameters, the script tells PIMPPA to delete all 
# incoming jpegs with either dimension smaller than 250x250, or 
# if the file size is smaller than 25000 bytes.
#
# uses: jpeginfo, du, sed
#

XMIN=250;
YMIN=250;
MINBYTES=25000;

###################################################################3

FILENAME="$1";

if [ -z "$FILENAME" ] ; then
  echo Usage: $0 filename
  exit 0;
fi

# does the file exist and with a size >0 ?
if [ ! -s "$FILENAME" ] ; then
  exit 1;
fi

# is the file too small?
FILESIZE=`du -b "$FILENAME" | sed -e "s/\t.*//g"`;
if [ $FILESIZE -lt $MINBYTES ] ; then
  exit 1;
fi

TMPFILE=`mktemp /tmp/p_prunejpeg.XXXXXX` || exit 0

jpeginfo -c -d -l "$FILENAME" >$TMPFILE
if [ ! -e "$FILENAME" ] ; then
  rm -f $TMPFILE;
  exit 1; # jpeginfo decided to delete a broken file
fi

if [ ! -s $TMPFILE ] ; then
  rm -f $TMPFILE;  # jpeginfo didnt create an output file, jpeginfo might be broken
  exit 0;
fi

read <$TMPFILE XLEN SEP YLEN REST;

# check dimensions 
if [ $XLEN -le $XMIN ] ; then
  rm -f $TMPFILE;  
  exit 1;
fi

if [ $YLEN -le $YMIN ] ; then 
  rm -f $TMPFILE;  
  exit 1;
fi

rm -f $TMPFILE;

exit 0;


