#!/usr/local/bin/bash
# Copyright (C) Naotoshi Seo, s1080134

function printUsage() {
  cmd=${0##*/}
  echo "Batch nkf. nkf front end." 1>&2
  echo "Usage: $cmd [nkf options] [file ...]" 1>&2
  echo "         ex) $cmd -d -e *.html (euc)" 1>&2
  echo "         ex) yes | $cmd -d -s ../*  (sjis)" 1>&2
}
if [ $# -lt 1 ]; then
  printUsage
  exit 0
fi

#Options
while [ $# -gt 0 ]
  do
  if [ $(expr "x$1" : 'x-') -eq 0 ];then
    break
  fi
  options="$options$1 "
  shift
done
#$@ become [file ... ]

#main
for file in $@
  do
  echo "nkf $options$file > /tmp/tmp ; mv -i /tmp/tmp $file"
  nkf $options$file > /tmp/tmp
  if [ "$?" != "0" ];then
    break
  fi
  mv -i /tmp/tmp $file
done
