#!/bin/bash
#
#  Extract character images from a image array
#
# Anthony Thyssen  <A.Thyssen@grffith.edu.au>

# [ -f ./generate_options     ] && . ./generate_options
# [ -f ../generate_options    ] && . ../generate_options
# [ -f ../../generate_options ] && . ../../generate_options

# Assumes font image is a concatanation of 32x32 images in a 10x4 array.
[ -f images/font.gif       ] && font=images/font.gif
[ -f ../images/font.gif    ] && font=../images/font.gif
[ -f ../../images/font.gif ] && font=../../images/font.gif

if [ -f ../../images/font.gif ]; then
  echo >&2 "generate_font_chars: unable to find 'font' image."
  exit 10
fi

if [ "X$1" = 'X--numbers' ]; then
  echo "Extracting Characters from a font table"
  convert $font -crop 320x32+0+96 +repage -crop 32x32 +repage font_%d.gif
  #x=0
  #while [ $x -le 9 ]; do
  #  convert $font -crop 32x32+`expr 32 \* $x`+96 -repage 0x0+0+0 font_$x.gif
  #  x=`expr $x + 1`
  #done
fi

if [ "X$1" = 'X--append' ]; then
  # For the word "APPEND"
  convert $font -crop 32x32+0+0    +repage  font_A.gif
  convert $font -crop 32x32+160+32 +repage  font_P.gif
  convert $font -crop 32x32+128+0  +repage  font_E.gif
  convert $font -crop 32x32+96+32  +repage  font_N.gif
  convert $font -crop 32x32+96+0   +repage  font_D.gif
fi

if [ "X$1" = 'X--legacy' ]; then
  # For the word "LEGACY!"
  convert $font -crop 32x32+32+32  +repage  font_L.gif
  convert $font -crop 32x32+192+0  +repage  font_G.gif
  convert $font -crop 32x32+64+0   +repage  font_C.gif
  convert $font -crop 32x32+128+64 +repage  font_Y.gif
  convert $font -crop 32x32+224+64 +repage  font_!.gif
fi

# Optimise generated chars
for gif in  font_?.gif
do
  if [ "$gifsicle" ]; then
    #echo "Optimizing GIF format of \"$gif\"..."
    gifsicle -O -b $gif
  fi
  chmod -f 644 $gif
done

