########################################################################
#                                                                      #
#   Copyright (c) 2007 SuSE Linux Products GmbH, Nuernberg, Germany    #
#                                                                      #
#   This library is free software; you can redistribute it and/or      #
#   modify it under the terms of the GNU Lesser General Public         #
#   License as published by the Free Software Foundation;              #
#   version 2.1 of the License.                                        #
#                                                                      #
#   This library is distributed in the hope that it will be useful,    #
#   but WITHOUT ANY WARRANTY; without even the implied warranty of     #
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      #
#   GNU Lesser General Public License at                               #
#   http://www.gnu.org/licenses/lgpl-2.1.html for more details         #
#                                                                      #
#   Author: Werner Fink <werner@suse.de>                               #
#                                                                      #
########################################################################

#
# Byte ranges for Shift-JIS encoding (hexadecimal):
# First byte:   81-9F, E0-EF
# Second byte:  40-7E, 80-FC
#
# Now test out some multi byte characters which
# include 7bit aka ASCII bytes with 0x81 0x{40-7E}
#

typeset -i chr=0
typeset -i err=0
typeset printf=$(type -p printf 2>/dev/null)

unset LC_ALL
unset LC_CTYPE
export LANG=ja_JP.SJIS

for second in $(seq 64 126); do
    let chr++
    second=$(printf '%x' ${second})
    mbchar="$(printf "\x81\x${second}")"
    if test -z "${mbchar}" ; then
	let err++		# ERROR in builtin printf
	continue
    fi
    if test -x "${printf}" ; then
	if test $(${printf} "\x81\x${second}") != ${mbchar} ; then
	    let err++	# ERROR in builtin printf
	    continue
	fi
    fi
    uq=$(echo ${mbchar})
    dq=$(echo "${mbchar}")
    test "$uq" != "$dq" && let err+=1
    test ${#uq} -ne 1 -o ${#dq} -ne 1 && let err+=1
done

if test $err -ne 0 ; then
    : err_exit
    : err_exit
    print -u2 -n "\t"
    print -u2 -r ${0##*/}[$LINENO]: "Shift-JIS encoding failed"
fi
exit $err
