#!/usr/bin/perl #author:Chris Sincock #date:20070127 #version:20070127 #license: GPL version 2 use Sys::Gamin; sub usage() { print STDERR < --remoteconfig This program watches for changed files in a 'local directory' and puts them to an ftp server. The idea being that I can edit my web pages locally and then immediately refresh my web browser to see my changes on my actual web site. The local directory is the directory which you wish to watch for changes and mirror to the ftp directory. The remoteconfig config file is a config file for ncftpput which should specify the remote host, user, and password. see the ncftpput man page for details. You will need to have ncftpput installed and not be bothered by your ftp password being stored in a config file in plain text. Software Requirements: ncftp (ncftpput) gamin perl Sys::Gamin module END_OF_USAGE } my $ldir; my $rcfg; while (@ARGV) { my $arg=shift @ARGV; if($arg eq "--localdir") { $ldir=shift @ARGV; } elsif($arg eq "--remoteconfig") { $rcfg=shift @ARGV; if($rcfg !~ m%^/%) { die "Expected an absolute path for --remoteconfig"; } } else { usage(); } } if(! (-f $rcfg && -d $ldir)) { usage(); } chdir $ldir or die "Could not change directory to:$ldir\n"; my $fm=new Sys::Gamin; $fm->monitor("."); my %watched=(); while(1) { my $event=$fm->next_event; my $type=$event->type; my $filename=$event->filename; my $which=$fm->which($event); if($filename !~ m%^/%) { if($which ne ".") { $filename=$which."/".$filename; } } print "\nparent=",$which,"\nPath:",$filename,"\nEvent: ",$type,"\n"; if($type eq "exist") { if(-d $filename) { $fm->monitor($filename); $watched{$filename}++; } #otherwise ignore it. next; } if($type eq "create") { if(-d $filename) { $fm->monitor($filename); $watched{$filename}++; } } if($type eq "delete") { if($watched{$filename}) { print STDERR "watched ${filename} is true for some fucking reason\n"; $fm->suspend($filename); } } if($filename =~ m%^/%) { print STDERR "ignoring absolute path event\n"; next; } my $dirpart=$filename; $dirpart =~ s%/[^/]+$%%; print STDERR "dirpart=$dirpart\n"; my $fpart=$filename; $fpart =~ s%.*/%%g; print STDERR "fpart=$fpart"; if($dirpart eq "" || $dirpart eq $fpart) { $dirpart=""; } if($type eq "change") { system("exec-in-bg","ncftpput","-f",$rcfg,$dirpart, $filename); } }