#!/usr/local/bin/perl

# S.Sano 1999/11 to make Internet Week Presentation Sheets

$TabStop=2;

$R='\fR';
$H='\fH';
$B='\fB';
$I='\fI';
$CW='\f(CW';
$P='\fP';
$Unit='m';

# $PS=20
# $VS=23;

$PS=18;
$VS=20;

print <<'EOF' ;
'\" 'so /usr/lib/tmac/tmac.N
.ds fN TEST
.PH "'\fH\s12\&1999.12.16 Internet Week 99\s0''\s12\fH\\\\*(fN\fP\fH \- \\\\nP\fP\s0'"
.PF "'\fH\s10Secure Programming\s0\fP''\fH\s10Susumu Sano\fP\s0"
'\" ------------------------------------------------------------------------
'\" ------------------------------------------------------------------------
.de HL
.sp -0.8
.ps 60
\D'l 6i 0'
.ps
..
.de H1
.SK
.in 0
.HL
\\&\s+2\\$1\s0
.HL
.SP 0.5
..
.de H2
.SK
.in 0
\\&\s+2\\$1\s0
.HL
.SP 0.5
..
EOF

print ".S $PS $VS\n";

foreach $file (@ARGV) {
    if (-r $file ) {
        print STDERR "$file:\n";

	$file =~ /^[0-9]*([\w]*)(\..*)?/;
	$title = uc($1);
	
	print ".ds fN $title\n";
	print ".nr Ps 1\n";


	open IN, "nkf -e $file | expand -$TabStop|";
	&doit;
	close IN;
    } else {
        printf STDERR "$file: cannot open\n";
    }
}

sub iskanji {
    return ord($_[0]) >= 0x80;
}

sub text0 {
    my ($font, $kfont, $src) = @_;

    $src =~ s/\[\[/$CW/g;
    $src =~ s/\]\]/$P/g;

    @chs = split(//, $src);
    $kanji = -1;


    foreach $ch (@chs) {
	if (iskanji($ch) && $kanji != 1) {
	    $kanji = 1;
	    print $kfont;
	} elsif (!iskanji($ch) && $kanji != 0) {
	    $kanji = 0;
	    print $font;
	}
	print $ch;
    }
    # print $font if ($kanji);
}

sub text {
    &text0('\fH', '\fB', $_[0]);
}

sub textProg {

    &text0('\f(CW', '\fR', $_[0]);
}

sub doit {

    while (<IN>) {

	chomp;

	if (/^\./) {
	    print "$_\n";
	    next;
	}

	if (/^=(.*)/) {
	    print "$1\n";
	    next;
	}

	s/\\/\\\\/g;

	if (/^@@ *(.*)$/) {
	    print ".S $PS $VS\n";
	    print ".H1 \"";
	    &text($1);
	    print "\"\n.br\n";
	    $spaces = $small = 0;
	    next;
	} 

	if (/^@ *(.*)$/) {
	    print ".S $PS $VS\n";
	    print ".H2 \"";
	    &text($1);
	    print "\"\n.br\n";
	    $spaces = $small = 0;
	    next;
	} 

	if (/^ *$/) {
	    $spaces++;
	    next;
	}

	if (/^(\[+) *$/) {
	    $small += length($1) * 2;
	    print ".vs -$small\n";
	    next;
	}
	if (/^(\]+) *$/) {
	    $small -= length($1) * 2;
	    print ".vs +$small\n";
	    next;
	}

	if ($spaces > 0) {
	    print ".sp ".0.5*$spaces."\n";
	    $spaces = 0;
	}

	$indchar = "";
	$prog = 0;

	if (/^( *)([0-9]+\)) *(.*)/) {
	    $indchar = $2;
	    $pre = $1;
	    $post = $3;
	} elsif (/^( *)-> *(.*)/) {
	    $indchar = '\(rh';
	    $pre = $1;
	    $post = $2;
	} 
	elsif (/^( *)<- *(.*)/) {
	    $indchar = '\(lh';
	    $pre = $1;
	    $post = $2;
	} 
	elsif (/^( *)([-+*]) *(.*)/) {

	    if ($2 eq '-')    { $indchar = "-"; }
	    elsif ($2 eq '+') { $indchar = "-"; }
	    elsif ($2 eq '*') { $indchar = '\(bu';}
	    
	    $pre = $1;
	    $post = $3;

	} else {
	    ($pre, $mid, $post) = /^( *)(\. *)?(.*)/;
	    $prog = !&iskanji($post) && ($mid eq "");
	}

	$in = length($pre) - $TabStop;

	$in = 0 if ($in < 0);

	if ($indchar) {
	    print ".in $in$Unit\n";
	    print "$H$indchar";
	    print "\n.sp -1\n";
	    $in += 1;
	}
	print ".in $in$Unit\n";

	print "\\s-". $small*2 if ($small);
	
	if ($prog) {
	    textProg($post);
	} else {
	    text($post);
	}
	print "\\s0"  if ($small);

	print "\n.br\n";
    }
}









