#!/bin/sh
#recode  UTF-16LE..UTF-8
#iconv -f UTF-16LE -t UTF-8

set -e

if [ $# == 0 ]; then
  rembom | recode  UTF-16LE..UTF-8
  exit 0
fi

while [ $# != 0 ]; do

  echo "$1"

  if [ ! -w "$1" ] ; then
    echo "error: cannot write $1"
    exit 1
  fi

  if ! hasbom < "$1" ; then
    echo "error: no BOM in $1"
    exit 1
  fi

  BACK=$1.bak
  cp $1 $BACK

  if ! (rembom | recode UTF-16LE..UTF-8) < $BACK > $1; then
    echo "error: $0: $1: i don't work"; exit 1;
  fi 

  shift
done

