#!/bin/sh
# If you're insane enough to put this on a live server you deserve what you get

if [ -n "${1}" ]; then
  node=$1
else
  node="default"
fi

if [ -f /var/www/shwikidataOSDc/$node ]; then
  content=`cat /var/www/shwikidataOSDc/$node`
fi
  
echo "Content-type: text/html"
echo ""

echo "<html><body>"
echo "<h1>SHWiki</h1>"
echo "<h2>$node</h2>"

STDIN=$(/bin/cat)
if [ -n "${STDIN}" ]; then
  QUERY_STRING="${STDIN}&${QUERY_STRING}"
  for paramvalue in $(/bin/echo ${QUERY_STRING} | /usr/bin/tr ";&" "\012") ; do
   param=${paramvalue%%=*}
   value=${paramvalue#*=}
   if [ $param == "content" ]; then
     value=$(/bin/echo ${value} | /usr/bin/tr "+" " ")
     eval "export '${param}=$value'"
     echo "$content" > /var/www/shwikidataOSDc/$node
   fi
  done
fi

output=`echo "$content" | sed -e "s/[A-Z][a-z]\+[A-Z]\S\+/<a href=\"?&\">&<\/a>/"`
echo "$output<hr>"

echo "<form method=\"post\"><textarea name=\"content\">$content</textarea><br /><input type=\"submit\" value=\"save\"></form>"

echo "</body></html>"
exit 0

