#!/bin/sh
#
# Copyright 2001-2002 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#ident	"@(#)directoryserver	1.6 - 02/02/20"
# set -x

#########################################
# usage
# print usage message
# input: $1 - 1: exit 1
#             0: do not exit
#########################################
usage () {
	echo "Usage: $0"
	echo "	   help [ subcommand ]"
	echo "Usage: $0"
	echo "	   { setup [ -f <configuration file> ] | uninstall }"
	echo "Usage: $0"
	echo "	   { start-admin | stop-admin | restart-admin | startconsole }"
	echo "Usage: $0 [ { -s|-server } server-instance ]" 
	echo "	   { start | stop | restart }"
	echo "Usage: $0 { -s|-server } server-instance"
	echo "	   { monitor | saveconfig | restoreconfig | db2index-task |"
	echo "	     ldif2db-task | ldif2db | ldif2ldap |  vlvindex |"
	echo "	     db2ldif | db2ldif-task | db2bak | db2bak-task |"
	echo "	     bak2db | bak2db-task | suffix2instance |"
	echo "	     account-status | account-activate | account-inactivate }"
	echo "	      { options-and-arguments ...}"
	echo "Usage: $0"
	echo "	   { nativetoascii | admin_ip | ldif | pwdhash | idsktune |"
	echo "	     mmldif | keyupg } { options-and-arguments ...}"
	echo "Usage: $0"
	echo "	   { sagt | magt } { options-and-arguments ...}"
	echo "	where options-and-arguments depend on the specific subcommand"
	[ $1 -eq 1 ] && exit 1
}
#########################################
# get_bindpasswd
# get bind password with echo OFF
#
# modify global variable: bind_passwd
#########################################
get_bindpasswd()
{
# Handle signals, so that echo can be turned back on if Ctrl-C.
trap "/usr/bin/stty echo; exit" 1 2 3 6 15

/usr/bin/stty -echo     # Turn echo OFF

# Endless loop that continues until passwd 
# is given
bind_passwd=""
first_try=yes
while [ "$bind_passwd" = "" ]
do
    if [ ${first_try} = no ]
    then
	echo ""
    fi
    first_try=no

    bind_passwd=""  

    echo "\nEnter bind password: \c"
    read bind_passwd
done

/usr/bin/stty echo      # Turn echo ON

# Removed signal handler
trap 1 2 3 6 15

echo ""
}
#########################################
# scan_passwd
# scan its arguments to find '-w -'.
#       if found, call get_bindpasswd and replace
#       '-w -' by '-j <temp_file>'
#
# modify global variable: new_arg
# use global variable: TMP_PASSWD_FILE and bind_passwd
#########################################
scan_passwd () {
init_arg=$@
> $TMP_PASSWD_FILE
chmod 700 $TMP_PASSWD_FILE
got_w=no
for i in "$@"
do
    case $i in
    -w)
	got_w=yes
	;;
    *)
	if [ $got_w = yes ]
	then
	    got_w=no
	    if [ "$i" = "-" ]
	    then
		get_bindpasswd
		echo $bind_passwd > $TMP_PASSWD_FILE
		new_arg="$new_arg -j $TMP_PASSWD_FILE"
	    else
		new_arg="$new_arg -w '$i'"
	    fi
	else
	    new_arg="$new_arg '$i'"
	fi
	;;
    esac
done
}
#########################################
# clean_passwd
# remove the temporay file used for passwd
#
# use global variable: TMP_PASSWD_FILE
#########################################
clean_passwd () {
    rm -f $TMP_PASSWD_FILE
}
#########################################
# subcommand_usage 
# print subcommand usage message
# input: $1 - type of the subcommand
# 	 $2 - directory of the subcommand
#########################################
subcommand_usage () {
COMMAND=$1 # subcommand
DIR=$2

if [ -z "$DIR" ] ; then
	case "$COMMAND" in
	db2index-task | \
	ldif2db-task | \
	db2ldif-task | \
	db2bak-task | \
	bak2db-task | \
	ldif2db | \
	vlvindex | \
	db2ldif | \
	suffix2instance | \
	account-status | \
	account-activate | \
	account-inactivate )
		echo "Usage: $COMMAND"
		echo "   For a more detailed usage message please setup your"
		echo "   directory server first, using directoryserver setup."
		exit 0
		;;
	*)
		;;
	esac
