package CIS;

# $Id: CISsubs.pl,v 1.2 1994/11/04 22:57:49 bates Exp bates $

require 'look.pl';
require 'termcap.pl';
$maxlen = 75;
if ($main'tty) {
    $ospeed = 9600;
    &Tgetent('');                      # loads TERMCAP into %TC
    $maxlen = $Config{'maxlen'} if $Config{'maxlen'};
    $maxlen = $TC{'co'} - 3 if $TC{'co'};

    $bld = $TC{'md'};
    $bld = $TC{'mr'} unless $bld;
    $ubld = $TC{'me'};
    $do_bolding = $bld && $ubld && $main'pager ne "less";
}

if ($main'opt_a) {
    open(ABBREV, "< $main'opt_a") ||
        open (ABBREV, "< $main'Config{'ClientDir'}/$main'opt_a") || die
       "Cannot open abbreviation file $main'opt_a in current directory or" .
        "\nin directory $main'Config{'ClientDir'}: $!\n";
    $ab_is_open = 1;
} elsif ($main'Config{'AbbrevFile'}) {
    open(ABBREV, "< $main'Config{'ClientDir'}/$main'Config{'AbbrevFile'}")
|| die
       "Cannot open abbreviation file $main'Config{'AbbrevFile'} in
directory $main'Config{'ClientDir'}: $!\n";
    $ab_is_open = 1;
}

