#!/bin/sh
# Copyright 1992 NEC Corporation, Tokyo, Japan.
#
# Permission to use, copy, modify, distribute and sell this software
# and its documentation for any purpose is hereby granted without
# fee, provided that the above copyright notice appear in all copies
# and that both that copyright notice and this permission notice
# appear in supporting documentation, and that the name of NEC
# Corporation not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior
# permission.  NEC Corporation makes no representations about the
# suitability of this software for any purpose.  It is provided 
# "as is" without express or implied warranty.
#
# NEC CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN 
# NO EVENT SHALL NEC CORPORATION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF 
# USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR 
# OTHER TORTUOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 
# PERFORMANCE OF THIS SOFTWARE. 

# $Id: mkbindic.cpp,v 1.6 2003/08/24 09:40:27 aida_s Exp $







































































































































































































































































































PATH=/usr/local/bin:/bin:/usr/bin:/etc:/usr/etc:/usr/5bin:$PATH;


export PATH;
text_file=;
dic_name=;
cpp_text=;
spl_text=;
bck_text=;
flag=;
compat_flag=;
sortcmd="sort -d -s +0 -1"
usage="usage: mkbindic [-m|-s] [-name dicname] [-c version] textfile [cpp-args ...]";
: ${TMPDIR:=/tmp}


