#! /bin/sh
# b-umap.  umap batch-call wrapper.
# To be called by enca, don't use directly. See enca(1) for calling convention.
#
# Copyright (C) 2000-2001 David Necas (Yeti) <yeti@physics.muni.cz>.
# This is free software; it can be copied and/or modified under the terms of
# version 2 of GNU General Public License, see COPYING for details.  There is
# NO WARRANTY; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
transform_enc_name() {
  echo "$1" | sed -e 's:/.*::'
}
umask 077
inenc=`transform_enc_name "$1"`
outenc=`transform_enc_name "$2"`
if test "$4" = "-"; then
  if umap "$inenc:$outenc" <"$3"; then
    rm -- "$3"
  else
    echo "$0: umap $inenc:$outenc failed" 1>&2
    rm -- "$3"
    exit 1
  fi
else
  temp=/tmp/tmp.b-umap$$
  if umap "$inenc:$outenc" <"$3" >"$temp"; then
    # use cat > to preserve permissions
    cat $temp >"$3"
  else
    echo "$0: umap $inenc:$outenc <$3 failed, file $3 unchanged" 1>&2
    rm -f $temp
    exit 1
  fi
  rm -f $temp
fi
