#!/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: install-tagbox,v 1.5 2006/10/26 17:30:02 jamiemccarthy Exp $

# This is the tagboxes install script.
# -Cbrown (cbrown@vasoftware.com)

use strict;
use File::Basename;
use FindBin '$Bin';
use Getopt::Std;
use File::Copy;
use Slash::Install;

(my $VERSION) = ' $Revision: 1.5 $ ' =~ /\$Revision:\s+([^\s]+)/;
my $PROGNAME = basename($0);
(my $SLASH_PREFIX = $Bin) =~ s|/[^/]+/?$||;

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

$| = 1;

unless (DBIx::Password::checkVirtualUser($opts{'u'})) {
	print "You did not supply a valid DBIx::Password virtual name($opts{'u'}).\n";
	exit;
}

{
	my $install = Slash::Install->new($opts{'u'});

	print "\nPlease select which tagboxes you would like?\n" unless $opts{'l'} ;
	my $tagboxes = $install->getTagboxList($SLASH_PREFIX);
	for (sort keys %$tagboxes) {
		print "$tagboxes->{$_}{'order'}.\t$_ $tagboxes->{$_}{'description'}\n";
	}
	exit 0 if $opts{'l'};

	my @answers;
	my $select = 'a';
	print "Hit 'a' to select all, otherwise select comma separated numbers or 'q' to quit\n";
	while ($select ne 'q'){
		chomp($select = <STDIN>);

		if ($select =~ /^\d\,/) {
			@answers = split(/\,/, $select);
			last;
		} elsif ($select eq 'a') {
			for (keys %$tagboxes) {
				push @answers, $tagboxes->{$_}{'order'};
			}
			last;
		} elsif ($select eq "") {
			last;
		} elsif ($select ne 'q') {
			push @answers, $select;
		}
	}

	$install->installTagboxes(\@answers, 0, 1);

	print <<EOT;


Installed.

Please check to see if there are any README files for your site's
tagboxes that you haven't already read.  You'll want to restart
slashd now.

EOT
}

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

Usage: $PROGNAME [OPTIONS]

Installs Slash tagboxes.

Main options:
	-h	Help (this message)
	-v	Version
	-u	Virtual user (default is "slash")
	-l	Does not install tagboxes, just lists known tagboxes

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__
