#!/bin/sh -
#
# xbm2xpm [-t]  files...
#
#   This uses the pbmplus package to convert the xbm files to xpm format
# The final xpm format is fixed with regards to internal color and filename
# and is a -t option is given the XBitmap white color becomes transparent.
#
# NOTE: created files are placed in the current directory.
#
# Original script: 11 January 1993
# Last Change:     16 Feburary 1996
#
#  Anthony Thyssen   <anthony@cit.gu.edu.au>
#
TRANS=
if [ "X$1" = "X-t" ]; then
  TRANS="#BFBFBF"  # the transparent color to use.
  shift
fi

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

  name="`expr "//$i" : '.*/\([^/]*\)'`"               # remove path
  suffix="`expr "$name" : '.*\.\([^.]*\)$'`"   # extract last suffix 
  name="`expr "$name" : '\(.*\)\.[^.]*$'`"     # remove last suffix

  if [ "$suffix" != "xbm" ]; then
    echo >&2 "suffix not \".xbm\" : \"$i\""
    continue
  fi

  echo "converting \"$i\""
  if [ "$TRANS" ]; then
    sed 's/unsigned //' "$i" | xbmtopbm |
      pnmdepth 255 2>/dev/null | pgmtoppm "$TRANS" | ppmtoxpm 2>/dev/null |
          sed "/colors/,/pixels/s/c $TRANS/c None/" | xpm-fix -o "$name".xpm
  else
    sed 's/unsigned //' "$i" | xbmtopbm |
      ppmtoxpm 2>/dev/null | xpm-fix -o "$name".xpm
  fi

  if [ ! -s "$name.xpm" ]; then
    rm -f "$name.xpm"
    echo >&2 "Failed to convert \"$i\""
  fi

done
