#!/usr/bin/perl

use strict;
use warnings;

use lib "./lib";

use PS::IPP::IData::Client;

my $idata = PS::IPP::IData::Client->new(
    uri     => 'http://localhost:80/PS/IPP/IData/Server',
    proxy   => 'http://localhost:80/idata'
);

my $key = shift;

my $kids = shift || 1;

foreach my $id ( 1..$kids ) {
    my $pid = fork;

    unless ( $pid )  {
        my $fname = "${key}_$id";
        my $fh = $idata->create( $fname );
        die "can't create file $fname" unless $fh;

        print $fh "fooby\n";

        close $fh;
        $idata->unlock( $fname, 'write' );

        $fh = $idata->open( $fname, 'read' ) or die "can't open file";
        close $fh;
        $idata->unlock( $fname, 'read' );

        $idata->replicate( $fname );
        $idata->cull( $fname );
        $idata->find( $fname );
        $idata->copy( $fname, $fname . "_copy" );
        $idata->move( $fname, $fname . "_move" );
        $idata->delete( $fname . "_copy" );
        $idata->delete( $fname . "_move" );

        exit 0;
    }
}

while ( $kids ) {
    wait();
    $kids --;
}
