#!/bin/sh

usage() {
	cat <<-twiki_install_EOF
Shell script to automate some of the steps enumerated in
the Twiki-on-Windows-setup in
http://twiki.org/cgi-bin/view/Codev/WindowsInstallCookbook

This is still "in-progress" because I haven't gotten past
some CPAN problems, but the steps seem to produce the required
changes.  The final proof is yet to come (as of 30-Jun-2002).
See NOT_DONE below.

Assumes that you have:
(1) performed the instructions up to section titled
	"5. Test Cygwin" under the main header "Installing Components"
(2) put the same domain in for the Apache ServerAdmin
	as the Apache server during the Apache install

chrisNO_SP!Mkeith@aol.com
http://home.pacbell.net/c_keith
twiki_install_EOF
}

# Use this parameter to see each command as it is executed,
# for debugging or monitoring.
if [ "$1" = "-x" ] ; then
	set -x
fi
#---------------------------------------------------------------------
# Function to execute a command and check the return code.  Return the
# output in case we need to analyze it for any reason.
execAndTest()
{
	out=`$1`
	status=$?
	if [ $status -ne 0 ] ; then
		echo "$0: $1: failed with error code: $status"
		exit 1
	fi
	echo "$out"
}

#---------------------------------------------------------------------
# Function to edit a file "in place".  Leaves a backup file for for any
# post-mortem analysis.
sedInPlace()
{
	set -x
	fn_="$1"
	oldText_="$2"
	newText_="$3"
	sep_="$4"
	if [ "$sep_" = "" ] ; then
		sep_=/
	fi
	tmp_="${fn_}.tmp"
	cp -f "$fn_" "${fn_}.bak" ;		if [ $? -ne 0 ] ; then exit 1 ; fi
	rm -rf "$tmp_" ;				if [ $? -ne 0 ] ; then exit 1 ; fi
	sed -e s${sep_}"$oldText_"${sep_}"$newText_"${sep_}g "$fn_" > "$tmp_"
									if [ $? -ne 0 ] ; then exit 1 ; fi
	mv "$tmp_" "$fn_" ;				if [ $? -ne 0 ] ; then exit 1 ; fi
}

#---------------------------------------------------------------------
# If the user name has spaces, then cpan messes up when trying to save
# stuff to the $HOME directory.
if [ "$USER" = "" ] ; then
	echo "$0: USER environment variable not set"
	exit 1;
fi
case $USER in
	*" "* ) echo "$0: You cannot have spaces in your user name:"
			echo "$USER"
			exit 1;
			;;
esac

#---------------------------------------------------------------------
# This error-checking may seem like overkill, but I strongly believe
# "first, do no harm".  I collect the output into a variable so that I
# can analyze it later (if necessary), and it doesn't spew out on the
# screen.

# Create necessary directories and mounts.
# TBD: Could eventually check that we have the correct versions by
# using "expr" to pick out the version number and comparing it.

v=`execAndTest "rcs -V"` ;		if [ $? -ne 0 ] ; then exit 1 ; fi
v=`execAndTest "perl -v"` ;		if [ $? -ne 0 ] ; then exit 1 ; fi

v=`mkdir -p /twiki` ;			if [ $? -ne 0 ] ; then exit 1 ; fi
v=`mkdir -p /c` ;				if [ $? -ne 0 ] ; then exit 1 ; fi
v=`mkdir -p c:/twiki` ;			if [ $? -ne 0 ] ; then exit 1 ; fi
v=`mount -b -s c:/twiki /twiki`
								if [ $? -ne 0 ] ; then exit 1 ; fi
v=`mount -b -s c:/ /c` ;		if [ $? -ne 0 ] ; then exit 1 ; fi
v=`mount -b -c /cygdrive` ;		if [ $? -ne 0 ] ; then exit 1 ; fi

lc_1=`mount | wc -l`
lc_2=`mount | grep binmode | wc -l`
if [ "$lc_1" -ne "$lc_2" ] ; then
	echo "$0: Not all mounts are binmode"
	mount
	exit 1
fi

# Note: Can't capture output of "cd" because that runs it in another
# shell, and then the following commands don't take place in the right
# directory.

cd /twiki ;						if [ $? -ne 0 ] ; then exit 1 ; fi
v=`echo hi > t` ;				if [ $? -ne 0 ] ; then exit 1 ; fi
if [ "`cat t`" != "hi" ] ; then
	echo "$0: binary mode not set!"
	exit 1
