#!/bin/sh

for F in $(find . -name "*.png" | fgrep -v /doc/)
do
  echo $F
  OLDSIZE=$(ls -l $F | sed -e 's/  */ /g' | cut -d ' ' -f 5)
  optipng $F > /dev/null
  mogrify $F > /dev/null
  NEWSIZE=$(ls -l $F | sed -e 's/  */ /g' | cut -d ' ' -f 5)
  if [ $OLDSIZE -gt $NEWSIZE ]
  then
     echo $(basename $F) $OLDSIZE "->" $NEWSIZE
  else
      echo "Already optimized/not shrinking" $F $OLDSIZE "->" $NEWSIZE 1>&2
      git checkout $F > /dev/null || exit 1
  fi
done
