#!/bin/sh

#
# This script removes the background color (black/white) from -pos/-neg PNGs
#

#If argument given, chdir into that directory
if [ -n "$1" ]
then
	cd "$1" || exit
fi
echo "Processing $PWD..."

TMP_DIR="$PWD/__tmp"
mkdir "$TMP_DIR"

for f in `find -iname '*-pos.png'`
do
  echo "$f"
  convert "$f" -colorspace HSB -separate -negate "$TMP_DIR/mask.png"
  convert "$f" -alpha Off "$TMP_DIR/mask-2.png" -compose CopyOpacity -composite "PNG32:$f.alpha.png"

  rm "$f"
  mv "$f.alpha.png" "$f"
done

for f in `find -iname '*-neg.png'`
do
  echo "$f"
  convert "$f" -colorspace HSB -separate "$TMP_DIR/mask.png"
  convert "$f" -alpha Off "$TMP_DIR/mask-2.png" -compose CopyOpacity -composite "PNG32:$f.alpha.png"

  rm "$f"
  mv "$f.alpha.png" "$f"
done

rm -rf "$TMP_DIR"
