#!/usr/bin/perl
##!/usr/bin/jperl -Llatin

# Copyright (C) 1993-94 Noboru Ikuta <noboru@ikuta.ichihara.chiba.jp>
#
# wmime: MIME-header encoder Ver.1.11a
#   $B%a%C%;!<%8%X%C%@Cf!J@hF,$+$i:G=i$N6u9T$^$G!K$K4^$^$l$k4A;z$NItJ,$r(B
#   ISO-2022-JP(7bit JIS)$B$KJQ49$7(B MIME $B%(%s%3!<%I$9$k!#(B
#   $B$^$?!"(BMime-Version: $B$H(BContent-Type: $B$N#2$D$N%U%#!<%k%I$,%X%C%@It$K(B
#   $B4^$^$l$J$1$l$P%a%C%;!<%8%X%C%@$N:G8e$K0J2<$NBP1~$9$k%U%#!<%k%I$rDI(B
#   $B2C$9$k!#!J(B$add_headers $B$,(B 1 $B$N$H$-!K(B

$mime_ver = "Mime-Version: 1.0\n";
$con_type = "Content-Type: text/plain; charset=iso-2022-jp\n";
$add_headers = 0;

# $B;H$$J}(B : [ perl ] wmime [ $B%U%!%$%kL>(B ...]
#
#         mimew.pl $B$O!"4D6-JQ?t(B HOME$B"*(Bwmime$B$HF1$8"*DL>o$N(B@INC $B$N=g$K%5(B
#         $B!<%A$5$l$^$9!#(B
#
# $BCm0U(B : wmime$B$r(Bjperl$B!J$N(B2$B%P%$%HJ8;zBP1~%b!<%I!K$G;HMQ$9$k$H!"(BSJIS$B$H(BEUC
#        $B$r$&$^$/(B7bit JIS(ISO-2022-JP)$B$KJQ49$G$-$^$;$s!#(B
#        $BF~NO$K4^$^$l$kJ8;z$,(B7bit JIS(ISO-2022-JP)$B$H(BASCII$B$N$_$G$"$k$3$H(B
#        $B$,J]>Z$5$l$F$$$k>l9g$r=|$-!"I,$:(Boriginal$B$N1Q8lHG$N(Bperl$B!J$^$?$O(B
#        jperl1.4$B0J>e$r(B -Llatin $B%*%W%7%g%sIU$-!K$GF0$+$7$F$/$@$5$$!#(B
#
# $BG[I[>r7o(B : $BCx:n8"$OJ|4~$7$^$;$s$,!"G[I[!&2~JQ$O<+M3$H$7$^$9!#(B

$home = $ENV{'HOME'};
$perl_dir = $0;
$perl_dir =~ s/\\/\//g;
substr($perl_dir, rindex($perl_dir, "/")) = '';
unshift(@INC, $home, $perl_dir);
require 'mimew.pl';

## `MIME-Version:'$B$H(B`Content-Type:' $B$K%^%C%A$9$k%Q%?!<%s(B
$match_mime_ver = '^mime-version:';
$match_con_type = '^content-type:';

## $B%a%C%;!<%8%X%C%@$N=hM}(B
$/ = "\n\n";
$* = 1;
$_ = <>;
$_ = &mimeencode($_);

if (/\n\n$/){
    chop; print;
    if ($add_headers){
        if (!/$match_mime_ver/io){
            print $mime_ver;
        }
        if (!/$match_con_type/io){
            print $con_type;
        }
    }
    print "\n";

## $B%a%C%;!<%8%\%G%#$N=hM}(B
    $/ = "\n";
    while (<>){
        print;
    }
}else{
    print if ($_);
}