fi
case "$COMMAND" in

help )
	usage 1
	;;
setup )
	echo "Usage: $COMMAND [ -f <configuration file> ]"
	;;
startconsole )
	$USR/startconsole -h
	;;
uninstall | \
start-admin | \
stop-admin | \
restart-admin | \
start | \
stop | \
restart | \
monitor | \
saveconfig | \
restoreconfig )
	echo "Usage: $COMMAND"
	;;
db2index-task )
	$DIR/db2index.pl
	;;
ldif2db-task )
	$DIR/ldif2db.pl
	;;
db2ldif-task )
	$DIR/db2ldif.pl
	;;
db2bak-task )
	$DIR/db2bak.pl
	;;
bak2db-task )
	$DIR/bak2db.pl
	;; 
ldif2db | \
vlvindex | \
db2ldif | \
suffix2instance ) 
	$DIR/$COMMAND
	;;
account-status ) 
	$DIR/ns-accountstatus.pl
	;;
account-activate ) 
	$DIR/ns-activate.pl
	;;
account-inactivate )
	$DIR/ns-inactivate.pl
	;;
ldif2ldap )
	echo "Usage: $COMMAND <rootDN> <password> <filename>"
	;;
bak2db )
	echo "Usage: $COMMAND <backupdir>"
	;; 
db2bak )
	echo "Usage: $COMMAND [backupdir]"
	;;
nativetoascii | \
NativeToAscii )
        $USR/shared/bin/NativeToAscii
        ;;
admin_ip )
        /usr/bin/perl $USR/shared/bin/admin_ip.pl
        ;;
ldif | \
pwdhash | \
keyupg  )
        $USR/bin/slapd/server/$COMMAND
        ;;
mmldif )
	$USR/bin/slapd/server/$COMMAND -h
	;; 
idsktune ) 
        $USR/bin/slapd/server/$COMMAND --help 2> /dev/null
        ;;
sagt | \
magt )
	$USR/plugins/snmp/$COMMAND/$COMMAND --help 
	;;
* )
	echo "$0: Invalid subcommand: $COMMAND"
	echo 
	usage 0
	;;
esac
exit 0
}
##########################################
# is_root
#
# return: 1 - TRUE
#         0 - FALSE
##########################################
is_root () {
ROOTID=`id | grep "uid=0(root)"`
if [ -z "$ROOTID" ]; then
  return 0
else
  return 1
fi
}
#########################################
# exec_var_subcommand
#
# input: $1 - directory of the subcommand
#        $2 - type of the subcommand
#        $3..N - the parameters of the subcommand
# return: 0 - Success
#         1 - Fail
##########################################
exec_var_subcommand () {

DIR=$1
COMMAND=$2 # subcommand

# Skip dir and subcommand.
# The $@ become the parameters for
# the subcommand
shift; shift

case "$COMMAND" in
start )
	# check if it's run as root
	is_root
	IS_ROOT=$?
	if [ $IS_ROOT -eq 0 ]; then
	  echo "$0 $COMMAND: must run as root"
	  return 1
	fi

	echo $DIR/start-slapd
	$DIR/start-slapd
	;;
stop )
	# check if it's run as root
	is_root
	IS_ROOT=$?
	if [ $IS_ROOT -eq 0 ]; then
	  echo "$0 $COMMAND: must run as root"
	  return 1
	fi

	echo $DIR/stop-slapd
	$DIR/stop-slapd
	;;
restart )
	# check if it's run as root
	is_root
	IS_ROOT=$?
	if [ $IS_ROOT -eq 0 ]; then
	  echo "$0 $COMMAND: must run as root"
	  return 1
	fi

	echo $DIR/restart-slapd
	$DIR/restart-slapd
	;;
