#!/bin/sh

# this shell script will try (when run inside old docman directory) to
# migrate docman v1.x installation to new v2.x directory layout
#
# 2003-06-11 Dobrica Pavlinusic <dpavlin@rot13.org>

dir=`pwd`

#
# try to guess docman installation directory from this script path
#

D=`dirname "$0"`
docman2_dir="`cd \"$D\" 2>/dev/null && pwd || echo \"$D\"`"
docman2_realm="$docman2_dir/realm"

if [ ! -e "$docman2_realm" -o ! -e "$docman2_dir/docman.php" ] ; then
	echo "FATAL: You should start this script using full path to"
	echo "docman installation directory and not from some other"
	echo "path!! (e.g. /home/httpd/docman2/`basename $0`)"
	exit 1
fi

#
# try to find .htusers and .docman.conf which should always exists in
# valid existing repository
#

if [ ! -e "$dir/.htusers" ] ; then
	echo "FATAL: can't find $dir/.htusers file... aborting automatic migration"
	echo "(is this really docman 1.x site directory?)"
	exit 1
fi

if [ ! -e "$dir/.docman.conf" ] ; then
	echo "FATAL: can't find $dir/.docman.conf file... aborting automatic migration"
	echo "(is this really docman 1.x site directory?)"
	exit 1
fi

#
# try to guess existing DirectoryIndex (docman.php)
#

if [ -e "index.php" ] ; then
	docman_php="index.php"
elif [ -e ".index.php" ] ; then
	docman_php=".index.php"
elif [ -e "docman.php" ] ; then
	docman_php="docman.php"
else
	echo "can't find docman.php in current directory,"
	echo -n "please enter it's name: "
	read docman_php
	if [ ! -e "$docman_php" ] ; then
		echo "FATAL: $docman_php doesn't exits!"
		exit 1
	fi
fi

#
# get some version numbers
#

docman1_ver=`grep "gblVersion *=" $docman_php | cut -d\" -f2`
# convert dir to absolute path
docman2_ver=`grep "gblVersion *=" $docman2_dir/docman.php | cut -d\" -f2`

#
# do some sanity checks
#

if [ ! -e "$docman2_realm" ] ; then
	echo "can't guess docman2 realm dir (it should be $docman2_realm),"
	echo -n "enter it here: "
	read $docman2_realm
fi

if [ ! -w "$docman2_realm" ] ; then
	echo "FATAL: $docman2_realm is not writable by current user..."
	echo "(this is needed to create configuration files there)"
	exit 1
fi

if [ ! -w "$dir" -a ! -w "$dir/.html" ] ; then
	echo "FATAL: $dir is not writable by current user..."
	echo "(we needed to create new DocumentRoot dir $dir/.html; you can create"
	echo "$dir/.html directory writable to current user manually)"
	exit 1
fi


#
# begin real move
#

d=`hostname -f`
echo "upgrading docman repository $dir from $docman1_ver to $docman2_ver..."
echo -n "docman2 virtual hostname [$d]: "
read http_virtual_host

if [ -z "$http_virtual_host" ] ; then
	http_virtual_host=$d
fi

#
# migrate docman.conf
#

f="$docman2_realm/$http_virtual_host.conf"
echo "creating $f"
#
# remove gblIncDir, fix gblUsers
#
cat $dir/.docman.conf | sed \
	-e 's#\(\$gblIncDir\)#// -- removed in migration -- \1#g' \
	-e 's#\(\$gblUsers.*\)htusers_#\1#' \
	-e 's#?>#\
\
//	added by migration script to create valid configuration\
	\$gblRepositoryDir = "'$dir'";\
\
?>#' > $f

#
# migrate htusers
#
f="$docman2_realm/$http_virtual_host.htusers"
echo "creating $f"
cat $dir/.htusers > $f

#
# copy template trustee file from distribution
#
f="$docman2_realm/$http_virtual_host.trustee"
echo "creating $f"
cat $docman2_realm/localhost.trustee.dist > $f
#
# add some permissions
#
echo "#
# this will give all permissions to all users (to emulate docman 1.x
# behaviour) -- you *WILL WANT* to change this!
/:*:RWB
#" >> $f

#
# now, create new .html directory which will be DocumentRoot
#
echo "creating $dir/.html -- new DocumentRoot"
mkdir $dir/.html
ln -sf $docman2_dir/docman.php $dir/.html/index.php
ln -sf $docman2_dir/html/docman.css $dir/.html/docman.css
echo
echo "!!!!!!!!!!!!!!!!!"
echo "!!! IMPORTANT !!!"
echo "!!!!!!!!!!!!!!!!!"
echo
echo "You will have to change apache's httpd.conf for"
echo "virtual host $http_virtual_host as follows:"
echo
echo "	DocumentRoot $dir/.html"
echo "	DirectoryIndex index.php"
echo
echo -n "remove old $docman1_ver files (new version are in $docman2_realm)? [y]: "
read rm_old
if [ "$rm_old" = "y" -o -z "$rm_old" ] ; then
	rm $dir/$docman_php $dir/.docman.conf $dir/.htusers
else
	echo "remove files manually: $dir/$docman_php $dir/.docman.conf $dir/.htusers"
fi
echo "migration done..."
