#!/usr/bin/perl
#
# BNCweb: A user-friendly interface to the British National Corpus - Version 4.3
# Copyright (C) 2005-10 Hoffmann/Evert/Lehmann
#
# Updates and information available at http://www.bncweb.info
#
# This file is part of BNCweb.
# 
# BNCweb 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 3 of the License, or
# (at your option) any later version.
# 
# BNCweb 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, see <http://www.gnu.org/licenses/>.

#
# based on BNCweb Version 2.0:
# © 1996/2004 Lehmann/Hoffmann/Schneider
#

use lib "../lib_files";
use bncLibCQPXML;
use DBI;
	
unless (getParams($ENV{'QUERY_STRING'})) {
	barkFull("incomplete transmition of URL<BR>This problem is likely to occur in combination with proxy servers<BR>turn off your web-proxy and try again");
}

setParam("delHits", "no");
$SQL = $params{SQL};
setParam("SQL", "");
$theURL = getURLparams();

if ($params{selected} eq "Distribution") {
	print "Location: /$bwCGIalias/distribution.pl?aim=dist&$theURL&urlTest=yes\n\n";
	exit(0);
} elsif ($params{selected} eq "Collocations...") {
	print "Location: /$bwCGIalias/coll_options.pl?$theURL&urlTest=yes\n\n";
	exit(0);
} elsif ($params{selected} eq "New Query") {
	print "Location: /$bwCGIalias/BNCquery.pl?theQuery=search&urlTest=yes\n\n";
	exit(0);
} elsif ($params{selected} eq "New Frequency List") {
	print "Location: /$bwCGIalias/BNCquery.pl?theQuery=frequency&urlTest=yes\n\n";
	exit(0);
} elsif ($params{selected} eq "Download whole Frequency List") {
	print "Location: /$bwCGIalias/freqlist_download.pl?$theURL&urlTest=yes\n\n";
	exit(0);
} elsif ($params{selected} eq "Download...") {
	print "Location: /$bwCGIalias/download_options.pl?$theURL&urlTest=yes\n\n";
	exit(0);
} elsif ($params{selected} eq "Sort") {
	print "Location: /$bwCGIalias/create_db.pl?kind=sort&ot=after1&exclude=&$theURL&urlTest=yes\n\n";
	exit(0);
} elsif ($params{selected} eq "Frequency breakdown") {
	print "Location: /$bwCGIalias/create_db.pl?kind=sort&ot=node&exclude=&sortpos=1&sorttype=direct&$theURL&urlTest=yes\n\n";
	exit(0);
} elsif ($params{selected} eq "New Lookup") {
	print "Location: /$bwCGIalias/BNCquery.pl?theQuery=lookup&urlTest=yes\n\n";
	exit(0);
} elsif ($params{selected} eq "frequency") {
	setParam("SQL", "$SQL");
	$theURL = getURLparams();
	print "Location: /$bwCGIalias/sort_freqList.pl?$theURL&urlTest=yes\n\n";
	exit(0);
} elsif ($params{selected} eq "Add files to subcorpus") {
	print "Location: /$bwCGIalias/subcorpAdmin.pl?action=addFiles&$theURL&urlTest=yes\n\n";
	&disconnect();
	exit(0);
} elsif ($params{selected} eq "Categorize hits...") {
	print "Location: /$bwCGIalias/categorizeAdmin.pl?action=choose&$theURL&urlTest=yes\n\n";
	exit(0);
} elsif ($params{selected} eq "New Keyword calculation") {
	print "Location: /$bwCGIalias/BNCquery.pl?theQuery=keywords_stat&urlTest=yes\n\n";
	exit(0);
} elsif ($params{selected} eq "Download whole list") {
	print "Location: /$bwCGIalias/keywords_download.pl?$theURL&urlTest=yes\n\n";
	exit(0);
} elsif ($params{selected} eq "Save current set of hits...") {
	print "Location: /$bwCGIalias/save.pl?action=getName&$theURL&urlTest=yes\n\n";
	exit(0);
} elsif ($params{selected} eq "collocation_redirect") {
	print "Location: /$bwCGIalias/show_collocation.pl?$theURL&urlTest=yes\n\n";
	exit(0);	
}  elsif ($params{selected} eq "collocation_download") {
	print "Location: /$bwCGIalias/coll_download.pl?$theURL&urlTest=yes\n\n";
	exit(0);	
} elsif ($params{selected} eq "HelpFrequency") {
	print "Location: $bwServer/$bwHTMLalias/manual/bncwebman-frequency.htm\n\n";
	exit(0);
} elsif ($params{selected} eq "HelpLookup") {
	print "Location: $bwServer/$bwHTMLalias/manual/bncwebman-lookup.htm\n\n";
	exit(0);
} elsif ($params{selected} eq "HelpAfter") {
	print "Location: $bwServer/$bwHTMLalias/manual/bncwebman-after.htm\n\n";
	exit(0);
} elsif ($params{selected} eq "HelpDelete") {
	print "Location: $bwServer/$bwHTMLalias/manual/bncwebman-deletehits.htm\n\n";
	exit(0);
} elsif ($params{selected} eq "updatesettings") {
	
	$username = $ENV{'REMOTE_USER'};
	
	$bncHandle = DBI->connect("DBI:mysql:$bwMYSQLtable:$bwMYSQLhost:$bwMysqlPort", "$bwMysqlUser", "$bwMysqlPwd") or bark("Can't connect to mysql on $bwMYSQLhost at port: $bwMysqlPort <BR>$bncHandle::errstr\n");
	
	&setParam("markup", "1") unless ($params{markup});
	
	$sqlQuery = "UPDATE settings SET view='$params{view}', showRandom='$params{showRandom}', historyView='$params{historyView}', markup=$params{markup}, disp_tags=$params{disp_tags}, disp_color=$params{disp_color}, disp_lines=$params{disp_lines}, show_value='$params{show_value}', crlf=$params{crlf}, statType=$params{stat_method}, delRare=$params{delRare}, totBNC=$params{totBNC}, `range`=$params{range}, minusrange=$params{minusrange} WHERE user='$username'";
	
	$bncHandle->do($sqlQuery) or bark("Can't update $sqlQuery:" . $bncHandle::errstr);
	
	print "Location: /$bwCGIalias/BNCquery.pl?theQuery=search&urlTest=yes\n\n";
	exit(0);
} else {



	setParam("theData", &encodeForm($params{theData}));

	print "Content-type: text/html\n\n";
	
	if ($params{selected} eq "Thin...") {
		$params{thMode}=~m/\AM?(\d+)(#(\d+))?/;
		setParam("numOfFiles", "$3");
	
		$select ="<SELECT NAME=\"thMode\">
				<OPTION VALUE=1 SELECTED>random (selection is reproducible)
				<OPTION VALUE=2>random (selection is not reproducible)
		</SELECT>";
		$display = "";
	
		
		print <<END;
	
		<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
		<HTML>
			<HEAD>
	
				<TITLE>Thinning options</TITLE>
	
				<META NAME="generator" CONTENT="BBEdit 4.5">
				<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
			</HEAD>
			<BODY BGCOLOR="#FFFFFF">
	
			<TABLE BORDER="0" WIDTH="100%" CELLPADDING=3 CELLSPACING=2>
				<TR>
					<TD HEIGHT="27" COLSPAN="5" BGCOLOR="#CCCCFF"> <FONT SIZE="4"><B>BNC Thinning Options</B></FONT></TD>
				</TR>
END

		print <<END;
				$display
				<FORM ACTION="/$bwCGIalias/main.pl">
				<TR>
					<TD HEIGHT="50" WIDTH="20%">
						Query thinning:
					</TD>
					<TD HEIGHT="50" WIDTH="20%">
							<INPUT TYPE="hidden" NAME="chunk" VALUE="1">
							<INPUT TYPE="hidden" NAME="inst" VALUE="$params{inst}">
							<INPUT TYPE="hidden" NAME="max" VALUE="INIT">
							<INPUT TYPE="hidden" NAME="qname" VALUE="$params{qname}">
							<INPUT TYPE="hidden" NAME="queryID" VALUE="$params{queryID}">
							<INPUT TYPE="hidden" NAME="oldthMode" VALUE="$params{thMode}">
							<INPUT TYPE="hidden" NAME="numOfFiles" VALUE="$params{numOfFiles}">
							<INPUT TYPE="hidden" NAME="theData" VALUE="$params{theData}">
							<INPUT TYPE="hidden" NAME="warned" VALUE="$params{warned}">
							<INPUT TYPE="hidden" NAME="numOfSolutions" VALUE="$params{numOfSolutions}">
							<INPUT TYPE="hidden" NAME="view" VALUE="$params{view}">
							<INPUT TYPE="hidden" NAME="dbname" VALUE="$params{dbname}">
							<INPUT TYPE="hidden" NAME="SQL" VALUE="$params{SQL}">
							<INPUT TYPE="hidden" NAME="letter" VALUE="$params{letter}">
							<INPUT TYPE="hidden" NAME="tag" VALUE="$params{tag}">
							<INPUT TYPE="hidden" NAME="ot" VALUE="$params{ot}">
							<INPUT TYPE="hidden" NAME="display" VALUE="$params{display}">
							<INPUT TYPE="hidden" NAME="sortpos" VALUE="$params{sortpos}">
							<INPUT TYPE="hidden" NAME="exclude" VALUE="$params{exclude}">
							<INPUT TYPE="hidden" NAME="theID" VALUE="$params{theID}">
							<INPUT TYPE="hidden" NAME="dump" VALUE="$params{dump}">
	
							$select
					</TD>
					<TD WIDTH="20">&nbsp;</TD>
					<TD HEIGHT="50">
						<INPUT TYPE="text" NAME="thin" VALUE="$thin" SIZE=8>
						<INPUT TYPE="hidden" NAME="qtype" VALUE="$params{qType}">
						(number of instances or percentage)
					</TD>
					<TD HEIGHT="50">
						<INPUT TYPE="hidden" NAME="urlTest" VALUE="yes">
						<INPUT TYPE="submit" VALUE="Submit">
					</TD>
				</TR>
				</FORM>
			</TABLE>
		
END
		&printFooter();
		exit(0);
	
	}
	print <<END;
	
	
	</BODY>
	</HTML>

END

}

&disconnect();
exit(0);