monitor )
	echo $DIR/$COMMAND "$@"
	$DIR/$COMMAND "$@"
	;;
ldif2db-task )
	scan_passwd "$@"
	echo $DIR/ldif2db.pl "$new_arg"
	eval $DIR/ldif2db.pl "$new_arg"
	clean_passwd
	;;
db2ldif-task )
	scan_passwd "$@"
	echo $DIR/db2ldif.pl "$new_arg"
	eval $DIR/db2ldif.pl "$new_arg"
	clean_passwd
	;;
db2bak-task )
	scan_passwd "$@"
	echo $DIR/db2bak.pl "$new_arg"
	eval $DIR/db2bak.pl "$new_arg"
	clean_passwd
	;;
bak2db-task )
	scan_passwd "$@"
	echo $DIR/bak2db.pl "$new_arg"
	eval $DIR/bak2db.pl "$new_arg"
	clean_passwd
	;;
db2index-task )
	scan_passwd "$@"
	echo $DIR/db2index.pl "$new_arg"
	eval $DIR/db2index.pl "$new_arg"
	clean_passwd
	;;
saveconfig | \
restoreconfig | \
ldif2db | \
db2ldif | \
vlvindex | \
db2bak | \
suffix2instance )
	# check if it's run as root
	is_root
	IS_ROOT=$?
	if [ $IS_ROOT -eq 0 ]; then
	  echo "$0 $COMMAND: must run as root"
	  return 1
	fi

	echo $DIR/$COMMAND "$@"
	$DIR/$COMMAND "$@"
	;;
bak2db )
	# check if it's run as root
	is_root
	IS_ROOT=$?
	if [ $IS_ROOT -eq 0 ]; then
	  echo "$0 $COMMAND: must run as root"
	  return 1
	fi

	if [ $# -ne 1 ]; then
	  echo "Usage: $0 -s server-instance $COMMAND <backupdir>"
	  return 1
	fi

	echo $DIR/$COMMAND "$@"
	$DIR/$COMMAND "$@"
	;;
ldif2ldap )
	if [ $# -ne 3 ]; then
	  echo "Usage: $0 -s server-instance $COMMAND <rootDN> <password> <filename>"
	  return 1
	fi

	echo $DIR/$COMMAND "$@"
	$DIR/$COMMAND "$@"
	;;
account-inactivate )
	scan_passwd "$@"
	echo $DIR/ns-inactivate.pl "$new_arg"
	eval $DIR/ns-inactivate.pl "$new_arg"
	clean_passwd
	;;
account-activate )
	scan_passwd "$@"
	echo $DIR/ns-activate.pl "$new_arg"
	eval $DIR/ns-activate.pl "$new_arg"
	clean_passwd
	;;
account-status )
	scan_passwd "$@"
	echo $DIR/ns-accountstatus.pl "$new_arg"
	eval $DIR/ns-accountstatus.pl "$new_arg"
	clean_passwd
	;;
* )
	echo "$0: exec_subcommand: Invalid subcommand: $COMMAND"
	echo 
	usage 0
	return 1
	;;
esac

return $?
}
#########################################
# parse_var_subcommands
#
# input: $@ - the parameters list
#             (-s <instance> is stripped off
#              if there is any)
#	       
# return: 0 - Success
#         1 - Fail
##########################################
parse_var_subcommands () {

# start, stop and restart call this subroutine
# iterate the "$VAR/slapd-*"
RC=0
for di in $DIR; do
	exec_var_subcommand "$di" "$@"
	[ $? -ne 0 ] && RC=1
done

return $RC
}

##########################################
# exec_usr_subcommand
#
# input: parameters of /ust/sbin/directoryserver
# return: the return code of the subcommand
##########################################
exec_usr_subcommand () {

COMMAND=$1 # subcommand

# Skip subcommand.
# The $@ become the parameters for
# the subcommand
shift

case "$COMMAND" in
nativetoascii | \
NativeToAscii )
	echo $USR/shared/bin/NativeToAscii "$@"
	$USR/shared/bin/NativeToAscii "$@"
	;;
