#!/usr/pkg/bin/perl

=begin metadata

Name: split
Description: split a file into pieces
Author: Rich Lafferty, rich@alcor.concordia.ca
License: perl

=end metadata

=cut


#
#  split -- split a file into pieces
#
#        Rich Lafferty <rich@alcor.concordia.ca>
#        Sat Mar  6 22:27:28 EST 1999
#
#   Perl Power Tools -- http://language.perl.com/ppt/
#

use strict;
use Getopt::Std;
use File::Basename;

my $me = basename($0);
my $Fh;
my $curext;
my $curname;

## get_count expands byte/linecount 'k' and 'm' and checks sanity
sub get_count {
    my $count = shift;
    my %mult = ('k' => 1024, 'm' => 1024 * 1024);
    return if $count !~ m/\A([0-9]+)([KkMm]?)\Z/;
    my $n = $1;
    $n *= $mult{lc($2)} if $2;
    return $n;
}

# nextfile creates the next file for output, and returns the
# typeglob of the filehandle. This is the part to hack if your OS's
# filenames are broken (8.3, for example, or RISC/OS's `.' path
# separator.

sub nextfile {
    package
    	nextfile; # hide from PAUSE

    my $prefix = shift;

    if (!defined($Fh)) {
	$curext = "aaa";   # initialize on first call
    }
    else {
	close($Fh) or die "$me: Can't close $curname: $!\n";

	if ($curext eq "zzz") { die "$me: can only create 17576 files\n" }
    	else { $curext++ }
    }

    # MS-DOS: $curname = "$prefix." . $curext;
    $curname = $prefix . $curext;
    open($Fh, '>', $curname) or die "$me: Can't open $curname: $!\n";
    binmode $Fh;
    return $Fh;
}

## clue explains usage.
sub clue {

    print <<EOT;
usage: $me -b byte_count[k|m] [file [prefix]]
       $me -l line_count[k|m] [file [prefix]]
       $me -p regexp [file [prefix]]

Output fixed-size pieces of INPUT to prefixaaa, prefixabb, ...; default
prefix is 'x'.  With no file, or when file is -, read standard input.

SIZE may have a multiplier suffix: k for 1024, m for 1024^2.

EOT

exit 1;
}

#### Main program starts here. ####

getopts('b:l:p:', \my %opt) or clue;
my $nbytes;
if (defined $opt{'b'}) {
    $nbytes = get_count($opt{'b'}) or die qq($me: "$opt{b}" is invalid number of bytes\n);
}
my $nlines;
if (defined $opt{'l'}) {
    $nlines = get_count($opt{'l'}) or die qq($me: "$opt{l}" is invalid number of lines\n);
}
clue() if ($nbytes && $nlines);
clue() if ($opt{'p'} && ($nbytes || $nlines));

my $infile = shift;
my $prefix = shift;
die("$me: extra operand: $ARGV[0]\n") if @ARGV;
unless (defined $infile) {
    $infile = '-';
}
unless (defined $prefix && length $prefix) {
    $prefix = 'x';
}

my $in;
if ($infile eq '-') {
    $in = *STDIN;
}
else {
    die("$me: $infile: is a directory\n") if (-d $infile);
    open($in, '<', $infile) or die("$me: Can't open $infile: $!\n");
}
binmode $in;
## Byte operations.
if ($opt{'b'}) {
    my ($chunk, $fh);
    while (read ($in, $chunk, $nbytes) == $nbytes) {
	$fh = nextfile ($prefix);
	print {$fh} $chunk;
    }

    # leftover bit. Last file will be >= $count.
    # There's gotta be something more elegant than this, too.
    if (length($chunk)) {
	$fh = nextfile ($prefix);
        print {$fh} $chunk;
    }
}

## Split on patterns.
elsif (defined $opt{'p'}) {
    my $regex = $opt{p};
    my $fh = nextfile ($prefix);

    while (<$in>) {
	$fh = nextfile ($prefix) if /$regex/;
	print {$fh} $_;
    }
}

## Line operations.
else {
    my $fh;
    $nlines = 1000 unless $nlines;
    my $line = 0;

    while (<$in>) {
	$fh = nextfile ($prefix) if $line % $nlines == 0;
	print {$fh} $_;
	$line++;
    }
}
close $in;
exit;

# (Thanks to Abigail for the pod template.)

=pod

=head1 NAME

split - split a file into pieces

=head1 SYNOPSIS

split [C<-b> byte_count[k|m]] [C<-l> line_count] [C<-p> pattern] [file [name]]

=head1 DESCRIPTION

The B<split> utility reads the given I<file> (or standard input if no file
is specified) and breaks it up into files of 1000 lines each.

=head1 OPTIONS

B<split> accepts the following options:

=over 4

=item -b byte_count[k|m]

Create smaller files I<byte_count> bytes in length.  If ``k'' is
appended to the number, the file is split into I<byte_count> kilobyte
pieces.  If ``m'' is appended to the number, the file is split into
I<byte_count> megabyte pieces.

=item -l line_count[k|m]

Create smaller files I<line_count> lines in length. ``k'' and ``m'' operate as
with B<-b>.

=item -p pattern

The file is split whenever an input line matches I<pattern>, which is
interpreted as a Perl regular expression.  The matching line will be
the first line of the next output file.  This option is incompatible
with the B<-b> and B<-l> options.

=back

If additional arguments are specified, the first is used as the name
of the input file which is to be split.  If a second additional
argument is specified, it is used as a I<prefix> for the names of the
files into which the file is split.  In this case, each file into
which the file is split is named by the I<prefix> followed by a
lexically ordered suffix in the range of ``aaa-zzz''.

If the name argument is not specified, the file is split into lexically
ordered files named in the range of ``xaaa-xzzz''.

=head1 BUGS

B<split> can only create 17576 files.

=head1 SEE ALSO


perlre(1)

=head1 AUTHOR

The Perl implementation of B<split> was written by Rich Lafferty,
I<rich@alcor.concordia.ca>.

=head1 COPYRIGHT and LICENSE

This program is free and open software. You may use, copy, modify,
distribute and sell this program (and any modified variants) in any
way you wish, provided you do not restrict others to do the same.

=cut
