#!/usr/bin/perl

#version 1.0

package sapouni;
$sapouni = "nice /usr/bin/diatheke";  # location of diatheke command line program -- if you are using a MS Windows server, you might need to remove the "nice"

$sword_path = "/home/sword";  # SWORD_PATH environment variable you want to use
$maxverses = 0; # maximum number of verses diatheke will return per query (prevents people from asking for Gen1:1-Rev22:21; 0 for unlim.)
$defversion = "KJV"; # book to query when none is selected, but a verse/search is entered
$deflocale = "abbr";  # this is just the default for cases where user has not selected a locale and his browser does not reveal one -- you can also set locale using locael=<locale> in the GET URL


###############################################################################
## You should not need to edit anything below this line.
## Unless you want to modify functionality of course. :)
###############################################################################

$ENV{'SWORD_PATH'} = $sword_path;

use SOAP::Transport::HTTP;

SOAP::Transport::HTTP::CGI
    -> dispatch_to('sapouni')
    -> handle;

package sapouni;

sub biblequery {
    my ($class, $books, $key, $options, $encoding, $markup, $searchtype, $locale, $script, $max) = @_;

    if ($key eq "") {
	$key = "Jn 3:16";
    }

    @booklist = split ' ', $books;
    
    $n = scalar(@booklist);
    if ($n == 0) {
	@booklist[0] = $defversion;
	$n++;
    }

    $query = "";

    if ($options ne "") {
	$query .= " -o \"$options\"";
    }

    if ($encoding ne "") {
	$query .= " -e \"$encoding\"";
    }
    else {
	$query .= " -e UTF8";
    }

    if ($markup ne "") {
	$query .= " -f \"$markup\"";
    }
    else {
	$query .= " -f ThML";
    }

    if ($searchtype ne "") {
	$query .= " -s \"$searchtype\"";
    }

    if ($locale ne "") {
	$query .= " -l \"$locale\"";
    }
    else {
	$query .= " -l $deflocale";
    }

    if ($script ne "") {
	$query .= " -t \"$script\"";
    }
    
    if ($max ne "" && $max ne 0) {
	$query .= " -m \"$max\"";
    }

    $rval = "";
    for ($i = 0; $i < $n; $i++) {   
	$line = "$sapouni $query -b $booklist[$i] -k \"$key\" 2> /dev/null";
	
	# uncomment to print the command line send to Diatheke (for debugging)
	# $rval .= "$line\n";

	$line = `$line`;
	chomp($line);
	
	$rval .= "$line\n";
    }	

    return "$rval";
}