fi
v=`rm t` ;						if [ $? -ne 0 ] ; then exit 1 ; fi
v=`mkdir c:/temp` ;				if [ $? -ne 0 ] ; then exit 1 ; fi


#---------------------------------------------------------------------
echo "Please put Twiki Zip file into C:/twiki."
echo "Press the Enter key to continue."
read a ;						if [ $? -ne 0 ] ; then exit 1 ; fi

cd c:/twiki ;					if [ $? -ne 0 ] ; then exit 1 ; fi
twikiZip=`ls TWiki*zip` ;		if [ $? -ne 0 ] ; then exit 1 ; fi
if [ "$twikiZip" = "" ] ; then
	echo "$0: Cannot find any twiki zip files in C:/twiki, it contains:"
	ls
	exit ;
fi
unzip $twikiZip

#---------------------------------------------------------------------
# Edit the Apache conf file.
# I assume that the serveradmin value in the Apache conf file was set
# correctly when the user installed Apache, so I don't edit it.

apacheConf=c:/apache/conf/httpd.conf
if [ ! -f "$apacheConf" ] ; then
	echo "$0: No file: $apacheConf"
	exit 1
fi
sedInPlace $apacheConf "^DocumentRoot.*" \
			"DocumentRoot \"C:/twiki\"" +
								if [ $? -ne 0 ] ; then exit 1 ; fi
sedInPlace $apacheConf "^" "" +
								if [ $? -ne 0 ] ; then exit 1 ; fi

cat <<-append_to_apache_EOF >> $apacheConf

# Alias /twiki/ "C:/twiki/"
# ScriptAlias /twiki/bin/ "C:/twiki/bin/"

    # RD: Changed None to All in next line, to enable .htaccess
    AllowOverride All
    Allow From All
    Options  ExecCGI
    SetHandler cgi-script


# Environment setup required to run Apache as service or as a
# standalone process.

   # Adjust TZ for your server timezone, e.g. EST5EDT - put the non-daylight-savings
   # timezone code first (e.g. EST or GMT), followed by the number of hours that it's behind GMT 
   # during non-daylight-savings time (use '-5' for timezones in advance of GMT).
   SetEnv TZ GMT0BST
   SetEnv RCSINIT -x,v/
   # Adjust TEMP and TMP for your server and create directories if necessary
   SetEnv TEMP c:/temp
   SetEnv TMP c:/temp
   SetEnv LOGNAME system
   SetEnv HOME c:/twiki

append_to_apache_EOF

# TBD-ck: make this faster by doing an in-line Perl script that we write
# out and execute.  It should slurp the whole file into memory at once,
# look for the right line and insert the new stuff, then blat it back
# out to disk.

edited=
tmp="${apacheConf}_tmp"
rm -f "$tmp"
while read LINE
do
	if [ "$LINE" = "" ] ; then
		echo "$LINE" >> "$tmp"
	    echo "# TWiki setup - avoid renaming scripts" >> "$tmp"
		echo "AddHandler cgi-script ." >> "$tmp"
		edited=TRUE
	else
		echo "$LINE" >> "$tmp"
	fi
done < "$apacheConf"
if [ "$edited" != "TRUE" ] ; then
	echo "Unable to perform \"AddHandler cgi-script .\" edit!"
	exit 1
fi
mv "$tmp" "$apacheConf"


#---------------------------------------------------------------------
# Edit the Twiki conf file.

twikiConf="c:/twiki/lib/TWiki.cfg"
if [ ! -f "$twikiConf" ] ; then
	echo "$0: No file: $twikiConf"
	exit 1
fi

# Extract the domain from the Apache conf file to use in the Twiki conf file

domain=`grep ^ServerAdmin\.\* "$apacheConf"`
										if [ $? -ne 0 ] ; then exit 1 ; fi
domain=`expr "$domain" : '.*@\(.*\)` ;	if [ $? -ne 0 ] ; then exit 1 ; fi

sedInPlace "$twikiConf" "^\\\$wikiHomeUrl.*" \
			"\\\$wikiHomeUrl = \"http://${domain}/bin/view\";" +
								if [ $? -ne 0 ] ; then exit 1 ; fi
