#!/usr/bin/perl -w
use warnings;
use strict;
package detect_cameras
{
    sub new
    {
        my $class = shift;
        my $self = {};
        bless($self, $class);
        return $self;
    }

    sub doupstart
    {
        return 1;
    }

    sub upstart
    {
        print "Detecting cameras... this may take a long time...\n";
        my $DIR;
        undef($main::plugins{'camera'}); #Reset the camera list
        my @cameras; # is the camera list.
        opendir ($DIR, "/dev");
        my @files = grep(m/video\d+/, readdir($DIR));
        while (scalar(@files) > 0)
        {
            push(@cameras, shift(@files));
        }
        closedir($DIR);
        $main::plugins{'camera'} = \@cameras;
        print scalar(@cameras)," cameras found.\n";
    }

    sub command
    {
        return "Detect Cameras";
    }

    sub help
    {
        return "find and list all the cameras connected to this system, any cameras found will be used to take images when the command 'capture' is used.";
    }

    sub do
    {
        my $DIR;
        undef($main::plugins{'camera'}); #Reset the camera list
        my @cameras; # is the camera list.
        opendir ($DIR, "/dev");
        my @files = grep(m/video\d+/, readdir($DIR));
        while (scalar(@files) > 0)
        {
            push(@cameras, shift(@files));
        }
        closedir($DIR);
        $main::plugins{'camera'} = \@cameras;
        print $main::socket "{ \"count\":",scalar(@cameras)," }\n";
    }
    sub defaultsettings
    {
    return undef;
    }
1;
}