#!/bin/sh
# Complete check of a stateless encoding.
# Usage: check-stateless SRCDIR CHARSET
srcdir="$1"
charset="$2"
set -e
# For systems with severe filename restrictions
# allow for an alternate filename.
UNAME=${UNAME-`uname 2>/dev/null`}
case X$UNAME in
  *-DOS) filename=`echo "$charset" | sed "s|ISO-|ISO/|; \
                                          s|Mac|Mac/|; \
                                          s|Georgian-|Georgian/|"`
         tmp_filename=`echo "$filename" | sed "s|/|/tmp-|"`
         tmp_orig_filename=`echo "$filename" | sed "s|/|/tmp-orig-|"` ;;
  *)     filename="$charset"
         tmp_filename="$charset"
         tmp_orig_filename="$charset" ;;
esac

# iconv in one direction.
./table-from "$charset" > "${srcdir}"/"$tmp_filename".TXT

# iconv in the other direction.
./table-to "$charset" | sort > "${srcdir}"/"$tmp_filename".INVERSE-TXT

# Check 1: charmap and iconv forward should be identical.
cmp "${srcdir}"/"$filename".TXT "${srcdir}"/"$tmp_filename".TXT 2> /dev/null

# Check 2: the difference between the charmap and iconv backward.
sed -e '/	.* 0x/d' < "${srcdir}"/"$filename".TXT > tmp-noprecomposed-"$charset".TXT
if test -f "${srcdir}"/"$filename".IRREVERSIBLE-TXT; then
  cat tmp-noprecomposed-"$charset".TXT "${srcdir}"/"$filename".IRREVERSIBLE-TXT | sort | uniq -u > tmp-orig-"$charset".INVERSE-TXT
else
  cp tmp-noprecomposed-"$charset".TXT tmp-orig-"$charset".INVERSE-TXT
fi
cmp tmp-orig-"$charset".INVERSE-TXT "${srcdir}"/"$tmp_filename".INVERSE-TXT 2> /dev/null

rm -f "${srcdir}"/"$tmp_filename".TXT "${srcdir}"/"$tmp_filename".INVERSE-TXT tmp-noprecomposed-"$charset".TXT tmp-orig-"$charset".INVERSE-TXT
exit 0
# For a new encoding:
# You can create the "$charset".TXT like this:
#   ./table-from "$charset" > "$charset".TXT
# You can create the "$charset".IRREVERSIBLE-TXT like this:
#   ./table-to "$charset" | sort > "$charset".INVERSE-TXT
#   diff "$charset".TXT "$charset".INVERSE-TXT | grep '^[<>]' | sed -e 's,^. ,,' > "$charset".IRREVERSIBLE-TXT
