#! /usr/bin/perl -w

# Sat Dec 29 15:16:30 JST 2007
# Takahiro Shinozaki

# divide file into multiple blocks.

if (@ARGV != 4) {
    die "$0 scp num outd stem\n";
}

$scpf = $ARGV[0];
$numb = $ARGV[1];
$outd = $ARGV[2];
$stem = $ARGV[3];

open(IN, $scpf) || die "$! : $scpf\n";
for ($i=0; $i<$numb; $i++) {
    $outfh = "out$i";
    $j=$i+1;
    $outf = "$outd/$stem.$j"; # file extension starts with 1 (not 0)
    open($outfh, ">$outf") || die "$! : $outf\n";
    $out[$i]=$outfh;
}
$i=0;
while (<IN>) {
    $i = 0 if ($i == $numb);
    $outfh = $out[$i];
    print $outfh $_;
    $i++;
}
