#!/bin/sh -
#
# anim2gif [-b BASENAME] file.anim...
#
# This is a simple script to magick my own animation sequence file
# which basically is a list of IM command line options, with added
# comments back into a GIF Animations.      file.anim --> file_anim.gif
#
# The animation sequence file can be extracted from an existing GIF animation
# using the "gif2anim" script. It can also be generated using some other
# porgram, or by hand.  It does not actually even need to generate a GIF
# animation but could actually be any sequence of "magick" command line
# options.  However it is designed for generating GIF aniamtions.
#
# OPTIONS
#    -b framename   basename for the individual frames
#    -g             Add '.gif' to end of basename, not '_anim.gif'
#    -c             Input frames are coalesced, ignore any initial page size
#
# The options basically perform slight modifications to the input animation
# sequence file, and assumes it is in the format generated by the "gif2anim"
# script.  They are provided for convenience only.
#
# It does not currently attempt to optimize the animation in any way, unless
# that optimization was given in the animation sequence file given.
#
####
#
# WARNING: Input arguments are NOT tested for correctness.
# This script represents a security risk if used ONLINE.
# I accept no responsiblity for misuse. Use at own risk.
#
#  Anthony Thyssen    20 April 1996
#
ORIGDIR=`pwd`
PROGNAME=`type $0 | awk '{print $3}'`  # search for executable on path
PROGDIR=`dirname $PROGNAME`            # extract directory of program
PROGNAME=`basename $PROGNAME`          # base name of program
Usage() {                              # output the script comments as docs
  echo >&2 "$PROGNAME:" "$@"
  sed >&2 -n '/^###/q; /^#/!q; s/^#//; s/^ //; 3s/^/Usage: /; 2,$ p' \
          "$PROGDIR/$PROGNAME"
  exit 10;
}

#VERBOSE=       # report the animation images being processed
#debug=true     # debug the magick line executed

basename=      # use this name for the individual frame images
sfx=_anim.gif  # the suffix to and to created GIF animation
COALESCED=     # frames are coalesced, ignore initial set page size option

while [ $# -gt 0 ]; do
  case "$1" in
  --help|--doc*) Usage ;;

  -b) shift; basename="$1" ;;
  -g) sfx=.gif ;;
  -c) COALESCED='/^-page [0-9][0-9]*x[0-9][0-9]*$/d' ;;
  --debug) debug=true ;;

  --) shift; break ;;    # end of user options
  -*) Usage "Unknown option \"$1\"" ;;
  *)  break ;;           # end of user options
  esac
  shift   # next option
done

[ $# -gt 1 ] && VERBOSE=true  # report each file as it is magicked

for i in "$@" ; do
  if [ ! -r "$i" ]; then
    echo >&2 "Unable to read \"$i\""
    continue
  fi

  [ "$VERBOSE" ] && echo "magicking \"$i\""

  path=`expr "$i" : '\(.*\)/'`                 # get file path (if any)
  name=`expr "//$i" : '.*/\([^/]*\)'`           # remove path to file
  suffix=`expr "$name" : '.*\.\([^./]*\)$'`     # extract last suffix
  name=`expr "$name" : '\(.*\)\.[^.]*$'`        # remove last suffix
  : ${path:=.}

  bname="$name"
  [ "$basename" ] && bname="$basename"

  case "$suffix" in
    anim) ;;
    *) echo >&2 "Skipping Non Animation file: \"$i\""
       continue ;;
  esac

  # This is the heart of the animation creator
  # The sed script removes comments from the scripts options
  # though ensures it does not remove any #RRGGBB color arguments.
  #
  cd $path
  [ "$debug" ] && set -x
  #magick -adjoin -colors 256 \
  sed 's/#[^0-9a-fA-F].*//; s/#$//;
       s/[ >:::]*$//;
       '"$COALESCED"';
       s/BASENAME/'"$bname"'/;
       /^$/d;
       ' $name.$suffix |\
  magick `cat` $ORIGDIR/$bname$sfx 2>&1
      #|| echo "anim2gif: Convert returned failed error status $?"
      #|| rm -f $ORIGDIR/$bname$sfx
  [ "$debug" ] && set -

  # Return to the original directory
  cd $ORIGDIR

  # Removed as being obsolete.
  #[ -f $bname$sfx ] &&
  #    gifsicle -O2 --batch $bname$sfx

  if [ ! -f "$bname$sfx" ]; then
    echo >&2 "anim2gif: Failed to magick \"$i\" into \"$bname$sfx\""
  fi

done
