#!/usr/local/bin/perl
#======================================================================
#             VisIPS -> Tk-VisIPS ǡѴġ
#      Copyright (C) 1996 Takuya Nakayama <takuya-n@is.aist-nara.ac.jp>
#======================================================================

# visips(batch model)Υǡ --> tk-visips Υǡ
sub Conv_Batch2TkV {
    $word_num = 0;
    $ID = 0;

    while (<>) {
	chop;
	@field = split(' ');
	$_ = shift @field;
	{
	    /^\\w/ && do {	# \w ñλ
		$word_num = $field[0];
		last;
	    };
	    /^\\s/ && do {	# \s ʸλ
		# \w λñȼºݤʸñ
		# ۤʤ
		$word_num = $#field + 1 if ($#field + 1) > $word_num;
		
		print "size $word_num\n";
		print "setstr @field\n";
		last;
	    };
	    /^\\a/ && do {	# \a ƥǡ
		$pos = $field[0] - 1;
		$len = $field[1] - 1;
		$info = ""; $info_rec = 0;
		$dtrs_num = 0;
	      READ_CAT_DATA: while (<>) {
		    $str = $_;
		    @field = split(' ');
		    $_ = shift @field;
		    {
			/^\\n/ && do {
			    $cat_name = $field[0];
			    last;
			};
			/^\\c/ && do {
			    $i = 0;
			    @dtrs = @field;
			    $dtrs_num = $#dtrs + 1;
			    last;
			};
			/^\\d/ && do {
			    $str =~ s/^\\d\s+//;
			    $info = $str;
			    $info_rec = 1;
			    last;
			};
			/^\\e/ && do {
			    last READ_CAT_DATA;
			};
			# default
			if ($info_rec == 1) {
			    $info .= $str;
			}
		    }
		}
		print "add $ID $pos $len $cat_name\n";
		print $info;
		print "\\e\n";
		print "setdtr $ID ";
		if ($dtrs_num > 0) {
		    for ($i = $#dtrs; $i >= 0; $i--) {
			print $dtrs[$i], " ";
		    }
		    print "\n";
		} else {
		    $tmp = "";
		    for ($i = $pos; $i <= $len + $pos; $i++) {
			$tmp .= "i$i ";
		    }
		    print "$tmp\n";
		}
		$ID++;
	    };
	}
    }
    print "end\n";
}

# visips(server model)Υǡ --> tk-visips Υǡ
sub Conv_Serv2TkV {

    # ѥ졼 \0 
    $/ = "\0";

    $ID = 0;
    while (<>) {
	chop $_;
	if ($_ eq "w") {
	    $cat_pos = <>; chop $cat_pos; $cat_pos--;
	    $cat_len = <>; chop $cat_len; $cat_len--;
	    $cat_name = <>; chop $cat_name;
	    
	    $cat_pos2 = $cat_pos + $cat_len;
	    $post = "add $ID $cat_pos $cat_len $cat_name\n";

	    $ditail_info = <>; chop $ditail_info;
	    $post .= "$ditail_info\n\\e\n";
	    print $post;
	    
	    $child_num = <>; chop $child_num;
	    if ($child_num > 0) {
		$post = "\n";
		for ($i = 0; $i < $child_num; $i++) {
		    $tmp = <>; chop $tmp;
		    $post = "$tmp $post";
		}
		$post = "setdtr $ID " . $post;

		print $post;
	    } else {
		$post = "setdtr $ID ";
		for ($i = $cat_pos; $i <= $cat_pos2; $i++) {
		    $post .= "i$i ";
		}
		print "$post\n";
	    }
	    $ID++;
	} elsif ($_ eq "i") {
	    $display = <>; chop $display;

	    $words_number = <>; chop $words_number;
	    print "size $words_number\n";

	    print "setstr ";
	    for ($i = 0; $i < $words_number; $i++) {
		$word = <>; chop $word;
		print "$word ";
	    }
	    print "\n";
	    
	    $sentence_cat = <>; chop $sentence_cat;
	    $init_option = <>; chop $init_option;

	} elsif ($_ eq "f") {
	    print "end\n";
	    exit(0);
	}
    }
    print "end\n";
    exit(0);
}

$_ = shift;

if (/s/) {
    &Conv_Serv2TkV;
} else {
    &Conv_Batch2TkV;
}
