#!/bin/bash
########################################################################
# Configuration of MoinMoin under Apache.
#   * You can run this to do your initial configuration, or just to add
#     a new instance.  
#   * You better be root when you run this.
#   * Creates:
#     - http://your-server/$MOININSTANCE (CGI access to your wiki)
#     - http://your-server/mod-$MOININSTANCE (mod_python access via wrapper)
#     - http://your-server/newmod-$MOININSTANCE (mod_python access w/o wrapper)
#
# Customize these to suit your site:
#   MOININSTANCE	the name you want given to your new instance.
#   MOINSHARE		where did setup.py put your moin shared files?
#   MOINWIKIS		the directory that will contain all of your wiki instances
#   APACHEDIR		the name of the directory that *contains* your apache conf directory.
#   PYSITE		the path name of your python site-packages dir.
#
########################################################################
export MOININSTANCE=mywiki
export MOINSHARE=/usr/share/moin
export MOINWIKIS=/usr/local/wikis
export APACHEDIR=/usr/local/apache
export PYSITE=/usr/lib/python2.3/site-packages


########################################################################
# 
########################################################################
export MMINST=$MOINWIKIS/$MOININSTANCE
export MYCOMMENT="MoinMoin 1.3.1 shared docs - kpk setup"



########################################################################
echo "Checking the stuff you do once, before you can have any instances."
########################################################################
if test -d $MOINWIKIS ; then
  echo "I see you already have created $MOINWIKIS"
else
  echo "Creating $MOINWIKIS"
  mkdir $MOINWIKIS
fi

grep "$MYCOMMENT" $APACHEDIR/conf/httpd.conf > /dev/null
export TMPX=$?
if test $TMPX -eq 0 ; then
  echo "I see you have already configured Apache for non-instance specific."
else
  echo "Appending Alias for master share to $APACHEDIR/conf/httpd.conf"
cat <<EOF >> $APACHEDIR/conf/httpd.conf
  # $MYCOMMENT
  Alias /wiki/ "$MOINSHARE/htdocs/"
EOF
fi


########################################################################
echo "Starting the moin stuff you do for each instance."
########################################################################
if test -d $MMINST ; then
  echo "Found existing $MMINST"
else
  mkdir $MMINST
fi

if test -d $MMINST/cgi-bin ; then
  echo "Found existing $MMINST/cgi-bin"
else
  mkdir $MMINST/cgi-bin
fi

cp -R $MOINSHARE/data $MMINST
cp -R $MOINSHARE/underlay $MMINST
cp $MOINSHARE/config/wikiconfig.py $MMINST
cp $MOINSHARE/server/moinmodpy.py $MMINST/cgi-bin

echo "updating wikiconfig.py"
cat <<EOF0 >> $MMINST/wikiconfig.py
    data_dir = '$MMINST/data/'
    data_underlay_dir = '$MMINST/underlay/'
EOF0

echo "Creating moin.cgi"
cat <<EOF1 > $MMINST/cgi-bin/moin.cgi
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
import sys
sys.path.insert(0, '$MMINST')
from MoinMoin.request import RequestCGI
request = RequestCGI()
request.run()
EOF1

echo "Setting permissions"
chown -R apache.apache $MMINST
chmod ug+x $MMINST/cgi-bin/moin.cgi
chmod -R ug+rwX $MMINST
chmod -R o-rwx $MMINST

echo "Appending to httpd.conf. If you like what you had, delete the obviously appended lines"
echo "CGI via /$MOININSTANCE"
echo "wrapped mod_python via /mod-$MOININSTANCE"
echo "straight mod_python via /newmod-$MOININSTANCE"
cat <<EOF2 >> $APACHEDIR/conf/httpd.conf
  ############### obviously appended lines #############
  ScriptAlias /$MOININSTANCE "$MMINST/cgi-bin/moin.cgi"
  <Location /mod-$MOININSTANCE>
    SetHandler python-program
    PythonPath "['$MMINST/cgi-bin','$MMINST']+sys.path"
    PythonHandler moinmodpy
  </Location>
  <Location /newmod-$MOININSTANCE>
    SetHandler python-program
    PythonPath "['$PYSITE','$MMINST']+sys.path"
    PythonHandler MoinMoin.request::RequestModPy.run
  </Location>
EOF2

########################################################################
# Finish up.
########################################################################
echo "Restarting apache"
apachectl restart
echo "Finished"
