#!/bin/sh
#
# xbm-cmd [-t] Ximage  pbmplus-command args...
#
#    Perform a pbmplus command on the xbm file given.
#
#  Anthony Thyssen         16 Dec 1993       anthony@cit.gu.edu.au
#
i="$1"
shift

exec </dev/null

TMP1=/tmp/xbmcmd$$.1
TMP2=/tmp/xbmcmd$$.2
trap "rm -f $TMP1 $TMP2; exit 0" 0

# on any failure exit
set -e "$@"

if [ ! -r "$i" ]; then
  echo >&2 "xbm-cmd: Unable to read \"$i\""
  exit 10;
fi
if [ ! -w "$i" ]; then
  echo >&2 "xbm-cmd: Unable to write \"$i\""
  exit 10;
fi

# --- find out the type ---
name="`expr "//$i" : '.*/\([^/]*\)'`"               # remove path
suffix="`expr "$name" : '.*\.\([^.]*\)'`"
name="`expr "$name" : '\([^.]*\)'`"

# --- convert to PbmPlus format ---
case "$suffix" in
  xbm) sed 's/unsigned //' "$i" | xbmtopbm > $TMP1 ;;
  xpm) x2p.sed "$i" | xpmtoppm  > $TMP1 ;;
  *)   echo >&2 "Unknown suffix for \"$i\""
       exit 0 ;;
esac

# --- execute the command given ---
"$@" < $TMP1 > $TMP2

if [ ! -f $TMP2 -o ! -s $TMP2 ]; then
  echo >&2 "Command failed for \"$i\""
  exit 0;
fi

# --- convert back ---
case "$suffix" in
  xbm) pbmtoxbm < $TMP2 |
       sed 's/static  *char/static unsigned char/;
            s/noname\([[_]\)/'"$name"'\1/' > "$i" ;;
  xpm) ppmtoxpm < $TMP2 2>/dev/null | xpm-fix -o "$i" ;;
esac 2>/dev/null

