package mycommon;
# $Revision: 1.9 $ $Date: 2004/01/12 06:32:37 $
# $Id: newshscript,v 1.9 2004/01/12 06:32:37 tconnors Exp $
# $Header: /home/office/tconnors/cvsroot/bin/newshscript,v 1.9 2004/01/12 06:32:37 tconnors Exp $
# $RCSfile: newshscript,v $

use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION);

use Exporter;
$VERSION=1.00;
@ISA= qw(Exporter);

@EXPORT    = qw (zcat min max);
@EXPORT_OK = qw (zcat min max);
%EXPORT_TAGS = ();

sub zcat {
#call in list context if want the result; call in scalar context if
#want to find out whether the file is a compressed file
  my (@files) = (@_);
  my (@res,$file);
  my ($strip,$noact);
  if ($files[0] eq "--strip") {
    $strip=shift @files;
  }
  $noact = !wantarray;
  local *FHI;
  foreach $file (@files) {
    my $type=`file "$file"`;
    $type =~ /gzip compressed data/ && 
      (open(FHI, "gzip -dc \"$file\"|") or die "Can't open gzip -dc \"$file\" for read") ||
    $type =~ /bzip2 compressed data/ && 
      (open(FHI, "bzip2 -dc \"$file\"|") or die "Can't open gzip -dc \"$file\" for read") ||
    do {
      if ($noact) {
        return undef;  #is not compressed
      }
      (open(FHI, "$file") or die "Can't open $file for read");
    };
    if ($noact) {
      return *FHI;    #is compressed
    }
    while (<FHI>) {
      defined($strip) && chomp;
      push @res, $_;
    }
    close FHI;
  }
  return @res;
}
sub min {
  my ($min,$i);
  $min=(@_)[0];
  for $i (@_) {
    if ($i < $min) {
      $min=$i;
    }
  }
  return $min;
}

sub max {
  my ($max,$i);
  $max=(@_)[0];
  for $i (@_) {
    if ($i > $max) {
      $max=$i;
    }
  }
  return $max;
}

1;
