#!/usr/local/bin/perl
#
# -------------------------------------------------------------------------
# TRINC (Technical Report Incorporation)
# by Alex Sherstinsky (shers@media.mit.edu).
#
# Last updated: December 7, 1994
#
# TRINC provides a mechanism by which the TR INDEX file in the
# ftp directory contains only the exported TRs from "/v/info/TechReports".
# Must be run on a machine that has access to the ftp directory
# (for example, whitechapel.media.mit.edu)
# -------------------------------------------------------------------------
#
# Define fairly-constants
#
$trdirindex = '/mas/disks/vismod/info/TechReports/INDEX';
$ftpdir = '/ftp/ftplink/ftp/pub/tech-reports';
$ftpdirindex = '/ftp/ftplink/ftp/pub/tech-reports/INDEX';
$ftpdirindexsave = '/ftp/ftplink/ftp/pub/tech-reports/INDEX.SAVE';
$medialab = 'MIT Media Laboratory';
$vismod = 'Vision and Modeling Technical Reports';
$trtitle = 'Technical Report:';
$publication = 'Also Available As:';
$trprefix = 'TR-';
$trsuffix = '.ps.Z';
$infopattern = '[\041-\176]';
#
$help = $ARGV[0];
$_ = $help;
$arglen = length;
if ($arglen == 0) {
    print <<EndOfHelp;
--------------------------------------------------------------------------------
    Syntax: trinc.pl export
    Typing trinc.pl by itself produces this help message.

    This script produces an updated "INDEX" file in
    "$ftpdir" for exported technical reports.
    The citation for each TR, previously placed into
    "$ftpdir", is incorporated into
    "$ftpdirindex" from
    "$trdirindex".

    Note that trinc.pl does not check the contents of TRs.

    TRs must obey the following convention.  Each TR file name in
    "$ftpdir" starts with "TR", followed by a dash
    ("-"), followed by the TR number, ending with ".ps.Z".  For example:
    "TR-301.ps.Z".

    A proper citation for each exported TR must already exist in
    "$trdirindex".  Proper citations have no
    blank lines.  The first string of the first line of the citation is the TR
    number.  For example:

    300  (109 pp)
    M-Lattice: A System For Signal Synthesis        Sc.D. Thesis, MIT dept. of
    And Processing Based On Reaction-Diffusion      Elec. Eng. and Comp. Sci.
    A. S. Sherstinsky                               Advisor:  R. W. Picard

    Proper citations in "$trdirindex" must be
    separated by blank lines without any characters (that means no spaces, no
    tabs, etc.), except the new line, the only character on the line.  It is
    the safest to not use any non-alpha-numeric characters anywhere at all.

    In addition, "$trdirindex" must have a line
    containing the string "MIT Media Laboratory", followed by a line containing
    the string "Vision and Modeling Technical Reports", followed by a line
    containing the strings "Technical Report:" and "Also Available As:" (in
    that order), followed by a blank line.  Citations above this pattern are
    ignored.

    If "$trdirindex" does not adhere to the
    above-described proper syntax, then
    "$ftpdirindex" will be corrupted.
--------------------------------------------------------------------------------
EndOfHelp
} else {
    if((-e $trdirindex) && (-r $trdirindex)) {
	if(open(indexfilehandle, $trdirindex)) {
	    @fullindex = <indexfilehandle>;
	    close(indexfilehandle);
	} else {
	    die "Cannot open \"$trdirindex\"!\n";
	}
    } else {
	print "Cannot access \"$trdirindex\"!\n";
	exit;
    }
    @revfullindex = reverse(@fullindex);
    for($_ = pop(@revfullindex), $has_amt = /$medialab/;
    (($has_amt eq '') && ($#revfullindex >= 0));
    $_ = pop(@revfullindex), $has_amt = /$medialab/) {
	;
    }
    if(($has_amt eq '') && ($#revfullindex == (-1))) {
	print "\"$trdirindex\" must contain \'$medialab\'.\n";
	exit;
    } else {
	$gotmedialab = $_;
    }
    for($_ = pop(@revfullindex), $has_vismod = /$vismod/;
    (($has_vismod eq '') && ($#revfullindex >= 0));
    $_ = pop(@revfullindex), $has_vismod = /$vismod/) {
	;
    }
    if(($has_vismod eq '') && ($#revfullindex == (-1))) {
	print "\"$trdirindex\" must contain \'$vismod\'.\n";
	exit;
    } else {
	$gotvismod = $_;
    }
    for($_ = pop(@revfullindex),
    $has_trtitle = /$trtitle/, $has_publication = /$publication/;
    ((($has_trtitle eq '') || ($has_publication eq '')) &&
    ($#revfullindex >= 0));
    $_ = pop(@revfullindex),
    $has_trtitle = /$trtitle/, $has_publication = /$publication/) {
	;
    }
    if((($has_trtitle eq '') || ($has_publication eq '')) &&
    ($#revfullindex == (-1))) {
	print "\"$trdirindex\" must contain \'$trtitle.'\t'.$publication\'.\n";
	exit;
    } else {
	$gotpublication = "\n".$_."\n";
    }
    for($_ = pop(@revfullindex), $hasinfo = /$infopattern/;
    (($hasinfo ne '') && ($#revfullindex >= 0));
    $_ = pop(@revfullindex), $hasinfo = /$infopattern/) {
	;
    }
    if(($hasinfo ne '') && ($#revfullindex == (-1))) {
	print "A valid TR citation must start with a blank line.\n";
	exit;
    }
    # Open ftp INDEX file
    if((-e $ftpdirindex) && (-r $ftpdirindex)) {
	$done_rename = rename($ftpdirindex, $ftpdirindexsave);
	if(!$done_rename) {
	    print "Cannot copy \"$ftpdirindex\" to \"$ftpdirindexsave\"!\n";
	    exit;
	}
    }
    open(ftpdirindexhandle, "> $ftpdirindex") ||
    die "Cannot open \"$ftpdirindex\"!\n";
    # Output heading
    print (ftpdirindexhandle "$gotmedialab");
    print (ftpdirindexhandle "$gotvismod");
    print (ftpdirindexhandle "$gotpublication");
    # Go through full list of citations
    for($index = 0; ($#revfullindex > 0); $index++) {
	# Get to TR citation (skip extra blank lines)
	for($_ = pop(@revfullindex), $hasinfo = /$infopattern/;
	(($hasinfo eq '') && ($#revfullindex >= 0));
	$_ = pop(@revfullindex), $hasinfo = /$infopattern/) {
	    ;
	}
	if(($index == 0) && ($#revfullindex == (-1))) {
	    print "No valid TR citations found in \"$trdirindex\".\n";
	    exit;
	}
	# Obtain TR number
	split;
	$trnum = @_[0];
	# Get citation (current citation is valid until next blank line)
	push(@citation, $_);
	for($_ = pop(@revfullindex), $hasinfo = /$infopattern/,
	push(@citation, $_); (($hasinfo ne '') && ($#revfullindex >= 0));
	$_ = pop(@revfullindex), $hasinfo = /$infopattern/,
	push(@citation, $_)) {
	    ;
	}
	# Construct TR file name
	$trfilename = $ftpdir.'/'.$trprefix.$trnum.$trsuffix;
	if(-e $trfilename) {
	    @citation = reverse(@citation);
	    for($_ = pop(@citation); ($#citation >= 0); $_ = pop(@citation)) {
		print(ftpdirindexhandle "$_");
	    }
	    print(ftpdirindexhandle "$_");
	} else {
	    for(pop(@citation); ($#citation >= 0); pop(@citation)) {
		;
	    }
	    pop(@citation);
	}
    }
    close(ftpdirindexhandle);
}
