#!/usr/local/bin/perl -w

#### LAST UPDATED: Jul, 2003 ####
# chunk.pl version 0.1

###########################################################################
#
# This program splits a SOURCE file into multiple files. Each piece has NUM 
# lines except the last file which may have less. The output files are 
# named DESTINATION.1, DESTINATION.2 and so on.
#
# USAGE :  chunk.pl [OPTIONS] NUM DESTINATION SOURCE
#
#       --version       Prints the version number.
#       --help          Prints help message.
#
###########################################################################

# Copyright (C) 2000-2002
# Ted Pedersen, University of Minnesota, Duluth
# tpederse@d.umn.edu
# Mohammad Saif, University of Minnesota, Duluth
# moha0149@d.umn.edu
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

# START OF PROGRAM
use Getopt::Long;

# Check if any options specified
GetOptions("version", "help");
if ( defined $opt_help )    { $help = $opt_help; }
else                        { $help = 0; }
# Print help if asked
if ( $help ) {
    &help();
    exit; }
# if version has been requested, show version
if ( defined $opt_version ) {
    $opt_version = 1;
    &showVersion();
    exit; }

# Check if command line options have been provided
# If not print usage information
if ( $#ARGV < 2 ) {
    print "Insufficient command line arguments.\n";
    &usage();
    exit;}

# Accept number of lines per file
$NUM = shift();
# Check if NUM is a positive integer
if($NUM !~ /^\d+$/) 	{
    print "NUM must be a positive integer.\n";
    &usage();
    exit;}

# Accept SOURCE and DESTINATION names
$opfl = shift();
$inpfl = shift();

# If output file exists, ask permission to over-write
if(-e $opfl) {  
    print "File/Directory with same name as DESTINATION already exists. Press `y' to overwrite\n";
    $resp = <STDIN>;
    chomp($resp);
    if($resp eq "y" || $resp eq "Y") {
	print "Overwriting the file.\n"; }
    else{
	exit; } }

# Check if SOURCE exists
if(!(-f $inpfl)) {
    print "SOURCE file does not exist.\n";
    &usage();
    exit; } 

# Open the various files
open(INP,"<$inpfl");
$lines=0;
$index=1;
open(OP,">$opfl.$index");

# Read lines from input file
while($ln = <INP>) {
	# Ignore blank lines
	if($ln =~ /^\s*$/){
	    next;}
	$lines++;
    	print OP $ln; 

	# Check if NUM lines have been read
	if($lines==$NUM) {
		$lines=0;
		$index++;
		close(OP);
		open(OP,">$opfl.$index"); }
}

# Subroutine to print help message
sub help{
    print "Usage: chunk.pl [OPTIONS] NUM DESTINATION SOURCE\n\n"; 
    print "This program splits a SOURCE file into multiple files. Each piece has NUM lines except the last file which may have less. The output files are named DESTINATION.1, DESTINATION.2 and so on.\n\n";
    print "OPTIONS:\n";
    print "  --version    Prints the version number.\n";
    print "  --help       Prints the help message.\n\n";}

sub usage{
    print "Usage: chunk.pl [OPTIONS] NUM DESTINATION SOURCE\n"; 
    AskHelp();}
# Message describing way to get help
sub AskHelp{
    print "Type chunk.pl --help for help.\n";}
# function to output the version number
sub showVersion
{
    print STDERR "chunk.pl      -        version 0.1\n";
    print STDERR "A component of the parSenseval 0.1\n";
    print STDERR "Copyright (C) 2000-2003, Saif Mohammad and Ted Pedersen\n";
}