admin_ip )
	echo /usr/bin/perl $USR/shared/bin/admin_ip.pl "$@"
	/usr/bin/perl $USR/shared/bin/admin_ip.pl "$@"
	;;
ldif | \
pwdhash | \
idsktune | \
mmldif | \
keyupg  )
	echo $USR/bin/slapd/server/$COMMAND "$@"
	$USR/bin/slapd/server/$COMMAND "$@"
	;;

* )
	echo "$0: exec_usr_command: Invalid subcommand: $COMMAND"
	echo
	usage 0
	return 1
esac

return $?
}
##########################################
# exec_setup_subcommand
#
##########################################
exec_setup_subcommand () {

SILENT_INSTALL=0
dash_s=
has_dash_s=0
DASH_S=
HAS_DASH_S=0

# The options are copied from Installer::getOptions
# of ns/netsite/installer/unix/installer.cc
# in "admin_branch Installer"
# If the options are updated in intaller.cc,
# the options below have to be updated accordingly.
#
# 
while getopts "?hb:srSf:vi:Bkp" opt; do
  case $opt in 
    f  )
      if [ ! -f "$OPTARG" ]; then
         echo "ERROR: The file \"$OPTARG\" does not exist!"
         return 1
      fi
      SILENT_INSTALL=1
      ;;
    s  )
      has_dash_s=1
      ;;
    S  )
      # Solaris native package
      HAS_DASH_S=1
      ;;
  esac
done 

if [ $SILENT_INSTALL -eq 1 ]; then
  # provide "-s" for the silent install if it's not provided by the user
  if [ $has_dash_s -eq 0 ]; then
     dash_s="-s"
  fi
else
  # -s and -f <file> have to coexist
  if [ $has_dash_s -eq 1 ]; then
    echo "ERROR: The silent installation requires a script."
    echo "e.g.  $0 setup $@ -f myscript.inf"
    return 1
  fi 
fi
 
# provide -S if it's not provided by the user
# 
if [ $HAS_DASH_S -eq 0 ]; then
   DASH_S="-S"
fi

echo "$SETUP/setup $DASH_S $dash_s $@"
$SETUP/setup $DASH_S $dash_s $@
return $?

}
##########################################
# exec_install_subcommand
#
##########################################
exec_install_subcommand () {
is_root
IS_ROOT=$?
if [ $IS_ROOT -eq 0 ]; then
  echo "$0 $1: must run as root"
  return 1
fi
COMMAND=$1

case $COMMAND in
setup  )
	shift # skip "setup" in $1
	exec_setup_subcommand "$@"
        return $?
        ;;
uninstall )
	shift # skip "uninstall" in $1
        echo "$USR/uninstall -S $@"
        $USR/uninstall -S "$@"
        return $?
        ;;
* )
        usage 0
        return 1
        ;;
esac


}
##########################################
# exec_admin_subcommand
#
# input: "$@" of the directoryserver
# return: 0 - success
#         None zero - Fail
#########################################
exec_admin_subcommand () {
COMMAND=$1
case "$COMMAND" in
start-admin | \
stop-admin  | \
restart-admin )
	if [ $# -ne 1 ]; then
		echo "Usage: $0 $COMMAND"
		return 1
	fi
	is_root
	IS_ROOT=$?
	if [ $IS_ROOT -eq 0 ]; then
	  echo "$0 $COMMAND: must run as root"
	  return 1
	fi
	echo "$USR/$COMMAND"
	$USR/$COMMAND
	return $?
	;;
startconsole )
	shift
	while getopts ":l:" LOPT
	do
        	[ "$LOPT" = "l" ] && break
	done
	if [ "$LOPT" != "l" ] && [ ! -z "$LANG" ]; then
		LOPT=`echo $LANG | awk '
			/^zh$/ || /^zh\..*/ || /^zh_CN.*/ { print "-l zh"}
			/^ja.*/ {print "-l ja"}'`
	else
        	LOPT=
	fi
	echo "$USR/$COMMAND $@ $LOPT"
	$USR/$COMMAND "$@" $LOPT
	return $?
	;;
