#!/usr/bin/perl

# iwebcam 1.0 by Josh Mast <josh@hivehaus.org>
# indycam + perl + netpbm + scp == fun.
# This assumes you have all of the above in order to make the equation work.
# as well as a passwordless key for ssh authentication to your host.
# ... and you better be running irix 6.5 as well, probably.
#
# I wrote this because I'm on a dialup and don't really have the bandwidth
# to spare running apache and doing webcam updates locally via cgi, plus
# all my webhosting was on a machine other than my indy.
#
# iwebcam on - turn webcam updating on (providing you've set it up in cron)
# iwebcam off - turn webcam updating off
# iwebcam manual - manually send html/jpeg to remote host
#
# My crontab entry:
# * * * * * /path/to/iwebcam 1> /dev/null 2> /dev/null

## stuff to configure ###################################################

$user = "zhixel";				# username on remote machine
$gamma = "1.8";
$host = "ohno.mrbill.net";			# hostname of remote machine
$remotedir = "~/www.zhixel.com/htdocs/cam/";	# directory to stick files in remote machine
$bindir = "/usr/freeware/bin/";			# directory where your netpbm & ssh binaries are located
$tempdir = "/tmp/";				# directory to keep temp files in.
$htmlfile = "index.html";			# name of html file to generate
$graphicname = "cam.jpg";			# name of jpeg to generate
$title = "hitting you in the face.";			# <title> of html file.
$commentline = "I AM BRAZILE.<br><br>... almost every minute on the
minute ...<br><br><a
href=\"mailto:josh\@hivehaus.org\">josh\@hivehaus.org</a><br>"; 	# comment line in html file.

# end of stuff to configure, nothing interesting below this point. ######

sub zhixeltime {

($one, $two, $three, $four, $five, $six, $seven, $eight, $nine, $ten) = localtime;



}

sub takepicture {
	$date = localtime;
	`vidtomem -f $tempdir/tempgfx`;
	`dmconvert -f tiff $tempdir/tempgfx-00000.rgb $tempdir/tempgfx.tif`;
	`$bindir/tifftopnm $tempdir/tempgfx.tif | $bindir/pnmscale -xysize 320 200 | $bindir/pnmgamma $gamma | $bindir/ppmtojpeg > $tempdir/$graphicname`;
	`rm $tempdir/tempgfx-00000.rgb`;
	`rm $tempdir/tempgfx.tif`;
	open(HTMLFILE, ">$tempdir/$htmlfile");
	print HTMLFILE "<html><head><title>$title</title></head>";
	print HTMLFILE "<meta http-equiv=\"Refresh\" content=\"60\">";
	print HTMLFILE "<body bgcolor=white text=#a9a9a9><center><br><br><br>\n";
	print HTMLFILE "<font face=verdana, helvetica size=-2>\n";
	print HTMLFILE "$date<br><br>\n";
	print HTMLFILE "<img src=$graphicname border=1 alt=\"me.\"><br><br>\n";
	print HTMLFILE "$commentline<br><br>\n";
	print HTMLFILE "</font></body></html>";
	close(HTMLFILE);
	`$bindir/scp $tempdir/$htmlfile $user\@$host:$remotedir`;
	`$bindir/scp $tempdir/$graphicname $user\@$host:$remotedir`;
};

if ($ARGV[0] =~ "on") {
	`touch $tempdir/camon`;
	exit(0);
} elsif ($ARGV[0] =~ "off") {
	`rm $tempdir/camon > /dev/null`;
	exit(0);
} elsif ($ARGV[0] =~ "manual") {
	takepicture();
	exit(0);
};

if (-e "$tempdir/camon") {
	takepicture();
};
