#!/usr/bin/perl -w
#---------#---------#---------#---------#---------#---------#---------#---------
#
# title
#   write STDIN to a file
# author
#   idc@planetlarg.net 20 April 2001
# description
#   Write everything this script receives (piped input) into a file.
#   The filename is created within this program.
#   It is made unique by adding the date, so if you are planning on running
#   this script more than once a day, change the file name subroutine or earlier
#   reports will be over-written.
# modifications
#
#---------#---------#---------#---------#---------#---------#---------#---------
# setup
   use 5.005;
   use strict;
# command line options
   # read any options entered with this program.
   &usage if $#ARGV != -1;

#---------#---------#---------#---------#---------#---------#---------#---------
# main
#   The main program goes here.

   my $dir     = '/export/home/fusion/logs/missing-files';
   my $file    = &file_name;

   open (OUT, ">$dir/$file") or die "can't open $file: $!";
   flock (OUT, 2) or die "can't lock file $dir/$file: $!"; # 2 is exlusive lock
   while (<STDIN>) {
      print OUT;
   }
   flock (OUT, 8) or die "can't unlock file $dir/$file: $!"; # 8 is unlock
   close OUT or die "can't close $file: $!";


#---------#---------#---------#---------#---------#---------#---------#---------
# subs
#   all subroutines go below here.

# subroutine
#  date
# description
#  take a time in seconds since Jan 01 1970, called the epoch for
#  some stoner reason.
#  translate it to a load of human-readable date-related values
#  and shove these values into a hash.
#  This is almost the same as using the standard module Time::localtime
#
sub date {
   my $unix_secs     = shift; # integer eg.987516249
   my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst)
                     = ( localtime ($unix_secs) );
   my $mon_abbr      = qw (Jan Feb Mar Apr May Jun Jul Aug
               Sep Oct Nov Dec)[$mon];
   my $day_abbr      = qw (Sun Mon Tue Wed Thu Fri Sat)[$wday];
   # translate from struct tm rubbish to human
   $mon++;                    # month number starts funny (from 0 to 11)
   $mon              = "0$mon"  if ($mon  < 10); # month is now always 2 digits
   $mday             = "0$mday" if ($mday < 10); # and month day is 2 digits
   $year             += 1900; # year is now cool for Y2K
   my %date_hash     = (
               'sec'       => $sec,
               'min'       => $min,
               'hour'      => $hour,
               'mday'      => $mday,
               'mon'       => $mon,
               'mon_abbr'  => $mon_abbr,
               'year'      => $year,
               'wday'      => $wday,
               'day_abbr'  => $day_abbr,
               'yday'      => $yday,
               'isdst'     => $isdst,
   );
   return %date_hash;
}


# subroutine
#   file_name
# description
#   create a unique filename.
#
sub file_name {

   my %date_hash = &date (time);
   my $name =
               'missing-file-report-'.
               $date_hash{year}.
               $date_hash{mon}.
               $date_hash{mday}.
               '.txt'
   ;
   return $name;
}


# subroutine
#   usage
# description
#   describe how this script should be used.
#   This is one way of producing a set of instructions,
#   using simple indentation.
#   For more complex document, use mark-up. See the perldoc section below.
#
sub usage {

   use Text::Wrap;
   $Text::Wrap::columns = 60;
   my $pre1             = "   ";
   my $pre2             = "   ";
   my $name             = "$0 - write piped input to a file";
   my $synopsis         = "DATA | $0  ";
   my $description      = "
Write everything this script receives into a file.
Edit this script to specify the filename and its path.
";
   my $examples         = "
This command
${pre1}echo 'hello!' | $0
produces a file
${pre1}/directory/file-with-datestamp-20010425.txt
containing the characters
${pre1}hello!
";

   print
               "NAME \n",
               wrap ($pre1, $pre2, $name), "\n",
               "SYNOPSIS \n",
               wrap ($pre1, $pre2, $synopsis), "\n",
               "DESCRIPTION \n",
               fill ($pre1, $pre2, $description), "\n",
               "EXAMPLES \n",
               wrap ($pre1, $pre2, $examples), "\n",
   ;
   exit 1;
}
#---------#---------#---------#---------#---------#---------#---------#---------


__END__

documentation

The "__END__" string is a token. It is the logical end of program text.
You can put anything you want after this token, such as an
instruction manual page; it will be ignored by the compiler.