{
    while [ $# -gt 0 ]; do {
        case $1 in
	-m)
	    if [ -z "$flag" ]; then {
		flag="-m";
	    } else {
		echo "$usage";
		exit 1;
	    } fi;
	    ;;
	-s)
	    if [ -z "$flag" ]; then {
		flag="-s";
	    } else {
		echo "$usage";
		exit 1;
	    } fi;
	    ;;
	-name)
    	    shift;
	    if [ -z "$dic_name" ]; then {
		dic_name=$1;
	    } else {
		echo "$usage";
		exit 1;
	    } fi;
	    ;;
	-c)
    	    shift;
	    if [ -z "$compat_flag" ]; then {
		compat_flag="-c $1";
	    } else {
		echo "$usage";
		exit 1;
	    } fi;
	    ;;
	*)
	    if [ -z "$text_file" ]; then {
		text_file=$1;
	    } else {
		args="$args $1";
	    } fi;
	    ;;
	esac;
	shift;
    }; done

    if [ -z "$text_file" ]; then
	echo "$usage";
	exit 1;
    fi;
    if [ ! -r $text_file ]; then 
	echo "$usage";
	exit 1;
    fi;
    if [ -d $text_file ]; then 
	echo "$usage";
	exit 1;
    fi;
    if [ "$dic_name" != "" ]; then
	dic_ck="`echo $dic_name | 		    awk -F. '{

			printf("%s", $NF)
		    }'`";
	case "$dic_ck" in
	    d) fqsuff="fq" ;;
	    cbd) fqsuff="cld" ;;
	    *)
	    echo "Invalid name : $dic_name"
            exit 1 ;;
	esac
        dic_ckn="`echo $dic_name | 		awk -F/ '{print $NF}' | 		awk -F. '{print NF}'`";


        if [ $dic_ckn -ne 2 ]; then
	    echo "Invalid name : $dic_name";
            exit 1;
        fi;
        dic_ck="`echo $dic_name | 		awk -F/ '{print $NF}' | 		awk -F. '{


			printf("%s", $(NF-1))
                }'`";
        if [ "$dic_ck" =  "" ]; then
	    echo "Invalid name : $dic_name";
            exit 1;
        fi
    fi;

    if [ -z "$flag" ]; then
	flag="-m";
    fi;

    cpp_text="`echo $text_file | 		awk -F/ '{print $NF}' | 		awk -F. '{


		    for(i = 1; i < NF; i++)
			printf("%s.", $i)
		}'`";
    if [ -z "$cpp_text" ]; then
	cpp_text="`echo $text_file | 		awk -F/ '{print $NF}'`".;

    fi;
    cpp_text=$TMPDIR/"$cpp_text"cpp;

    spl_text="`echo $text_file | 		awk -F/ '{print $NF}' | 		awk -F. '{


		    for(i = 1; i < NF; i++)
			printf("%s.", $i)
		}'`";
    if [ -z "$spl_text" ]; then
	spl_text="`echo $text_file | 		awk -F/ '{print $NF}'`".;

    fi;
    spl_text=$TMPDIR/"$spl_text"spl;

    bck_text="`echo $text_file | 		awk -F/ '{print $NF}' | 		awk -F. '{


		    for(i = 1; i < NF; i++)
			printf("%s.", $i)
		}'`";
    if [ -z "$bck_text" ]; then
	bck_text="$text_file".;
    fi;
    bck_text="$bck_text"bk;

    out="`echo $text_file | 		awk -F/ '{print $NF}' | 		awk -F. '{


		    for(i = 1; i < NF; i++)
			printf("%s.", $i)
		}'`";
    if [ -z "$out" ]; then
	out="`echo $text_file | 		awk -F/ '{print $NF}'`".;

    fi



    out="$out"cbd;

    if [ -z "$dic_name" ]; then
	dic_name=$out;
    fi

    child="`echo $text_file | 		awk -F/ '{print $NF}' | 		awk -F. '{


		    for(i = 1; i < NF; i++)
			printf("%s.", $i)
		}'`";
    if [ -z "$child" ]; then
	child="`echo $text_file | 		awk -F/ '{print $NF}'`".;

    fi
    if [ "OPT$child" = "OPT." ]; then
	  echo "Invalid name : $text_file";
	  exit 1;
    fi

    toplen="`echo $child |awk '{printf("%d", index($1, "."))}'`"
    arglen="`echo $child |awk '{printf("%d", length($1))}'`"

    if [ $toplen -ne $arglen ]; then
        echo "Invalid name : $text_file";
        exit 1;
    fi

    if [ "x$fqsuff" != "x" ]; then
	fqoutopt="-o $child$fqsuff"
    fi
    if [ "OPT$flag" = "OPT-m" ]; then
	child="$child"mwd;
    else
	child="$child"swd;
    fi

    trap "rm -f $cpp_text $spl_text; exit 1;" 2;
    if echo cpptest | /usr/bin/cpp -traditional $args >/dev/null 2>&1; then
        echo "forcpp -7 < $text_file |" /usr/bin/cpp -traditional "$args | forcpp -8 > $cpp_text";
        forcpp -7 < $text_file | /usr/bin/cpp -traditional $args | forcpp -8 > $cpp_text;
    else
	echo "Cannot use cpp !!"
        echo "cp  $text_file $cpp_text";
        cp $text_file  $cpp_text;
    fi
    if [ $? != 0 ]; then
	echo "mkbindic: fatal error. exit";
	rm -f $cpp_text $spl_text;
	exit 1;
    fi
    echo "splitword $cpp_text > $spl_text";
    splitword $cpp_text > $spl_text;
    if [ $? != 0 ]; then
	echo "mkbindic: fatal error. exit";
	rm -f $cpp_text $spl_text;
	exit 1;
    fi;
    echo "mv $text_file $bck_text";
    mv $text_file $bck_text;
    echo "forsort -7 < $spl_text | $sortcmd | forsort -8 | mergeword -X > $text_file";
    forsort -7 < $spl_text | $sortcmd | forsort -8 | mergeword -X > $text_file;
    if [ $? != 0 ]; then
        mv $bck_text $text_file;
	echo "mkbindic: fatal error. exit";
	rm -f $cpp_text $spl_text;
	exit 1;
    fi;




    echo "crxdic $flag $compat_flag -o $dic_name $text_file";

    crxdic $flag $compat_flag -o $dic_name $text_file;
    if [ $? != 0 ]; then
        mv $bck_text $text_file;
	echo "mkbindic: fatal error. exit";
	rm -f $cpp_text $spl_text;
	exit 1;
    fi;
    echo "crfreq -div 512 $fqoutopt $dic_name $child";
    crfreq -div 512 $fqoutopt $dic_name $child;
    if [ $? != 0 ]; then
        mv $bck_text $text_file;
	echo "mkbindic: fatal error. exit";
	rm -f $cpp_text $spl_text;
	exit 1;
    fi;
    mv $bck_text $text_file;
    echo "rm $cpp_text $spl_text";
    rm -f $cpp_text $spl_text;
    exit $?;
}