* )
	usage 0
	return 1
	;;
esac	

}
##########################################
# exec_snmp_subcommand
#
# input: "$@" of the directoryserver
# return: 0 - success
#         None zero - Fail
#########################################
exec_snmp_subcommand () {

COMMAND=$1
if [ "$COMMAND" = "magt" ]; then
	is_root
	IS_ROOT=$?
	if [ $IS_ROOT -eq 0 ]; then
	  echo "$0 $COMMAND: must run as root"
	  return 1
	fi
fi

shift

echo $USR/plugins/snmp/$COMMAND/$COMMAND "$@"
cd $USR/plugins/snmp/$COMMAND
$USR/plugins/snmp/$COMMAND/$COMMAND "$@"
return $?
}
#########################################
# main 
##########################################
SETUP=/usr/iplanet/ds5/setup
VAR=/usr/iplanet/ds5
USR=/usr/iplanet/ds5
SERVER_INSTANCE=""
DIR=""
TMPDIR="/tmp/directoryserver.$$"
if mkdir -m 700 ${TMPDIR}
then
	# Cleanup on exit.
	trap 'rm -rf ${TMPDIR}' 0
else
	echo "ERROR: unable to create a safe temporary directory."
	exit 1
fi
TMP_PASSWD_FILE=${TMPDIR}/passdir.$$
new_arg=""
for di in $VAR/slapd-*
do
	[ -d "$di" ]  && DIR="$DIR $di"
done

LD_LIBRARY_PATH=$USR/lib:$LD_LIBRARY_PATH; export LD_LIBRARY_PATH

case "$1" in
setup | \
uninstall )
	exec_install_subcommand "$@"
	exit $?
	;;
start-admin | \
stop-admin  | \
restart-admin | \
startconsole  )
 	exec_admin_subcommand "$@"
	exit $?
	;;
start | \
stop  | \
restart )
	parse_var_subcommands "$@"
	exit $?
	;;
monitor | \
saveconfig | \
restoreconfig )
	echo "Usage: $0 -s server-instance $1"
	echo "e.g. $0 -s `/usr/bin/hostname` $1"
	exit 1
	;;
ldif2db | \
ldif2db-task | \
ldif2ldap | \
db2index-task | \
db2ldif | \
db2ldif-task | \
vlvindex | \
db2bak | \
db2bak-task | \
bak2db | \
bak2db-task | \
account-inactivate | \
account-activate | \
account-status | \
suffix2instance )
	echo "Usage: $0 -s server-instance $1 "
	echo "		{ options-and-arguments ...}"
	echo "e.g. $0 -s `/usr/bin/hostname` $1 ..."
	exit 1
	;;
-s | \
-server )
	shift
	SERVER_INSTANCE="$1"
	if [ ! -d "$VAR/slapd-$1" ]; then
		echo "$0: $VAR/slapd-$1 directory does not exist"
		if [ -z "$2" ]; then
			echo
			usage 0 
		fi
		exit 1
	fi
	shift
	exec_var_subcommand "$VAR/slapd-$SERVER_INSTANCE" "$@"
	exit $?
	;;
nativetoascii  | \
NativeToAscii  | \
admin_ip  | \
ldif  | \
pwdhash  | \
idsktune  | \
mmldif | \
keyupg )
	exec_usr_subcommand "$@"
	exit $?
	;;
magt | \
sagt )
	exec_snmp_subcommand "$@"
	exit $?
	;;
help )
	shift
	if [ $# -ne 0 ] ; then
		COMMAND="$1"
		HDIR=""
		for di in $VAR/slapd-*
		do
			[ -d "$di" ]  && HDIR="$di"
		done
		subcommand_usage "$COMMAND" "$HDIR"
	else
		usage 1
	fi
	;;
* )
	usage 1
	;;
esac
