#!/bin/bash
#
# shade
#
# Copyright (c) 2007  Donn.C.Ingle <[EMAIL PROTECTED]>
# Released under the GPL.  The moral right of the authors to claim authorship
# of this shadowy work has been asserted.
#
# PURPOSE:
# Put shadows under stuff
#
# USAGE:
#     shade -h 
#
# BUGS:
# Don't throw weird filenames at it.
# Won't shadow other than southeast

#Some defaults
col=black
darkness=60
fuzzy=2
xoff=4
yoff=4
wantsnq=0 #assume user does not want to crush pngs.
hasnq=1 #assumes pngnq is installed.

#Function that does the heavy-lifting
shadow () {
        #darknessxfuzzyness+xoff+yoff:
        #offsets can be negative, i.e. to left or upwards
        #but we are not handling them in this script.
        #so southeast only - i.e. +x+y
        filter=$darkness\x$fuzzy\+$xoff\+$yoff
        
        #Experimental code for other directions
        #xoff=$(( $xoff + 2 * $fuzzy))
        #yoff=$(( $yoff + 2 * $fuzzy))
        #splice=$xoff\x$yoff
        
        #Original version:
        #/opt/bin/convert "$1" -background none -gravity southeast -splice $splice \( +clone -background Black -shadow $thing \) -background none -compose DstOver -flatten temp.png
        
        #This one make the canvas too big all round.
        #/opt/bin/convert "$1" \( +clone -background black -shadow $thing \) +swap -repage +$xoff+$yoff\! -background none  -mosaic  temp.png
        
        #Nice one.
        convert "$1" \( +clone -background $col -shadow $filter \) +swap -background none -mosaic temp.png
        
        #PNGs can be HUGE. I cannot figure out how to reduce them in imagemagick,so I rely on pngnq which rocks.
        [[ $wantsnq = 1 && $hasnq = 1 ]] && (pngnq -f temp.png 2>/dev/null; mv temp-nq8.png temp.png) #compress the heck out of it
        #Now, let's make sure we copy OVER older shadow files
        #If the current name is "shadow.xxxxx" then strip that.
        nam=`echo "$1" | sed 's/shadow\.//'`
        #Strip the original file extension
        notype=${nam%.*}
        #Build the new name
        newname=shadow.${notype}.png
        #Now rename it, erasing any older shadow file.
        mv temp.png $newname
}

#Function that displays the help.
help () {
    echo "The shadowy shadow script"
    echo 
    echo "-h     : This message"
    echo "-d n   : Darkness of shadow (=$darkness)"
    echo "-f n   : Fuzzyness (=$fuzzy)"
    echo "-x n   : Offset x (=$xoff)"
    echo "-y n   : Offset y (=$yoff)"
    echo "-c col : Colour of shadow, name or rgb code. Put it in quotes.(\"$col\")"
    echo "-l     : Lists all the colours and quits"
    echo "-crush : Will use pngnq to crush the life out of the png. It gets converted to an 8-bit file, but the size is really worth it."
    echo 
    echo "All files will be output with \"shadow.\" in front and as png."
    echo "Examples:"
    echo "$0 -d 50 -f 3 -x 2 -y 2 file1.jpg file2.gif file3.png"
    echo "$0 -d 20 *"
    echo "$0 -crush -c purple *.jpg"
    echo
    echo "You can also repeat arguments:"
    echo "$0 -c wheat pic1.jpg -crush -c purple pic2.jpg"
}

#Look for convert command
if [[ ! -e `which convert` ]]; then
 echo "You need Imagemagick installed."
 echo "Ubuntu : sudo apt-get install imagemagick"
 echo "Then try again."
 exit 0
fi

#No params == help
[ $# = 0 ] && help && exit 0
#Look for the pngnq app. Set a var.
if [[ ! -e `which pngnq` ]]; then
 echo "Tip: Install pngnq for totally awesome png compression. (Ubuntu : sudo apt-get install pngnq.)"
 hasnq=0
fi

#Keep going while there are still parameters:
until [ -z "$1" ]
 do
   #Was it help?
   [[ "$1" = "-h" ]] && help &&  exit 0
   #Was it list colour?
   [[ "$1" = "-l" ]] && convert -list color | cut -d " " -f 1 | sed 's/-*//' | column && exit 0
   #Was it crush?
   if [[ "$1" = "-crush" ]]; then 
    wantsnq=1; shift 
    [[ $hasnq = 0 ]] && echo "Cannot crush, you haven't got pngnq installed."
   fi
   #Was it colour?
   if [[ "$1" = "-c" ]]; then
    col="$2"
    shift; shift
   fi
   #Was it Darkness?
   if [[ "$1" = "-d" ]]; then
    darkness="$2"
    shift; shift
   fi
   #Was it fuzzy?
   if [[ "$1" = "-f" ]]; then
    fuzzy="$2"
    shift; shift
   fi
   #Was it x?
   if [[ "$1" = "-x" ]]; then
    xoff="$2"
    shift; shift
   fi
   #Was it y?
   if [[ "$1" = "-y" ]]; then
    yoff="$2"
    shift; shift
   fi
   #Was it a file?
   if [[ -e "$1" ]]; then
    #yes, it's a file, but is it an image?
    file "$1" | grep image 1>/dev/null
    [[ $? = 0 ]] && shadow "$1" #yes, so shadow it.
   fi
 shift #remove that filename
 done
exit 0

