#!/bin/sh
#
# Copyright (c) 1998, by Sun Microsystems, Inc.
# All rights reserved.
#
#pragma ident	"@(#)preinstall	1.15	99/09/24 SMI"

#
# This version number be kept in sync with the version in the request script
#
request_vers=3

if [ "$SUNWKR5CL_REQUEST_VERS" != "$request_vers" ]
then
        gettext "Answers out of date. Need to re-run pkgask on this package."
        echo
        echo
        exit 1
fi

: ${KRB5_CONF_REPLACE:?}

if [ ${KRB5_CONF_REPLACE} -eq 1 ]
then
	: ${KRB5_CONF_REALM:?}
	: ${KRB5_CONF_MASTER:?}
	: ${KRB5_CONF_SLAVES:?}
	: ${KRB5_CONF_HELP_URL:?}
fi

: ${WARN_CONF_REPLACE:?}
: ${LOGIN_PATH:?}
: ${LOGIN_SUPATH:?}

pname=SUNWkr5cl

from=${PKG_INSTALL_ROOT}/etc/krb5/krb5.conf
ofrom=${PKG_INSTALL_ROOT}/etc/krb5.conf
to=${from}-

if [ ! -d ${PKG_INSTALL_ROOT}/etc/krb5 ]
then
	echo creating ${PKG_INSTALL_ROOT}/etc/krb5 directory
	mkdir -p ${PKG_INSTALL_ROOT}/etc/krb5
	chown root:sys ${PKG_INSTALL_ROOT}/etc/krb5
	chmod 0555 ${PKG_INSTALL_ROOT}/etc/krb5
fi

if [ ! -d ${PKG_INSTALL_ROOT}/var/krb5 ]
then
	echo creating ${PKG_INSTALL_ROOT}/var/krb5 directory
	mkdir -p ${PKG_INSTALL_ROOT}/var/krb5
	chown root:sys ${PKG_INSTALL_ROOT}/var/krb5
	chmod 0755 ${PKG_INSTALL_ROOT}/var/krb5
fi

if [ -f $from ]
then
	if [ $KRB5_CONF_REPLACE -eq 1 ]
	then
	
		rm -f $to
		echo saving existing $from into $to
		mv $from $to
	fi 
else
	if [ -f $ofrom ]
	then
		echo
		echo
		echo Warning: $ofrom exists on this system.
		echo After installation, you may want to hand
		echo merge parts of $ofrom into $from.
		echo
		echo
	fi
fi

from=${PKG_INSTALL_ROOT}/etc/krb5/warn.conf
to=${from}-
if [ -f $from ]
then
	if [ $WARN_CONF_REPLACE -eq 1 ]
	then
	
		rm -f $to
		echo saving existing $from into $to
		mv $from $to
	fi 
fi

from=${PKG_INSTALL_ROOT}/etc/krb5.keytab
to=${PKG_INSTALL_ROOT}/etc/krb5/krb5.keytab

if [ -f $from ]
then
	if [ ! -f $to ]
	then
		echo moving existing $from into $to
		mv $from $to
	fi
fi


if [ -d ${PKG_INSTALL_ROOT}/opt/krb5/var/krb5kdc ]
then
	for i in krb5.keytab krb5_adm.acl kdc.conf kadm5.keytab kadm5.acl kpropd.acl
	do
		from=${PKG_INSTALL_ROOT}/opt/krb5/var/krb5kdc/${i}
		to=${PKG_INSTALL_ROOT}/etc/krb5/${i}
		
		if [ -f $from ]
		then
			if [ ! -f $to ]
			then
				echo moving existing $from into $to
				mv $from $to
			fi
		fi
	done
		
	for i in principal.db principal.kadm5 principal.kadm5.lock principal.ok
	do
		from=${PKG_INSTALL_ROOT}/opt/krb5/var/krb5kdc/${i}
		to=${PKG_INSTALL_ROOT}/var/krb5/${i}
		if [ -f $from ]
		then
			if [ ! -f $to ]
			then
				echo moving $from to $to
				mv $from $to
			fi
		fi
	done

	for i in `find ${PKG_INSTALL_ROOT}/opt/krb5/var/krb5kdc -name slave_datatrans* -print`
	do
		filename=`basename $i`
		to=${PKG_INSTALL_ROOT}/var/krb5/${filename}
		if [ ! -f $to ]
		then
			echo moving $i to $to
			mv $i $to
		fi
	done

	for i in `find ${PKG_INSTALL_ROOT}/opt/krb5/var/krb5kdc -name .k5.* -print`
	do
		filename=`basename $i`
		to=${PKG_INSTALL_ROOT}/var/krb5/${filename}
		if [ ! -f $to ]
		then
			echo moving $i to $to
			mv $i $to
		fi
	done

	for i in `find ${PKG_INSTALL_ROOT}/opt/krb5/var/krb5kdc -name from_master* -print`
	do
		filename=`basename $i`
		to=${PKG_INSTALL_ROOT}/var/krb5/${filename}
		if [ ! -f $to ]
		then
			echo moving $i to $to
			mv $i $to
		fi
	done
fi

exit 0
