#!/usr/bin/perl
# $Id: jpick,v 1.4 2017/10/18 06:33:29 nomura Exp nomura $
# Change charset of Japanese search string for nmh/pick

use Encode 'from_to';

@NEW_ARGV = ("-sequence", "s", "last:200");	# Set defaults here.

while ($_ = shift(@ARGV)) {
    if (/^-su/) {
	push(@NEW_ARGV, $_);
	$_ = shift(@ARGV);
	/[\x80-\xff]/ and from_to($_, "euc-jp", "utf8");
    } elsif (/^-sea/ && @ARGV[0] =~ /[\x80-\xff]/) {	# -search Japanese
	$_ = shift(@ARGV);
	push(@NEW_ARGV, "-lbrace", "-search");
	from_to($_, "euc-jp", "utf8");
	push(@NEW_ARGV, $_, "-or", "-search");
	from_to($_, "utf8", "7bit-jis");
	s/^\x1b\$B//;	# gets rid of shift-in/out
	s/\x1b\(B$//;
	s/\\/\\\\/g;
	s/\[/\\[/g;
	push(@NEW_ARGV, $_, "-rbrace");
	next;
    } elsif ($_ eq "-whom") {	# the original command
	$_ = shift(@ARGV);
	push(@NEW_ARGV, "-lbrace", "-from", $_, "-or", "-to", $_, "-or",
	     "-cc", $_, "-rbrace");
	next;
    }
    push(@NEW_ARGV, $_);
}

exec("/usr/bin/pick", @NEW_ARGV);