#!/bin/sh

#
# Copyright (C) 1999, 2004 PADL Software Pty Ltd
# All rights reserved.
#

YPLDAPD=/Library/NISLDAPGateway
CONFDIR=$YPLDAPD/etc
BINDIR=$YPLDAPD/bin
SBINDIR=$YPLDAPD/sbin
LICENCE_FILE_DIR=$CONFDIR
HOSTNAME=`hostname`
VERS="2.1FCS"

echo
echo "================================================================"
echo "Thank you for downloading ypldapd."
echo "================================================================"
echo
echo "Please read the license agreement carefully before installing"
echo "the software."
echo
echo "From here on it is assumed that you have read the"
echo "license agreement. If you have not please hit CONTROL-C"
echo "and re-run this script."
echo
echo "Please hit the ENTER key to continue..."
echo

read agree
clear

echo
echo "ypldapd will not run without"
echo "a password license key correctly installed on the host."
echo
echo "Your package may have been shipped with a temporary password"
echo "license key, to enable installation or evaluation of the"
echo "software."
echo
echo "You may request a 30-day evaluation license or (upon payment)"
echo "obtain a permanent license key by sending email to keys@padl.com."
echo

questionPassword="Please enter your password license string"
noPassword="ENTER to quit"

echo
echo "$questionPassword [$noPassword]"
echo
read thePassword
case "$thePassword" in
	'') echo
echo "ypldapd installation aborted."
echo
exit 1          ;;      # NOTE BREAK OUT
	*) echo
esac

PADLOCK_LICENSE_KEY="$thePassword"

question="Enter the distinguished name of your naming context:"
echo "$question " | tr -d '\012' > /dev/tty
read LDAP_BASEDN
if [ "X$LDAP_BASEDN" = "X" ]; then
	echo "You must enter a distinguished name."
	exit 2
fi

defaultdomain=`/bin/domainname`
question="Enter the NIS domain ypldapd will serve [$defaultdomain]:"
echo "$question " | tr -d '\012' > /dev/tty
read NISDOMAIN
if [ "X$NISDOMAIN" = "X" ]; then
	NISDOMAIN=$defaultdomain
fi

question="Enter your default DNS mail domain [$NISDOMAIN]:"
echo "$question " | tr -d '\012' > /dev/tty
read MAILDOMAIN
if [ "X$MAILDOMAIN" = "X" ]; then
	MAILDOMAIN=$NISDOMAIN
fi

question="Enter the IP address or name of your LDAP server [ldap]:"
echo "$question " | tr -d '\012' > /dev/tty
read LDAPHOST
if [ "X$LDAPHOST" = "X" ]; then
	LDAPHOST=ldap
fi

question="Do you wish to import /etc flat files into LDAP?"
default="no"

while :
do
        echo "$question [$default]? " | tr -d '\012' >/dev/tty  # portable echo -n
        read answer
        case "$answer" in
        '')     answer="$default"       ;;
        esac
        case "$answer" in
        n*|N*)  answer=no       ;;
        y*|Y*)  answer=yes      ;;
        esac
        case "$answer" in
        !*)     cmd="`expr \"$answer\" : '!\(.*\)'`"
                trap : 2
                ${SHELL-/bin/sh} -c "$cmd"
                trap 2
                echo '!'
                ;;
        yes|no) break           ;;      # NOTE BREAK OUT
        *)      echo '???' >/dev/tty    ;;
        esac
done

BUILDDB=$answer

if [ "$BUILDDB" = "yes" ]; then
	if [ "X$LDAP_BINDDN" = "X" ]; then
	        question="Enter the manager DN: [cn=manager,$LDAP_BASEDN]:"
       		echo "$question " | tr -d '\012' > /dev/tty
	        read LDAP_BINDDN
	        if [ "X$LDAP_BINDDN" = "X" ]; then
	                LDAP_BINDDN="cn=manager,$LDAP_BASEDN"
	        fi
	fi

	if [ "X$LDAP_BINDCRED" = "X" ]; then
	        question="Enter the credentials to bind with:"
	        echo "$question " | tr -d '\012' > /dev/tty
	        stty -echo
	        read LDAP_BINDCRED
	        stty echo
	fi
fi

echo
echo


cat > $LICENCE_FILE_DIR/padlock.ldif << EOF
dn: cn=$HOSTNAME+padlockApplication=ypldapd@PADL.COM,$LDAP_BASEDN
cn: $HOSTNAME
objectClass: padlockLicense
padlockApplication: ypldapd@PADL.COM
padlockType: dsaWithSHA
padlockKey:: $PADLOCK_LICENSE_KEY

EOF

mv $BINDIR/migrate_common.ph $BINDIR/.migrate_common.ph
cat $BINDIR/.migrate_common.ph | sed s/@@@DEFAULT_BASE@@@/"$LDAP_BASEDN"/ | sed s/@@@DOMAIN@@@/"$MAILDOMAIN"/ > $BINDIR/migrate_common.ph
rm $BINDIR/.migrate_common.ph

cat > $CONFDIR/ypldapd.conf << EOF
# ypldapd $VERS configuration file
# Created on `date` by `/usr/bin/whoami`.

# NIS domain to serve
ypdomain $NISDOMAIN

# LDAP server
ldaphost $LDAPHOST

# Search base
basedn $LDAP_BASEDN

# Enable caching
caching on

# Dump caches every half hour
cache_dump_interval 30

# Use the default naming context mappings
namingcontexts namingcontexts.conf

# see $CONFDIR/ypldapd.conf.sample for additional configuration
# options (or manual page)
EOF

if [ -x "/usr/bin/perl" ]; then
	PERL=/usr/bin/perl
elif [ -x "/usr/local/bin/perl" ]; then
	PERL=/usr/local/bin/perl
elif [ -x "perl" ]; then
	PERL=perl
fi
export PERL

if [ "X$PERL" != X ]; then
	case "$BUILDDB" in
	        yes)    echo 'Loading local configuration...'
			(cd $BINDIR; ./migrate_all_online.sh)
			if [ $? -ne 0 ]; then
				echo "WARNING: migrate_all_online.sh failed"
				echo
			fi
	esac
else
	echo
	echo "Once you have installed the Perl interpreter and ldapadd"
	echo "you can run $BINDIR/migrate_all_offline.sh or"
	echo "$BINDIR/migrate_all_online.sh to migrate your"
	echo "existing configuration information."
	echo
fi

echo
echo
echo "Installation completed; now follow instructions in install guide."
echo


