#!/usr/local/bin/perl
#
# This tool is used to view images in a database
# It is designed to track viewing time and view count,
# randomizing the next image based on user preference
#
# $Id: imaverage,v 1.19 2000/06/26 18:57:21 nemesis Exp $
#
# Copyright (C) 2000 Cornelius Cook
# cook@cpoint.net, http://collective.cpoint.net/
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
# http://www.gnu.org/copyleft/gpl.html

#
# TODO:
#	- need to have single-key input in the Perl
#         script, rather than the viewer.  That way
#         we can implement various functions, like
#         "remove image from db", "ignore my viewing
#         time", "quit out totally", etc etc
#         instead of depending on exit codes from the
#         viewer
#
require "sql-attach.pl";

$VERSION="0.9.0";

# force full flushing
select STDERR; $|=1;
select STDOUT; $|=1;

# build handle
$dbh = DBI->connect($SQL_dsn, $SQL_user, $SQL_password, { RaiseError => 1 } )
        || die "Can't connect to $dsn: $DBI::errstr";

# defaults
$VIEWER="xv -geom +50+0";
$DELAYARG="-wait";
$DELAY=60;

# set up signal hanlders
$SIG{'TERM'}=$SIG{'INT'}=$SIG{'QUIT'}="Shutdown";

# for testing signal handlers
if ($ARGV[0]) {
	warn "sleeping for $ARGV[0] seconds ...\n";
	sleep $ARGV[0];
}

for (;;) {
	ViewImage(GetNextImage());
}
Shutdown("For loop ended??");

sub Shutdown {
	my($text)=@_;
	if ($text) {
		warn "$text\n";
	} else {
		warn "Signal!\n";
	}

	$dbh->disconnect;

	# quit out
	exit(0);
}

sub GetNextImage {
	my($img,$path,$query);

	# our default view-weighted random query
	$query="SELECT image FROM Images ORDER BY (view_time/view_count)*RAND() DESC LIMIT 1";

	# give preference to unviewed images
	$img=GetScalar($dbh,"SELECT COUNT(DISTINCT image) FROM Images WHERE view_time=0");
	$query="SELECT image FROM Images WHERE view_time=0 ORDER BY RAND() DESC LIMIT 1" if ($img>0);

	$img=GetScalar($dbh,$query);
	$path=GetScalar($dbh,"SELECT path FROM Images WHERE image=$img");

	return ($img,$path);
}

sub ViewImage {
	my($img,$path)=@_;
	my($start,$finish,$view_time);
	my($exit_value);

	$start=time();	 # get our start time

	# is this the best system viewer?
	print STDERR "Viewing '$path' ... ";
	system("$VIEWER $DELAYARG $DELAY '$path'");
	$exit_value=$?;

	if ($exit_value != 0) {
		print STDERR "exit: $exit_value "
	}

	$finish=time();	 # get our end time

	$view_time=$finish-$start; # how long we viewed it
	print STDERR "${view_time} sec\n";

	# make sure the system clock wasn't mucked with
	if ($view_time<0) {
		$view_time=0;
		warn "Went back in time??\n";
	}
	# if we waited the entire time for the viewer to exit, we don't
	#  want to keep running
	if ($view_time>=$DELAY) {
		Shutdown("Looks like you went to sleep.  Stopping viewer...");
	}
	
	# should I increment the view count before viewing it, and then the view time
	#  after the viewing?
	$dbh->do("UPDATE Images SET view_count=view_count+1, view_time=view_time+$view_time WHERE image=$img");

	# if we didn't get a user quit from the viewer, we should shutdown
	Shutdown("Exit requested") if ($exit_value != 0);
}
