#!/usr/bin/perl

$ENV{PATH} = "$ENV{PATH}:/usr/bin";
$ENV{PATH} = "$ENV{PATH}:/bin";
$ENV{PATH} = "$ENV{PATH}:/mnt/net/usr/bin";
$ENV{PATH} = "$ENV{PATH}:/mnt/net/home/magnus/pfl/build/bin";

my $DEVICE = "/dev/cdroms/cdrom0";

&debug("begining cd_poll...");
open POLL, "cd_poll $DEVICE|";
&debug("entering loop...");

while ($line = <POLL>) {
    &debug("$line");
    if ($line =~ m/^new media$/) {
	close POLL;  # it will have exited anyway
	system("kill -USR1 `cat /var/run/slideshow.pid`");;
	&debug("mounting cd...");
	system ("mount /mnt/cdrom");
	&debug("searching cd...");
	system ("find /mnt/cdrom -iname '*.jpg' -exec cp {} /mnt/hd/images/ ';'");
	system ("chmod 660 /mnt/hd/images/*");
	system ("killall fbi");
	&debug("unmounting cd...");
	system ("umount /mnt/cdrom");
	&debug("ejecting cd...");
	system ("/usr/bin/eject -v $DEVICE");
	open POLL, "cd_poll $DEVICE|";
    }
}

sub debug {
    print @_, "\n";
}


# cd_poll has to be killed explicitly before the cd can be ejected.
# to do this, we need some middle-high magic, but as it turns out, I
# can just change cd_poll to exit.  Duh.

