#!/usr/bin/perl

use Getopt::Long;

GetOptions(
	"--dir=s"		=> \$watchDir,
	"--help"		=> \$help,
	"--player=s"	=> \$soundPlayer,
	"--quiet"		=> \$quiet,
	"--sound=s"		=> \$soundFile,
	"--keep=i"		=> \$keepCount,
	"--keepAll"		=> \$keepAll,
	"--name=s"		=> \$name,
	"--padding=i"	=> \$padding,
	"--prefix=s"	=> \$snapPrefix,
	"--start=i"		=> \$startIndex,
	"--suffix=s"	=> \$iconSuffix,
	"--verbose"		=> \$verbose
);

($prog=$0) =~ s!.*/!!;

$watchDir = "$ENV{HOME}/www/snapshots" unless $watchdir;
$snapPrefix = "snap" unless $snapPrefix;
$keepCount = $ENV{WATCHCOUNT} unless $keepCount;
$soundPlayer = "/usr/sbin/sfplay" unless $soundPlayer;
chop($host = `hostname`);
$name = $host unless $name;
$iconSuffix = "icon" unless $iconSuffix;

$bin = "/u/rae/bin/graphics";
$convert = "$bin/convert";
$mogrify = "$bin/mogrify";


if($help) {
	print <<"EOF";

Usage: $prog [-d dir] [-h] [-k n] [-n name] [-pa pad] [-pl soundplayer]
            [-pr prefix] [-q] [-st start] [-su suffix] [-so sound] [-v]

$prog saves images in 'dir' [default $ENV{HOME}/www/snapshots] with the names
name-suffix.jpg and prefix-name.jpg -- the default name is the hostname of the
machine on which $prog is run, the default prefix is "snap", the default
suffix is "icon".  The numbers will be padded to 3 digits, unless another
'pad' is specified.  Indexing will start at '001' unless another 'start' value
is specified.

$prog will play the sound 'sound' using the program 'soundplayer' [default
/usr/sbin/sfplay]. If the '-q' flag is also present, no sound will be played.

$prog will spit out lots of info as it goes if you specify the -v flag.

EOF
	exit(0);
}

if($quiet) {
	$soundFile = 0;
	$soundPlayer = 0;
}

# check for existance of watch directory
if(! -d ${watchDir}) {
	print "### $prog: Error\n";
	print "### The directory ${watchDir} does not exist.\n";
	exit(1);
}

print "cd $watchDir\n" if $verbose;
chdir ${watchDir};

$padding = 3 unless $padding;
$startIndex = 1 unless $startIndex;
# rotate the keepers
if($keepCount || $keepAll) {
	for($i=$startIndex; ; $i++) {
		$istr = sprintf("%0${padding}d", $i);
		$thisName = "$snapPrefix-$name.$istr.jpg";
		last unless -e $thisName;
	}
	print "mv -f $snapPrefix-$name.jpg $thisName\n" if $verbose;
	system("mv -f $snapPrefix-$name.jpg $thisName");
}

if($keepCount) {
	foreach $file (<${snapPrefix}-${name}.*.jpg>) {
		print "# checking $file\n" if $verbose;
		if($file =~ /${snapPrefix}-${name}.(\d+).jpg/) {
			$num = $1;
			if($num > $keepCount) {
				print "rm $file\n" if $verbose;
				unlink $file;
			}
		}
	}
}

# make sure the temp file isn't already there
print "rm -f ${name}-00000.rgb\n" if $verbose;
unlink "${name}-00000.rgb";

# capture the frames
if($soundFile) {
	system("$soundPlayer $soundFile > /dev/null 2>&1");
}
print "vidtomem -f ${name}\n" if $verbose;
$status = system("vidtomem -f ${name} > /dev/null 2>&1");
$status /= 16;
if($status) {
	die "$prog: Unable to take a snapshot from ${host}'s video camera: $! [error $status]\n"
}
if($soundFile) {
	system("$soundPlayer $soundFile > /dev/null 2>&1");
}

# convert ${name}-00000.rgb to ${filename}
print "$convert -normalize -quality 60 -gamma 1.5 ${name}-00000.rgb ${snapPrefix}-${name}.jpg\n" if $verbose;
$status = system("$convert -normalize -quality 65 -gamma 1.5 ${name}-00000.rgb ${snapPrefix}-${name}.jpg");
$status /= 16;

if(! $status && -r "${snapPrefix}-${name}.jpg") {
	# make a 1/4-size icon
	print "cp ${snapPrefix}-${name}.jpg ${name}-${iconSuffix}.jpg\n" if $verbose;
	$status = system("/bin/cp ${snapPrefix}-${name}.jpg ${name}-${iconSuffix}.jpg");
	$status /= 16;
	unless($status) {
		print "$mogrify -geometry 25% ${name}-${iconSuffix}.jpg\n" if $verbose;
		$status = system("$mogrify -geometry 25% ${name}-${iconSuffix}.jpg");
		$status /= 16;
	}
} else {
	die "## ERROR: ${snapPrefix}-${name}.jpg didn't get created: $! [error $status]\n";
}

print "rm -f ${name}-00000.rgb\n" if $verbose;
unlink "${name}-00000.rgb";