sedInPlace "$twikiConf" "^\\\$defaultUrlHost.*" \
			"\\\$defaultUrlHost = \"http://${domain}\";" +
								if [ $? -ne 0 ] ; then exit 1 ; fi
sedInPlace "$twikiConf" "^\\\$scriptUrlPath.*" \
			"\\\$scriptUrlPath = \"/bin\";" +
								if [ $? -ne 0 ] ; then exit 1 ; fi
sedInPlace "$twikiConf" "^\\\$pubUrlPath.*" \
			"\\\$pubUrlPath = \"/pub\";" +
								if [ $? -ne 0 ] ; then exit 1 ; fi
sedInPlace "$twikiConf" "^\\\$pubDir.*" \
			"\\\$pubDir = \"/twiki/pub\";" +
								if [ $? -ne 0 ] ; then exit 1 ; fi
sedInPlace "$twikiConf" "^\\\$templateDir.*" \
			"\\\$templateDir = \"/twiki/templates\";" +
								if [ $? -ne 0 ] ; then exit 1 ; fi
sedInPlace "$twikiConf" "^\\\$dataDir.*" \
			"\\\$dataDir = \"/twiki/data\";" +
								if [ $? -ne 0 ] ; then exit 1 ; fi
sedInPlace "$twikiConf" "^\\\$safeEnvPath.*" \
			"\\\$safeEnvPath = \"/bin\";" +
								if [ $? -ne 0 ] ; then exit 1 ; fi
sedInPlace "$twikiConf" "^\\\$rcsDir.*" \
			"\\\$rcsDir = \"c:/cygwin/bin\";" +
								if [ $? -ne 0 ] ; then exit 1 ; fi
sedInPlace "$twikiConf" "^\\\$egrepCmd.*" \
			"\\\$egrepCmd = \"/bin/grep\";" +
								if [ $? -ne 0 ] ; then exit 1 ; fi
sedInPlace "$twikiConf" "^\\\$fgrepCmd.*" \
			"\\\$fgrepCmd = \"/bin/grep\";" +
								if [ $? -ne 0 ] ; then exit 1 ; fi

#---------------------------------------------------------------------
# Edit the cgi scripts, misc files

cd /twiki/bin ;					if [ $? -ne 0 ] ; then exit 1 ; fi
v=`mkdir .backup` ;				if [ $? -ne 0 ] ; then exit 1 ; fi
v=`cp * .backup` ;				if [ $? -ne 0 ] ; then exit 1 ; fi
v=`perl -pi~ -e 's;#!/usr/bin/perl;#!c:/cygwin/bin/perl;' *[a-z]`
								if [ $? -ne 0 ] ; then exit 1 ; fi
v=`head -1 view` ;				if [ $? -ne 0 ] ; then exit 1 ; fi
case "$v" in
	*c:/cygwin/bin/perl* ) ;;
	* )	echo "Unsuccessful edit of cgi scripts" ; exit 1 ;;
esac

sedInPlace "/twiki/bin/register" "encode_base64" \
				"MIME::Base64::encode_base64"
								if [ $? -ne 0 ] ; then exit 1 ; fi

# NOT DONE. Exiting here because haven't gotten CPAN modules installed
# yet.
exit 1

#---------------------------------------------------------------------
echo "Please go back to the instructions and run \"cpan\""
echo "Then run the test in the instructions to get the Apache server's userid."
echo "Type the Apache server's userid and press the Enter key when you are done."
read userid ;					if [ $? -ne 0 ] ; then exit 1 ; fi

#---------------------------------------------------------------------
# Mass edit the rcs files

cd /twiki/data ;				if [ $? -ne 0 ] ; then exit 1 ; fi
v=`tar czvf all-files.tar.gz */*`
								if [ $? -ne 0 ] ; then exit 1 ; fi
v=`perl -pi~~~ -e 'NR <= 10 && s/nobody:/${userid}:/ ' */*,v`
								if [ $? -ne 0 ] ; then exit 1 ; fi
v=`grep 'strict;$' */*,v | grep -v ${userid}`
								if [ $? -ne 0 ] ; then exit 1 ; fi
if [ "$v" != "" ] ; then
	echo "Mass edit of rcs files failed:"
	echo "$v"
	exit 1
fi
v=`rm */*~~~`

echo "Finished Twiki on Windows setup"