#!/bin/bash
#newwiki.sh - Script to create a new wiki instance.
#v-0.75
#Created by Matt Lorimer matt.lorimer@usu.edu
#Last edited on 04-07-2005

#Function to display available options you can pass to this script
function ShowOpts
{
	if [ $# -lt 1 ]
	then
	{
		echo "newwiki.sh WikiName [-f] [-l] [-u] [-g] [-p] [-s] [-h] [--help]"
		echo "   WikiName is the name of the wiki you are creating"
		echo "   -f --farm		Indicates this is part of a wiki farm (some"
		echo "			files are not copied) - default value is false"
		echo "   -l --location	Location you would like you wiki data stored"
		echo "			in - default value is current directory"
		echo "   -u 			User Apache runs under - default value is apache"
		echo "   -g			Group Apache runs under - default value is apache"
		echo "   -p --prefix		Prefix you used when installing moin - default"
		echo "			value is /usr (same as installer)"
		echo "   -s			Share directory for moin installation - default"
		echo "			value is \$PREFIX/share/moin (same as installer)"
		echo "   -q --quiet		Supress all output except errors"
		echo "   -v --verbose		Display information on progress"
		echo "   -h --help		Display this information"
		exit 1
	}
	fi
}

#function that actually creates the wiki
function CreateWiki
{
	#create the directory for this instance
	mkdir -p $WIKILOCATION/$INSTANCE

        if [ $VERBOSE = true ]
        then
                echo "Copying Share data to $WIKILOCATION/$INSTANCE/ ...."
        fi

	# Copy template data directory
	cp -r $SHARE/data $WIKILOCATION/$INSTANCE/

	if [ $FARM = true ]
	then
        	if [ $VERBOSE = true ]
        	then
        	        echo "Copying default wikiconfig to $WIKILOCATION/$INSTANCE.py..."
        	fi

		cp $SHARE/config/wikiconfig.py $WIKILOCATION/$INSTANCE.py

	        if [ $VERBOSE = true ]
	        then
	                echo "Setting permissions on $WIKILOCATION/$INSTANCE.py..."
	        fi

	        # Set permissions correctly on this file
		chown $USER:$GROUP $WIKILOCATION/$INSTANCE.py
	        chmod -R ug+rwX $WIKILOCATION/$INSTANCE
	        chmod -R o-rwx $WIKILOCATION/$INSTANCE
	else
	        if [ $VERBOSE = true ]
	        then
	                echo "Copying underlay directory and wikiconfig.py file to $WIKILOCATION/$INSTANCE/ ..."
	        fi

		# Copy underlay data directory -- Not necissary for additional wiki's with one install
		cp -r $SHARE/underlay $WIKILOCATION/$INSTANCE/

		# Copy wiki configuration sample file
		cp $SHARE/config/wikiconfig.py $WIKILOCATION/$INSTANCE
	fi

        if [ $VERBOSE = true ]
        then
                echo "Fixing Permissions on $WIKILOCATION/$INSTANCE/ ..."
        fi

	# Fix any ownership problems
	chown -R $USER.$GROUP $WIKILOCATION/$INSTANCE

	# Make apache user and group have read and write permissions and execute permissions where necissary
	chmod -R ug+rwX $WIKILOCATION/$INSTANCE

	# Nobody else needs to do anything with the files.
	chmod -R o-rwx $WIKILOCATION/$INSTANCE

	if [ $QUIET = false ]
	then
		echo "$INSTANCE created successfully.  You must edit your apache config file accordingly."
		if [ $FARM = true ]
		then
			echo "You must also edit $WIKILOCATION/$INSTANCE.py"
			echo "and your $WIKILOCATION/farmconfig.py file"
		else
			echo "You must also edit $WIKILOCATION/$INSTANCE/wikiconfig.py"
		fi
	fi
}

#Default values for the script

#Weather this is being added to a wiki farm or not
FARM=false

# Prefix you used when installing moinmoin
PREFIX=/usr

# If you didn't change this, this is correct.
SHARE=$PREFIX/share/moin

# Where your wiki data will be stored.
WIKILOCATION="./"

# User and group apache runs under
USER=apache
GROUP=apache

#Set quiet and verbose to both be off by default
QUIET=false
VERBOSE=false

#Time to parse out the
TEMP=`getopt -o fhvql:u:g:p:s: -l help,farm,quiet,verbose,prefix:,location: -- "$@"`

#Make sure they didn't pass any invalid arguments to the script
if [ $? != 0 ]
then
	#if they did, then show them what the options are and quit with an error code of 1
	ShowOpts
	exit 1
fi

# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"

while true ; do
	case "$1" in
		-h|--help) ShowOpts ; shift ;;
		-q|--quiet) QUIET=true ; shift ;;
		-v|--verbose) VERBOSE=true ; shift ;;
		-f|--farm) FARM=true ; shift ;;
		-l|--location) WIKILOCATION=$2 ; shift 2 ;;
		-u) USER=$2 ; shift 2 ;;
		-g) GROUP=$2 ; shift 2 ;;
		-p|--prefix) PREFIX=$2 ; shift 2 ;;
		-s) SHARE=$2 ; shift 2 ;;
		--) shift ; break ;;
		*) echo "Internal error!" ; exit 1 ;;
        esac
done

if [ $# -ne 1 ]
then
	echo "You specified $# values.  The command must only have one value"
	ShowOpts
	exit 1
fi

INSTANCE=$1

CreateWiki

