#!/bin/sh
##############################################
#
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# WARNING! This script does not check its inputs and is
# therefore completely UNSAFE!
# Do not run it on any webserver reachable via a network!
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#
# Extracts wikipedia index files for Javascript-based titel
# search.
# To be used on data created with wiki2staticz and wiki-tar.
# 
# Author: Markus Baumeister (markus@spampit.retsiemuab.de)
#
# Version: 0.1
#
# More info on http://www.retsiemuab.de/wiki2zaurus
#
####################################################


# disable filename globbing
set -f

#source wpg-cgi.conf
export lang="en"
export wikiurlpath="/wiki/${lang}"
export wikifilepath="/mnt/cf/www/wiki/${lang}"
#export wikifilepath="/srv/www/htdocs/wiki/${lang}"
export wikicharset="iso-8859-1"
cd ${wikifilepath}/search

# Transmit first part of javascript

echo Content-type: text/html
echo

cat search1.ht

# find and extract correct javascript index

#echo argc is $#. argv is "$*".
#echo $QUERY_STRING
arg=` echo $QUERY_STRING | cut -b8- `
#echo $arg
# we have to DumpDown() the search term here. Alas it doesn't work
# so easily because web browsers transmit umlauts in %E4 notation.
arg=`echo $arg | awk -- "{ a=tolower(\\$0); gsub(\"|||\",\"ae\", a ); gsub(\"|\",\"oe\", a); gsub(\"|\", \"ue\", a); gsub(\"\", \"ss\", a); gsub(\"[^a-zA-Z0-9]\", \"_\", a); gsub(\"[]\", \"a\", a); gsub(\"|\",\"c\",a); gsub(\"[]\",\"e\",a); gsub(\"[]\",\"i\",a); gsub(\"\", \"d\",a); gsub(\"|\",\"n\",a); gsub(\"[]\",\"o\",a); gsub(\"[]\",\"u\",a); gsub(\"[]\",\"y\",a);  print a }"`
len=${#arg}
#echo $arg
pathp1=`echo $arg | cut -b-1 `
if expr $len \< 2 >/dev/null; then
  pathp2=${pathp1}${pathp1}; 
else
  pathp2=` echo $arg | cut -b-2 `
fi

echo "var searchterm=\"${arg}\";"

indexname="${pathp2}.js"

tar xzOf ${pathp1}.tgz $indexname

# transmit the second part of javascript

cat search2.ht


