#!/bin/sh
#
# BEGIN COPYRIGHT BLOCK
# This Program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; version 2 of the License.
# 
# This Program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along with
# this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA 02111-1307 USA.
# 
# In addition, as a special exception, Red Hat, Inc. gives You the additional
# right to link the code of this Program with code not covered under the GNU
# General Public License ("Non-GPL Code") and to distribute linked combinations
# including the two, subject to the limitations in this paragraph. Non-GPL Code
# permitted under this exception must only link to the code of this Program
# through those well defined interfaces identified in the file named EXCEPTION
# found in the source code files (the "Approved Interfaces"). The files of
# Non-GPL Code may instantiate templates or use macros or inline functions from
# the Approved Interfaces without causing the resulting work to be covered by
# the GNU General Public License. Only Red Hat, Inc. may make changes or
# additions to the list of Approved Interfaces. You must obey the GNU General
# Public License in all respects for all of the Program code and other code used
# in conjunction with the Program except the Non-GPL Code covered by this
# exception. If you modify this file, you may extend this exception to your
# version of the file, but you are not obligated to do so. If you do not wish to
# provide this exception without modification, you must delete this exception
# statement from your version and license this file solely under the GPL without
# exception. 
# 
# 
# Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
# Copyright (C) 2005 Red Hat, Inc.
# All rights reserved.
# END COPYRIGHT BLOCK
#
# This file essentially serves as a wrapper around the instance
# creation and configuration program (index) since it must be run from
# the directory it lives in due to run time shared library
# dependencies

sroot=`echo $0 | sed s#/bin/slapd/admin/bin/.\*##g`
PERL=perl # just use perl from PATH

#        run_ds_create "$sroot" "$dir" "$inffile"
run_ds_create()
{
    sroot="$1"
    shift
    dir="$1"
    shift
    inffile="$1"

    # the inffile is incomplete - it needs the instance specific stuff
    dseldif=$sroot/$dir/config/dse.ldif
    port=`grep \^nsslapd-port: $dseldif | sed -e s/\^nsslapd-port:[\ \	]*//`
    rootdn=`grep \^nsslapd-rootdn: $dseldif | sed -e s/\^nsslapd-rootdn:[\ \	]*//`
    id=`echo $dir | sed -e s/^slapd-//`
    suffix="o=NetscapeRoot"

    realfile=/tmp/fds$$.inf
    cp $inffile $realfile
    echo "" >> $realfile
    echo "[slapd]" >> $realfile
    echo "ServerIdentifier= $id" >> $realfile
    echo "ServerPort= $port" >> $realfile
    echo "RootDN= $rootdn" >> $realfile
    # dummy password
    echo "RootDNPwd= password" >> $realfile
    echo "Suffix= $suffix" >> $realfile

    cwd=`pwd`
    cd `dirname $0`

    ./ds_create -r -f $realfile

    rm -f $realfile
	cd $cwd
}

start_server()
{
	NETSITE_ROOT=$1
	REQUEST_METHOD=GET 
	export NETSITE_ROOT REQUEST_METHOD
	QUERY_STRING="InstanceName=$2"
	SERVER_NAMES=$2
	export QUERY_STRING SERVER_NAMES
	cwd=`pwd`
	cd `dirname $0`
	./start 2>&1 | grep -v '^Content' | grep -v '^ds_'
	cd $cwd
}

# get the inf file name
seenfarg=
inffile=

# if the -r flag is present, this means we're doing a
# reinstall or an upgrade, so restart the servers
for arg in "$@" ; do
	if [ "$arg" = "-r" ]; then
		reconfig=1
	fi
	if [ "$arg" = "-S" ]; then
                iDSISolaris=1
        fi
    if [ $seenfarg ]; then
        inffile="$arg"
        seenfarg=
    fi
    if [ "$arg" = "-f" ]; then
        seenfarg=1
    fi
done

extraflags=
# For Solaris 9+ specific installation, the following 
# method of determining reconfiguration by checking 
# the existence of <sroot>/slapd-nickname/config
# directory cannot be used. This is because the
# server_root is the same for all instances. Also, it
# uses standard Solaris supplied perl.
if [ "$iDSISolaris" = "" ]; then 
	# for some reason, we are not being passed the -r flag during
	# reinstall; so, determine if there are existing server
	# instances in this server root, and assume -r if there
	# are
	if [ "$reconfig" = "" ]; then
		cd $sroot
		for dir in slapd-* ; do
			if [ -d $dir/config ]; then
				reconfig=1
				extraflags="-r"
				break
			fi
		done
	fi
fi

if [ $reconfig ] ; then
	cd $sroot
	for dir in slapd-* ; do
		# first, fix any old password files
		if [ -d alias ]; then
			cd alias
			echo Converting $dir to new format password file . . .
			$PERL $sroot/bin/slapd/admin/bin/migratePwdFile $sroot $dir
			cd ..
		fi
		# Copy new schema ldiffiles
		echo Copying new schema ldiffiles . . .
		$PERL $sroot/bin/slapd/admin/bin/upgradeServer $sroot $dir

		# next, start the server
		echo Starting $dir . . .
		start_server $sroot $dir
		echo ""

        # more reconfiguration
        run_ds_create "$sroot" "$dir" "$inffile" "$extraflags" "$@"

	done
	# fix any non-instance specific files - omit server instance argument
	$PERL $sroot/bin/slapd/admin/bin/upgradeServer $sroot
fi

cd `dirname $0`

# we need to make sure the alias directory is owned by the server user/group
# in order for the server to be able to create the initial key/cert db files

if [ ! -f "$inffile" ] ; then
	echo "WARNING: Could not read the setup info file $inffile"
else
# grab the ssuser and ssgrp
	ssuser=`grep SuiteSpotUserID $inffile | sed -e 's/SuiteSpotUserID[ 	]*=[ 	]*//'`
	ssgrp=`grep SuiteSpotGroup $inffile | sed -e 's/SuiteSpotGroup[ 	]*=[ 	]*//'`
#	echo "user = $ssuser group = $ssgrp"
# chown the alias directory
	chown $ssuser:$ssgrp $sroot/alias
fi

rc=0
# not reconfig - run ds_create as usual
if test -z "$reconfig" ; then
    ./ds_create $* $extraflags
fi
rc=$?

# chown the cookie directory - bug 175098
if [ "$ssuser" ] ; then
    if [ "$ssgrp" ] ; then
        chown $ssuser:$ssgrp $sroot/bin/slapd/authck
    else
        chown $ssuser $sroot/bin/slapd/authck
    fi
fi

exit $rc
