#!/usr/bin/perl -w
# This code is a part of Slash, and is released under the GPL.
# Copyright 1997-2005 by Open Source Technology Group. See README
# and COPYING for more information, or see http://slashcode.com/.
# $Id: rebuildAutoPoll,v 1.6 2006/08/31 13:39:58 jamiemccarthy Exp $

# What's this script for?  Is it still used? - Jamie 2006-08-27

use strict;
use File::Basename;
use Getopt::Std;
use Slash;
use Slash::Utility;
use Slash::Constants ':people';
use Slash::PollBooth;

use vars qw( $pollbooth_db $werder $constants $junk );

(my $VERSION) = ' $Revision: 1.6 $ ' =~ /\$Revision:\s+([^\s]+)/;
my $PROGNAME = basename($0);

my %opts;
# Remember to doublecheck these match usage()!
usage('Options used incorrectly') unless getopts('hu:v', \%opts);
usage() if $opts{'h'};
$opts{'u'} ||= 'slash';

createEnvironment($opts{u});
$pollbooth_db = getObject('Slash::PollBooth');
$constants = getCurrentStatic();

my $entries = $pollbooth_db->sqlSelectAll('section,qid', 'auto_poll');

for (@$entries) {
	my $qid = $_->[1];
	my $sids = $pollbooth_db->sqlSelectColArrayref('sid', 'stories', "qid IS NULL AND section = '$_->[0]'" );
	for my $sid (@$sids) {
		print "Doing $sid \n";
		my $question = $pollbooth_db->getPollQuestion($qid, 'question');
		my $answers = $pollbooth_db->getPollAnswers($qid, [ qw| answer | ]);
		my $newpoll = {
			section => $_->[0],
			topic => 1,
			autopoll => 'yes',
			question  => $question,
		};
		
		my $x =1;
		for my $answer (@$answers) {
			$newpoll->{'aid' . $x} = $answer->[0];
			$x++;
		}
		my $qid = $pollbooth_db->savePollQuestion($newpoll);
		$pollbooth_db->setStory($sid, { qid => $qid, writestatus => 'dirty' });
	}
}


sub usage {
	print "*** $_[0]\n" if $_[0];
	# Remember to doublecheck these match getopts()!
	print <<EOT;

Usage: $PROGNAME [OPTIONS] [#users]

This rebuilds the people table for the Zoo system.
(Um, I'm pretty sure it doesn't, actually.)

Main options:
	-h	Help (this message)
	-v	Version
	-u	Virtual user (default is "slash")

EOT
	exit;
}


sub version {
	print <<EOT;

$PROGNAME $VERSION

This code is a part of Slash, and is released under the GPL.
Copyright 1997-2005 by Open Source Technology Group. See README
and COPYING for more information, or see http://slashcode.com/.

EOT
	exit;
}

__END__
