#!/usr/bin/perl -w
# $Id: netexamplix-inotify,v 1.1 2010-02-05 00:58:31 jeffbeck Exp $
# This Script (examplix) was created by Rdiger Beck
# It is released under the GPL Version 2 or higher
# For Bugs send mail to:
# jeffbeck@web.de

# Bibliotheken
use strict;
use Linux::Inotify2;

#my $inotify = new Linux::Inotify2;

#$inotify->watch ("/home/bz/test",IN_ACCESS, sub {
#    my $e = shift;
#    print "$e->{w}{name} was accessed\n" if $e->IN_ACCESS;
#
#}) or die "watch creation failed\n";

#$inotify->watch ("/home/bz/test",IN_CLOSE);

#while () {
#    my @events = $inotify->read;
#    unless (@events > 0){
#        print "read error: $!";
#	    last;
#    }
#    printf "mask\t%d\n", $_->mask foreach @events;
#}


my $dir="/home/bz/test";
my $logfile="/home/bz/loggy";

### Define an Inotify2 instance
my $inotify = new Linux::Inotify2
or die "Unable to create new inotify object: $!";
$inotify->watch ( $dir, IN_ALL_EVENTS )
or die "watch creation failed";

### Open the logfile for writing
open( LOGFILE, ">>$logfile" );

### Monitor the defined directory and if a .wav file is moved into it
### convert that file to .ogg format in a different directory. Write
### the result to the logfile.
while() {
    my @events = $inotify->read;
    unless ( @events > 0 ) {
        print "read error: $!";
        last;
    }
    foreach( @events ) {
        my @path = split( /\//, $_->fullname );
        my $infile = pop( @path );
        #if( $infile =~ '.wav' ) {
        #    my @array = split( /\./, $infile );
        #    my $outname = $array[0];
            print LOGFILE "date: $infile\n";
            #print LOGFILE "date: $dir/$infile converted to $outname.ogg\n";
            #`oggenc $wavdir/$infile -Q -o $oggdir/$outname.ogg`;
        #}
    }
}
close( LOGFILE );




