#!/bin/sh

Die () { # [Message [Error_code]]
  test "${2:-2}" -lt 1 || echo -n "$Usage
ERROR: " >&2
  test -z "$1" || echo "${1:-UNKNOWN}" >&2
  test "${2:-2}" -lt 0 || exit "${2:-2}"
}

# APACHE	AUid:AGid
# USER		UUid:UGid
#
# Instance 	AUid:UGid


case `uname` in
  Linux) APACHECFG="/etc/httpd/conf"
  	 MOINSHARE="/usr/share/moin"
  	 AUid=apache; AGid=apache
	 Service="service httpd"
  ;;
  FreeBSD) APACHECFG="/usr/local/etc/apache"
	   MOINSHARE="/usr/local/share/moin"
  	   AUid=www; AGid=www
	   Service="/usr/local/etc/rc.d/apache.sh"
  ;;
  *)	# XXX auto-detection?
  	Die "Unknown operating system"
  ;;
esac
VERBOSE=0
REMOVE=0
HELP=""
LIST=""
WIKIS=/var/www/wiki
OPT="li:u:g:w:dRvh"
Usage="Usage: `basename $0` [-$OPT] Instance [Group]

Creates a Moin-Moin instance directory tree and apache config file.
Apache must run as user $AUid, group $AGid.

Options:
-l		List instances already installed (DEFAULT)
-i NAME		Instance name is NAME
-u UID		Instance owned by primary group of user UID
-g GID		Instance owned by group GID
-w PATH		Instance root directory is PATH
-R		REMOVE ALL INSTANCE DATA AND APACHE CONFIG
-d		Dummy run (don't touch anything)
-v		Increase verbosity
-h		Display this help message
"

while getopts "$OPT" opt
do
  test "$VERBOSE" -lt 2 || echo "Processeing '$opt/$OPTARG' option" >&2
  case "$opt" in
    i) INST="$OPTARG" ;;
    u) UUid="$OPTARG" ;;
    g) UGid="$OPTARG" ;;
    w) WIKIS="$OPTARG" ;;
    d) DUMMY=1;;
    R) REMOVE=1;;
    l) LIST=1;;
    v) VERBOSE=$(($VERBOSE+1)) ;;
    h) HELP=1;;
    \?) shift $(($OPTIND-1))
        Die "Unknown command $OPTIND line '$*'"
	;;
  esac
done
shift $(($OPTIND-1))
test -n "$INST" || INST="$1"
test -n "$INST" || LIST=1
test -n "$UUid" || UUid="root"
test -n "$UGid" || UGid="${2:-`id -g $UUid`}"

APACHADD="$APACHECFG/addon-modules.d"
HOSTNAME=`hostname || echo "YOURSITE"`
ADDAPACHE="$APACHADD/moin-$INST.conf"
DESTDIR="$WIKIS/$INST"

test -d "$APACHECFG" || Die "Apache config directory '$APACHECFG' not found"

test "$(($VERBOSE+$REMOVE))" -lt 1 || Die "Apache: $AUid:$AGid, config $ADDAPACHE
Group: $UGid
Moin: $MOINSHARE
Host: $HOSTNAME
Wiki: $DESTDIR" -1

test -z "$HELP" || Die "$Usage" 0

test -z "$LIST" || { # TODO config analysis
  echo "
	Config files:"
  ls -1 "$APACHADD/"moin-*.conf
  echo "
  	Instance directories:"
  ls -d1 "$WIKIS/"*
  exit 0
}

test "$REMOVE" -lt 1 || {
  echo -n "
Remove $DESTDIR
Remove $ADDAPACHE

Are you sure [y/N]? "
  read a; test "$a" = "y" || Die "Cool, let it live." 0
  echo -n "REALLY REMOVE [y/N]? "
  read a; test "$a" = "y" || Die "Cool, let it live." 0
}  
test -z "$DUMMY" || Die "Dummy mode finished" 0
test "$REMOVE" -lt 1 || { rm -rf $DESTDIR $ADDAPACHE; $Service restart; Die "Removed" 0; }
test -d "$DESTDIR" && Die "Directory '$DESTDIR' already exists!"
test -d "$WIKIS" || { mkdir -p $WIKIS; chmod 755 $WIKIS; }

test -d "$APACHADD" || { Die "Creating '$APACHADD'" -1; mkdir "$APACHADD" || Die "Oops. Cannot create."; }
mkdir -p $DESTDIR/cgi-bin
cp -r $MOINSHARE/data $DESTDIR
cp -r $MOINSHARE/underlay $DESTDIR
cp -r $MOINSHARE/server/moin.cgi $DESTDIR/cgi-bin
cp -r $MOINSHARE/config/wikiconfig.py $DESTDIR/cgi-bin
chown -R $AUid:$UGid $DESTDIR/
chmod -R g+w $DESTDIR/cgi-bin $DESTDIR/data/plugin

cat > "$ADDAPACHE" <<EOF
ScriptAlias /$INST "$DESTDIR/cgi-bin/moin.cgi"
<Directory "$DESTDIR/cgi-bin/">
    Options Indexes FollowSymLinks ExecCGI
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>
# To use mod_pyton disable all above, enable the following lines and
# also change wikiconfig.py to have absolute paths for data/underlay dirs.
#<Location /$INST>
#    SetHandler python-program
#    PythonPath "['$DESTDIR/cgi-bin'] + sys.path"
#    PythonHandler MoinMoin.request::RequestModPy.run
#</Location>
Alias /wiki/ "$MOINSHARE/htdocs/"
<Directory "$MOINSHARE/htdocs/">
    Options Indexes FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>
EOF

# patch (un)hacked installation
sed -i~ "
/data_dir = '[.][/]data/s/'[.]/'../
/data_underlay_dir = '[.][/]underlay/s/'[.]/'../
" $DESTDIR/cgi-bin/wikiconfig.py

rm -f $DESTDIR/cgi-bin/*~

$Service restart

echo "
Moin-Moin $INST installation is finished:
	Wiki pages: $DESTDIR
	Wiki url: http://$HOSTNAME/$INST
	Additional Apache config file: $ADDAPACHE
Edit $DESTDIR/cgi-bin/wikiconfig.py to set your site up.
"
