#!/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 5.9 1996/10/28 00:22:30 kon Exp $

























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


export PATH;
text_file=;
dic_name=;
cpp_text=;
spl_text=;
bck_text=;
flag=;


{
    while [ $# -gt 0 ]; do {
        case $1 in
	-m)
	    if [ -z "$flag" ]; then {
		flag="-m";
	    } else {
		echo "usage: mkbindic [-m|-s] [-name dicname] textfile [cpp-args ...]";
		exit 1;
	    } fi;
	    ;;
	-s)
	    if [ -z "$flag" ]; then {
		flag="-s";
	    } else {
		echo "usage: mkbindic [-m|-s] [-name dicname] textfile [cpp-args ...]";
		exit 1;
	    } fi;
	    ;;
	-name)
    	    shift;
	    if [ -z "$dic_name" ]; then {
		dic_name=$1;
	    } else {
		echo "usage: mkbindic [-m|-s] [-name dicname] textfile [cpp-args ...]";
		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: mkbindic [-m|-s] [-name dicname] textfile [cpp-args ...]";
	exit 1;
    fi;
    if [ ! -r $text_file ]; then 
	echo "mkbindic: cannot open $text_file";
	exit 1;
    fi;
    if [ -d $text_file ]; then 
	echo "mkbindic: cannot open $text_file";
	exit 1;
    fi;
    if [ "$dic_name" != "" ]; then
	dic_ck="`echo $dic_name | 		    awk -F. '{
			printf("%s", $NF)

		    }'`";
        if [ "$dic_ck" != "d" -a "$dic_ck" != "cbd" ]; then
	    echo "Invalid name : $dic_name";
            exit 1;
        fi;
        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=/tmp/"$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=/tmp/"$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 [ "OPT$flag" = "OPT-m" ]; then
	child="$child"mwd;
    else
	child="$child"swd;
    fi

    trap "rm -f $cpp_text $spl_text; exit 1;" 2;
    if [ -x cpp ]; then
        echo "forcpp -7 < $text_file |" cpp "$args | forcpp -8 > $cpp_text";
        forcpp -7 < $text_file | cpp $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 | sort -d | forsort -8 | mergeword -X > $text_file";
    forsort -7 < $spl_text | sort -d | 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 -o $dic_name $text_file";

    crxdic $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 $dic_name $child";
    crfreq $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 $?;
}
