:       '(c) Copyright  Empress Software Inc.   1983, 2001'

if test "$EMPRESSPATH" = ""
then
	echo "EMPRESSPATH not set"
	exit 1

elif test ! -d $EMPRESSPATH
then
	echo "invalid EMPRESSPATH"
	exit 1
fi

. $EMPRESSPATH/config/options

case $MSSYS_PLATFORM in
 SYS_OS_WIN32)
	SED=sed
	;;
 *)
	SED=$EMPRESSPATH/common/gnu/empsed
	;;
esac

prtusage ()
{
	cat <<EOM
*** Usage ***	sedsjis -f sed_jp_euc_script [src_file]

sed_jp_euc_script is a sed script which contains jp_euc character.
This shell script convert the jp_euc character to sjis and then
run sed with it.

e.g.	sedsjis -f sed_script < testout.raw > testout

If this is not a jp_sjis port and MSLANG is not set to japanese,
this shell script will just do the normal sed.

EOM
}

case $# in
  2 | 3)
	if test "$1" != "-f"
	then
		prtusage
		exit 1
	fi
	SEDFILE=$2
	SRCFILE=$3
	;;

  *)	prtusage
	exit 1
	;;
esac

X=`empvers -langid`

if test "$X" = "jp_sjis"
then
	case "$MSLANG" in
	   jap*)
		case "$MSSYS_PLATFORM" in
		 SYS_OS_UNIX_HPUX*)
			LANG=ja_JP.SJIS
			export LANG
			;;
		esac

		NKF=$EMPRESSPATH/common/gnu/nkf

		case "$MSSYS_PLATFORM" in
		  SYS_OS_WIN32)
			# win32 supports SJIS but the sed program
			# of MKS toolkit does not.

			SRCTMP=`$EMPRESSPATH/common/sys_bin/tmpdir`/sedtmp.$$
			TMPFILE=`$EMPRESSPATH/common/sys_bin/tmpdir`/sedsjis.$$

			# convert the SJIS source file to EUC
			cat $SRCFILE | $NKF -S -e > $SRCTMP

			# run the EUC sed script
			sed -f $SEDFILE $SRCTMP > $TMPFILE

			# convert the output back to SJIS
			$NKF -E -s < $TMPFILE
			rm -fr $TMPFILE $SRCTMP
			exit 0
			;;
		esac

		TMPFILE=`$EMPRESSPATH/common/sys_bin/tmpdir`/sedsjis.$$
		$NKF -E -s < $SEDFILE > $TMPFILE
		$SED -f $TMPFILE $SRCFILE
		rm -fr $TMPFILE
		exit 0
		;;
	esac
fi

$SED -f $SEDFILE $SRCFILE
