#!/bin/sh

#########################################################################
# tool to send commands to an APC interface
# this file is in the public domain --Andreas K. Foerster
#
# you may use code from this file and put it under any license
# you wish to use
#########################################################################

VERSION=2

help()
{
cat <<EOF
Usage: avtcmd [options] 'command'

The program sends commands to an APC interface.
(APC = Application Program Command)
The environment variable APC must be set.

This is meant for interactive use only. If you write your own
shell-scripts or programs, it is much better to use the
escape-sequences directly.

Options:
 -h, --help       show this help
 -v, --version    show version information
 -s, --sync       synchronize the working directory with lua-akfavatar

Examples:
  avtcmd "avt.avatar_image_file(avt.search('female_user.xpm'))"
  avtcmd "avt.set_avatar_name 'Lieschen Mueller'"

Report bugs to <bug-akfavatar@akfoerster.de>
EOF

exit 0
}

version()
{
  echo "avtcmd version: $VERSION"
  if [ -n "$AKFAVTTERM" ]
  then
    echo "AKFAvatar version: $AKFAVTTERM"
  fi
  
  exit 0
}

apc()
{
  if [ -n "$APC" ]
  then
    printf "\033_%s\033\\" "$*"
  else
    echo "error: environment variable APC not set" >&2
    exit 1
  fi
}

case "$1" in
  -h | --help | "") help ;;
  -v | --version) version ;;
  -s | --sync) apc "avt.set_directory('$(pwd)')"; shift ;;
esac

[ $# -ge 1 ] && apc "$*"

exit 0

