#!/bin/sh
#
#ident "@(#)dsadmintool	1.14 01/07/99 SMI"
#
# 
# Copyright 01/07/99 Sun Microsystems, Inc. All Rights Reserved
#
#
trap 'stty echo;exit 1' INT QUIT

unset ENV

if [ -z "${JAVA_HOME}" ] ; then
   if [ -d /usr/java ] ; then
      JAVA_HOME=/usr/java
      CLASSPATH=.:/usr/java/lib/classes.zip
      export CLASSPATH
  else
    if [ -d /usr/local/java/jdk1.1.5/solaris ] ; then
      JAVA_HOME=/usr/local/java/jdk1.1.5/solaris
      CLASSPATH=.:/usr/local/java/jdk1.1.5/solaris/lib/classes.zip
      export CLASSPATH
    else
      echo "JAVA_HOME environment variable not set." 1>&2 
      exit 1
    fi
  fi
fi

if [ -x "${JAVA_HOME}/bin/jre" ] ; then
  JAVAEXE="${JAVA_HOME}/bin/jre"
else
  if [ -x "${JAVA_HOME}/bin/java" ] ; then
    JAVAEXE="${JAVA_HOME}/bin/java"
  else 
    echo "No Java run time found in ${JAVA_HOME}" 1>&2
    exit 1
  fi
fi

BASEDIR=`/bin/pkgparam SUNWsds BASEDIR 2>/dev/null`
if [ -z "$BASEDIR" ]    # pkg not installed
then
        BASEDIR=/opt    # set defaul. Maybe the dir is nfs mounted
fi

Host=
PortCmd=

case $1 in
	-h|-\?)	echo "Usage: $0 [ host [port] ] "
		exit 1;;
	*)	# Nothing;;
esac

if [ $# -gt 2 ] ; then
  echo "Usage: $0 [ host [port] ] "
  exit
fi

if [ $# -eq 2 ] ; then
  HostCmd="-Dsunds.host=$1"
  PortCmd="-Dsunds.port=$2"
elif [ $# -eq 1 ] ; then
  HostCmd="-Dsunds.host=$1"
fi


ADMINCLASSES="$BASEDIR/SUNWconn/ldap/html/lib:$BASEDIR/SUNWconn/ldap/html"

if [ -z "$CLASSPATH" ] ; then
  CLASSPATH="${ADMINCLASSES}:${JAVA_HOME}/lib/classes.zip"
else
  CLASSPATH="${ADMINCLASSES}:$CLASSPATH"
fi
#
# update the password if not already set
#
if grep rootpw /etc/opt/SUNWconn/ldap/current/dsserv.conf | head -1 | grep NOTSET >/dev/null 2>&1
then
	echo "Admin password not set, the script will now ask you for to provide one."
	echo
	echo "If you don't see the 'Enter new admin password:' prompt"
	echo "make sure that you are not running dsadmintool in background."
	echo
  stty -echo
  ok=0
  while [ $ok -eq 0 ]
  do
    echo "Enter new admin password: \c"
    read p1
		echo
    echo "Re-enter new admin password: \c"
    read p2
		echo
    if [ "$p1" != "" -a "$p1" = "$p2" ]
    then
      ok=1
      LD_LIBRARY_PATH=$BASEDIR/SUNWconn/ldap/html/lib:$LD_LIBRARY_PATH
      export LD_LIBRARY_PATH
      pass=`echo $p1 | ${JAVAEXE} -Dbasedir=$BASEDIR -classpath ${CLASSPATH} COM.Sun.sunsoft.ldaps.sims.server.LDACommandManager 2>/dev/null`
			if echo $pass | grep {sunds} >/dev/null 2>&1
			then
				sed "s/rootpw.*/rootpw $pass/" < /etc/opt/SUNWconn/ldap/current/dsserv.conf > /etc/opt/SUNWconn/ldap/current/dsserv.conf.1
				mv  /etc/opt/SUNWconn/ldap/current/dsserv.conf.1 /etc/opt/SUNWconn/ldap/current/dsserv.conf
			else
				echo "can't update password"
			  stty echo
				exit 1
			fi
		else
			echo "Passwords don't match or null password"
    fi
	done
	stty echo
fi

CMD="${JAVAEXE} -Dbasedir=$BASEDIR $HostCmd $PortCmd -classpath ${CLASSPATH} COM.Sun.sunsoft.sims.admin.console.SDSAdmin"

${CMD}