sub main'pretty {
    (*FH, $cite) = @_;
    &split_citation($cite);
    if ($main'opt_B) { &BibTeX_print; }
    elsif ($main'opt_T) {&TiB_print; }
    else { &default_print; }
}

sub split_citation {
    ($null,$field1,$title,$authors,$key_words,$search_auth,$add_search) =
       split('#', $_[0]);
    local($pub, $jour, $bk);
    warn "unusual field 1 in citation\n" unless length($field1) == 28;
    undef($publisher);
    undef($journal);
    undef($volume);
    undef($number);
    undef($book);
    $cite_ind = substr($field1,-1,1);
    if ($cite_ind =~ /[Bb]/) { # book
       ($year, $publisher, $number_of_pages) =
           unpack('A2 A16 x2 A5', $field1);
       $number_of_pages =~ s/^\s+//;
       $pub = &abbrev_expand($publisher);
       undef($publisher_address);
       if ($pub =~ /#/) {
           ($publisher, $publisher_address) = split('#', $pub);
       } else {
           $publisher = $pub;
       }
    } elsif ($cite_ind =~ /[Jj]/) { # article in journal or other periodical
       ($year, $journal, $volume, $number, $begin_page,
        $end_page, $special_page_mark) =
           unpack('A2 A8 A3 A3 A5 x A4 A1', $field1);
       $volume =~ s/^\s+//;
       $begin_page =~ s/^\s+//;
       $end_page =~ s/^\s+//;
       $number =~ s/\/\s*//;
       $jour = &abbrev_expand($journal);
       undef($journal_address);
       if ($jour =~ /#/) {
           ($journal, $journal_address) = split('#', $jour);
       } else {
           $journal = $jour;
       }
    } elsif ($cite_ind =~ /[Pp]/) { # article in proceedings or edited book
       ($year, $book, $volume, $begin_page, $end_page,
        $special_page_mark) =
           unpack('A2 A10 A4 A5 x A4 A1', $field1);
       $volume =~ s/[\(\)]//g;
       $volume =~ s/^\s+//;
       $bk = &abbrev_expand($book);
       undef($book_eds); undef($book_publisher);
       if ($bk =~ /#/) {
           ($book, $book_eds, $book_address) = split('#', $bk);
       } else {
           $book = $bk;
       }
    } elsif ($cite_ind =~ /C/) { # Computer readable material
       ($year, $publisher, $size, $unit) =
           unpack('A2 A16 x2 A4 A2', $field1);
    } elsif ($cite_ind =~ /[Ee]/) { # Electronic journal article
       ($year, $journal, $volume, $issue, $ident, $size, $ident_code) =
           unpack('A2 A8 A3 A3 A4 x A4 A1', $field1);
       if ($fullname{$journal}) {
           $journal = $fullname{$journal};
       }
    } elsif ($cite_ind =~ /Z/) { # Administrative records
       ($message) = substr($field1, 0, 26);
    }
    @authors = split(';', $authors);
    @key_words = split(';', $key_words);
    @search_auth = split(';', $search_auth);
    @add_search = split(';', $add_search);
}

sub default_print {
    for (@authors) { print FH "%AUTHOR   = ", $_, "\n"; }
    &fill_print("%TITLE    = ", $title, $bld, $ubld);
    if ($cite_ind =~ /[Bb]/) {
       &fill_print("%PUBLISH  = ", $publisher, '', '');
       print FH "%PAGES    = ", 0+$number_of_pages, "\n" if $number_of_pages;
    } elsif ($cite_ind =~ /[Jj]/) {
       &fill_print("%JOURNAL  = ", $journal, '', '');
       print FH "%VOLUME   = ", $volume, "\n" if $volume;
       print FH "%NUMBER   = ", $number, "\n" if $number;
       if ($begin_page) {
           print FH "%PAGES    = ", $begin_page;
           print FH "-", $end_page if $end_page;
           print FH "\n";
       }
    } elsif ($cite_ind =~ /[Pp]/) {
       &fill_print("%BOOK     = ", $book, '', '');
       print FH "%VOLUME   = ", $volume, "\n" if $volume;
       if ($begin_page) {
           print FH "%PAGES    = ", $begin_page;
           print FH "-", $end_page if $end_page;
           print FH "\n";
       }
    } elsif ($cite_ind =~ /C/) {
       &fill_print("%PUBLISH  = ", $publisher, '', '');
       print FH "%SIZE    = ", $size, $unit, "\n" if $size;
    } elsif ($cite_ind =~ /[Ee]/) {
       &fill_print("%JOURNAL  = ", $journal, '', '');
       print FH "%VOLUME   = ", $volume, "\n" if $volume;
       print FH "%ISSUE    = ", $issue, "\n" if $volume;
       print FH "%IDENT    = ", $ident, "\n" if $ident;
       print FH "%SIZE     = ", $size, "\n" if $size;
       print FH "%CODE     = ", $ident_code, "\n" if $ident_code;
    } elsif ($cite_ind =~ /Z/) {
       print FH "%MESSAGE  = ", $message, "\n" if $message;
    }
    print FH "%YEAR     = ", 1900+$year, "\n" if $year;
    &fill_print("%KEYWORDS = ", $key_words, '', '');
}

sub BibTeX_print {
    local($surname, $cite, @pieces, @names);
    @names = ();
    for (@authors) {
       @pieces = split(/,/);
       @pieces = split(/ /, shift(@pieces));
       ($surname = pop(@pieces)) =~ tr/A-Z/a-z/;
       push(@names, substr($surname, 0, 4));
    }
    $cite = join(":", @names, 1900 + $year);
    if ($cite_ind =~ /[Bb]/) {
       print FH "\@book{$cite,\n";
       &fill_print("    Publisher = ", "{".$publisher."},", '', '');
       print FH "    Pages = ", 0+$number_of_pages, ",\n" if $number_of_pages;
    } elsif ($cite_ind =~ /[Jj]/) {
       if ($title =~ /^Review of/ && $authors =~ /\(Rev/) { # review
           print FH "\@bookreview{$cite,\n";
       } else {
           print FH "\@article{$cite,\n";
       }
       &fill_print("    Journal = ", "{".$journal."},", '', '');
       print FH "    Volume = ", $volume, ",\n" if $volume;
       print FH "    Number = ", $number, ",\n" if $number;
       if ($begin_page) {
           print FH "    Pages = {", $begin_page;
           print FH "--", $end_page if $end_page;
           print FH "},\n";
       }
    } elsif ($cite_ind =~ /[Pp]/) {
       print FH "\@inproceedings{$cite,\n";
       &fill_print("    Booktitle  = ", "{".$book."}", '', '');
       print FH "    Volume = ", $volume, ",\n" if $volume;
       if ($begin_page) {
           print FH "    Pages = {", $begin_page;
           print FH "--", $end_page if $end_page;
           print FH "},\n";
       }
    } elsif ($cite_ind =~ /C/) {
       print FH "\@misc{$cite,\n";
       &fill_print("    HowPublished  = ", "{".$publisher."}",
                   '', '');
       print FH "    Note = ", "{", $size, $unit, "}\n" if $size;
    } elsif ($cite_ind =~ /[Ee]/) {
       print FH "\@article{$cite,\n";
       &fill_print("    Journal = ", "{".$journal."},", '', '');
       print FH "    Volume = ", $volume, ",\n" if $volume;
       print FH "    Issue = ", $issue, ",\n" if $issue;
       print FH "    Ident = ", "{", $ident, "},\n" if $ident;
       print FH "    Size = ", "{", $size, "},\n" if $size;
       print FH "    Code = ", "{", $ident_code, "},\n" if $ident_code;
    } elsif ($cite_ind =~ /Z/) {
       print FH "\@control{$cite,\n";
       print FH "    Message = ", "{", $message, "},\n" if $message;
    }
    &fill_print("    Keywords = ", "{".$key_words."},",
               '', '') if $key_words;
    &fill_print("    Author = ", "{" . join(" and ", @authors) ."},",
               '', '');
    &upcase_title;
    &fill_print("    Title = ", $title, '', '');
    print FH "    Year = ", 1900+$year, "\n" if $year;
    print FH "}\n\n";
}


sub TiB_print {
    for (@authors) { print FH "%A ", $_, "\n"; }
    &upcase_title_nobracket;
    &fill_print("%T ", $title, $bld, $ubld);
    if ($publisher) {
       &fill_print("%I ", $publisher, '', '');
       print FH "%P ", 0+$number_of_pages, "\n" if $number_of_pages;
    } else {
       &fill_print("%J ", $journal, '', '');
       print FH "%V ", $volume, "\n" if $volume;
       print FH "%N ", $number, "\n" if $number;
       if ($begin_page) {
           print FH "%P ", $begin_page;
           print FH "-", $end_page if $end_page;
           print FH "\n";
       }
    }
    print FH "%D ", 1900+$year, "\n" if $year;
    &fill_print("%K ", $key_words, '', '');
}

for ('a', 'and', 'of', 'the', 'for', 'to', 'on', 'in') { $nocap{$_}++; }

sub upcase_title {
    local(@words) = split(/ /,$title);
    local($first) = shift(@words);
    local($_);
    for (@words) {
       next if $nocap{$_};
       s/^([A-Z])/{$1}/;
       substr($_,0,1) =~ tr/a-z/A-z/;
    }
    $title = '{' . join(' ', $first, @words) . '},';
}

sub upcase_title_nobracket {
    local(@words) = split(/ /,$title);
    local($first) = shift(@words);
    local($_);
    for (@words) {
       next if $nocap{$_};
       s/^([A-Z])/{$1}/;
       substr($_,0,1) =~ tr/a-z/A-z/;
    }
    $title =  join(' ', $first, @words) ;
}

sub fill_print {
    local($header, $body, $bld, $ubld) = @_;
    local($line_len, $header_len, $bold_this, @body);
    $bold_this = $bld && $ubld && $do_bolding;
    $body =~ s/\; */\; /g;
    $body =~ s/  +/ /g;
    $body =~ s/ _/ /g;
    @body = split(' ', $body);
    return unless scalar(@body);
    $header_len = length($header);
    $_ = shift(@body);
    print FH $header;
    &Tputs($bld, 0, 'FH') if $bold_this;
    print FH $_;
    $line_len = $header_len + length;
    for (@body) {
       $new_len = 1 + $line_len + length;
       if ($new_len <= $maxlen) {
           $line_len = $new_len;
           print FH " ", $_;
       }
       else {
           print FH "\n";
           &Tputs($ubld, 0, 'FH') if $bold_this;
           print FH " " x $header_len;
           &Tputs($bld, 0, 'FH') if $bold_this;
           print FH $_;
           $line_len = $header_len + length;
       }
    }
    &Tputs($ubld, 0, 'FH') if $bold_this;
    print FH "\n";
}

sub abbrev_expand {
    local($xab);
    local($abbrev) = $_[0];
    $abbrev =~ s/^\s+//;
    $abbrev =~ s/\s+$//;
    return $fullname{$abbrev} if $fullname{$abbrev};
    if($ab_is_open) {
       &look(ABBREV, '#'.$abbrev, 0, 1);
       $line = <ABBREV>;
       chop($line);
       @abbrev_pieces = split('#', $line);
       shift(@abbrev_pieces);
       $xab = shift(@abbrev_pieces);
       $xab =~ s/\s+$//;
       if( $xab eq $abbrev ) {
           $fullname{$abbrev} = join('#', @abbrev_pieces);
       } else {
           $fullname{$abbrev} = $abbrev;
       }
    } else {
       $fullname{$abbrev} = $abbrev;
    }
    return $fullname{$abbrev};
}

1;
