#!/bin/sh
#
# makebutton
#
# Create 1 button gif-image using Imagemagick utilities, by reading
# from a button-text file and writing to the equivalent .gif file.
#
# Usage:  makebutton buttonname
#
# If buttonname has an extension, it is removed before continuing.
# Then we read from buttonname.button, and write to buttonname.gif.
#
# Copyright (C) 1993-1999 Paul A. Balyoz <pab@domtools.com>
#

# Parse args first
file=`echo "$1" | awk -F. '{print $1}'`
infile="$file.button"
file="$file.gif"

# Parameters
xedge=5
yedge=5
xraise=2
yraise=2
font="-adobe-helvetica-bold-r-normal--11-80-100-100-p-60-iso8859-1"

xcorner=$xedge
ycorner=`expr $yedge - 1`

#
# Make template
#

if test ! -f template.gif; then
	echo "building template.gif..."
	cp /home/wwwtest/html/images/graypixel.gif template.gif
	mogrify -geometry 300x100\! template.gif
fi

#
# Compute font's height
#

temptext="jygqXMml10"		# characters with ascenders and descenders
tempfile="temp.gif"

# paint the sample text
cp template.gif $tempfile
mogrify -pen gray50 -draw 'fillRectangle +0+0 +9999+9999' $tempfile
mogrify -pen white -font "$font" -draw 'text 10,10 "'"$temptext"'"' $tempfile

# trim the edges so we can see size of painted words
mogrify -crop 0x0 $tempfile
set `identify $tempfile | perl -ne '/\s(\d+)x(\d+)+\+\d+\+\d+\s/ && print "$1 $2\n"'`
#width="$1"
height="$2"

rm $tempfile

#
# Create a button, given the text string and gif filename to write to.
#

if test ! -f "$infile"; then
	echo "$0: $infile: file not found." >&2
	exit 1
fi
text=`cat $infile`

# paint our words on a big template
cp template.gif $file
mogrify -pen gray50 -draw 'fillRectangle +0+0 +9999+9999' $file
mogrify -pen white -font "$font" -draw 'text 10,10 "'"$text"'"' $file

# trim the edges so we can see size of painted words
mogrify -crop 0x0 $file
set `identify $file | perl -ne '/\s(\d+)x(\d+)+\+\d+\+\d+\s/ && print "$1 $2\n"'`
width="$1"
#height="$2"

# add raised edging
newwidth=`expr $xedge + $width + $xedge`
newheight=`expr $yedge + $height + $yedge`

# paint our words again, now that we know right size to make image
cp template.gif $file
mogrify -geometry ${newwidth}x${newheight}\! $file
mogrify -pen gray50 -draw 'fillRectangle +0+0 +9999+9999' $file
mogrify -pen white -font "$font" -draw 'text '$xcorner,$ycorner' "'"$text"'"' $file
mogrify -raise ${xraise}x${yraise} $file

exit 0
