#/usr/bin/perl

# Usage: wrapman [files]

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
    = localtime(time);
$month = (January,February,March,April,May,June,
    July,August,September,October,November,December)[$mon];

$user = $ENV{'USER'} || $ENV{'LOGNAME'} || (getpwuid($<))[0];
$fullname = (getpwnam($user))[6];
$fullname =~ s/.*-\s*(.*)\(.*//;
$fullname =~ s/,.*//;

substr($user,0,1) =~ tr/a-z/A-Z/;
$fullname =~ s/&/$user/;        # Propagate the & abomination.

$log = '$' . 'Log' . '$';
$header = '$' . 'Header' . '$';

foreach $file (@ARGV) {

    # Generate various strings for the manual page.

    ($prog = $file) =~ s/\.\w+$//;
    ($PROG = $prog) =~ y/a-z/A-Z/;
    $Prog = $prog; substr($Prog,0,1) =~ y/a-z/A-Z/;

    # See if we really want to wrap this file.

    open(IN,$file) || next;
    $/ = "\n";
    $line1 = <IN>;
    next unless $line1 =~ /perl/;
    $line1 .= <IN> if $line1 =~ /eval/;
    $line1 .= <IN> if $line1 =~ /argv/;
    $line2 = <IN>;
    next if $line2 eq "'di';\n";

    # Pull the old switcheroo.

    ($dev,$ino,$mode) = stat IN;
    print STDERR "Wrapping $file\n";
    rename($file,"$file.bak");
    open(OUT,">$file");
    chmod $mode, $file;

    # Spit out the new script.

    print OUT $line1;
    print OUT <<EOF;
'di';
'ig00';
#
# $header
#
# $log
EOF

    # Copy entire script.

    undef $/;
    $_ = <IN>;
    print OUT $line2, $_;

    # Now put the transition from Perl to nroff.
    #   (We prefix the .00 below with $null in case the wrapman
    #   program is itself wrapped.)

    print OUT <<EOF;
###############################################################

    # These next few lines are legal in both Perl and nroff.

$null.00;                       # finish .ig
 
'di           \\" finish diversion--previous line must be blank
.nr nl 0-1    \\" fake up transition to first page again
.nr % 0         \\" start at page 1
'; __END__ ##### From here on it's a standard manual page #####

.TH $PROG 1 "$month $mday, 19$year"
.AT 3
.SH NAME
$prog \\- whatever
.SH SYNOPSIS
.B $prog [options] [files]
.SH DESCRIPTION
.I $Prog
does whatever.
.SH ENVIRONMENT
No environment variables are used.
.SH FILES
None.
.SH AUTHOR
$fullname
.SH "SEE ALSO"

.SH DIAGNOSTICS

.SH BUGS

EOF

    close IN;
    close OUT;
}