#!/sbin/sh
#
# Copyright (c) 2001-2004 by Sun Microsystems, Inc.
# All rights reserved.
#
#ident	"@(#)$RCSfile: directoryserver.wrapper,v $	$Revision: 1.1.6.2 $ - $Date"

if [ "x"$VERBOSE != "x" ] ; then
    set -x
fi

debug() {
    if [ "x"$VERBOSE != "x" ] ; then
	echo $1
    fi
}

#########################################
# pkg_get_basedir
#
# input $1 : package name
##########################################
pkg_get_basedir () {
    pkg=$1
    num=`pkginfo -r "${pkg}" 2>/dev/null | sort | uniq | wc -l`
    if [ $num -eq 0 ]; then
	return 1
    fi
    BASEDIR=`pkginfo -r "${pkg}" | head -1`
    return 0
}

#########################################
# init 
#     Defines $REGISTRY
##########################################
init() {
    BASEDIR=/
    pkg_get_basedir SUNWdsvu
    if [ $? -ne 0 ]; then
	exit 1
    fi
    REGISTRY=${BASEDIR}/etc/ds/versions
    AWK_CMD=/usr/bin/nawk
}

#
# Copyright (c) 2001-2004 by Sun Microsystems, Inc.
# All rights reserved.
#

#########################################
# usage
# print usage message
# input: $1 - 1: exit 1
#             0: do not exit
#########################################
usage () {
	echo "Usage: ${DSCMD}"
	echo "	   -setdefaultversion | -d <version>"
	echo "Usage: ${DSCMD}"
	echo "	   -getdefaultversion | -g"
	echo "Usage: ${DSCMD}"
	echo "	   -listversions | -l"
	echo "Usage: ${DSCMD}"
	echo "	   -useversion | -u <version> subcommand {options-and-arguments ...}"
	echo "	where options-and-arguments depend on the specific subcommand"
	echo "Usage: ${DSCMD}"
	echo "	   subcommand {options-and-arguments ...}"
	echo "	where options-and-arguments depend on the specific subcommand"
	[ $1 -eq 1 ] && exit 1
}


##########################################
# is_root
#
# return: 1 - TRUE
#         0 - FALSE
##########################################
is_root () {
	ID=`/usr/bin/id | cut -f1 -d' '`
	if [ "$ID" = "uid=0(root)" ] || [ "$ID" = "uid=0" ]; then
		return 1
	else
		return 0
	fi
}

#########################################
# is_available_version
# 
# Checks if the version is installed and the command exists
#
# input:  $1 : version to test
#
# reminder: REGISTRY contents: version|path|installed|default
##########################################
is_available_version(){
    VERSION=$1

    if [ ! -f $REGISTRY ] ; then
	return 1;
    fi

    ret=`cat $REGISTRY | $AWK_CMD -v version=$VERSION -F "|" '{ \
        if ($1 !~ /^#/ ) {\
            if ( ($1== version) && ($3 == "YES") ) \
                print $2; \
        }}' | while read file ; do [ -f $file ] &&  echo 0; done`
    if [ -z "$ret" ] ; then
	return 1
    else
	return 0
    fi
}

#########################################
# available_versions
# 
# Sets AVAILABLES with all installed versions
#
# Return codes:
#     0 : No error
#     1 : No default defined
#
# reminder: REGISTRY contents: version|path|installed|default
##########################################
available_versions(){
    if [ ! -f $REGISTRY ] ; then
	available_versions_rc=1
	return;
    fi

    AVAILABLES=`cat $REGISTRY | $AWK_CMD -F "|" '{ 
        if ($1 !~ /^#/ ) {\
            if ($3 == "YES") \
                print $1" "$2; \
        }}' | while read version file ; do [ -f $file ] &&  echo $version ; done`
    if [ -z "$AVAILABLES" ] ; then
	available_versions_rc=1
    else
	available_versions_rc=0
    fi
}

#########################################
# get_default 
# 
# Sets DEFAULT with all default versions
#
# Return codes:
#     0 : No error
#     1 : No default defined
#     2 : Several default defined
# reminder: REGISTRY contents: version|path|installed|default
##########################################
get_default(){
    if [ ! -f $REGISTRY ] ; then
	get_default_rc=1
	return ;
    fi

    DEFAULT=`cat $REGISTRY | $AWK_CMD -F "|" '{ \
            if ($4 == "YES") { \
                print $1;\
                nb=nb+1;
            }
        }'`
    if [ -z "$DEFAULT" ] ; then
	get_default_rc=1
    elif [ `echo "$DEFAULT" | wc -l` -ne 1 ] ; then
	get_default_rc=2
    else
	get_default_rc=0
    fi
}

#########################################
# set_default 
# 
# input:  $1 : version to set as default
#
# reminder: REGISTRY contents: version|path|installed|default
##########################################
set_default() {

    # Must be root 
    is_root
    if [ $? -eq 0 ]; then
	echo "Error: Must run as root"
	exit 1
    fi
    
    # First test the version availibility
    if [ -z "$VERSION" ] ; then
	usage 1
    fi

    is_available_version $VERSION
    if [ $? -ne 0 ] ; then
	echo "The $VERSION version is not installed on this host. You must specify one of the following versions: "
	echo "$AVAILABLES"
	exit 1
    fi

    # Modify registry
    rm -f /tmp/versions.tmp
    cat $REGISTRY | $AWK_CMD -v version=$VERSION -F "|" '{ \
        if ($1 ~ /^#/ ) { \
            print $0 } \
        else  { \
            if ( $1 == version ) { \
                $4="YES" \
            } else { \
                $4="NO" \
            }; \
            print $1"|"$2"|"$3"|"$4} \
        }' > /tmp/versions.tmp
    mv -f /tmp/versions.tmp $REGISTRY
    echo "Default version is now: $VERSION"
}

#########################################
# get_directoryserver_command
# 
# input:  $1 : version to use
#              if no $1, use default version
# 
##########################################
get_directoryserver_command() {
    # If no version defined, select default one
    if [ -z "$VERSION" ] ; then
	get_default
	if [ $get_default_rc -eq 1 ] ; then
	    echo "Error: no default version defined. Please choose one."
	    usage 1
	elif [ $get_default_rc -eq 2 ] ; then
	    echo "Error: several default versions defined."
	    usage 1
	else
	    VERSION=$DEFAULT
	fi
        # Test the version availibility
	is_available_version $VERSION
	if [ $? -ne 0 ] ; then
	    echo "The default version is not installed on this host. You must change the default to one of the following versions: "
	    echo "$AVAILABLES"
	    exit 1
	fi
    else
        # Test the version availibility
	is_available_version $VERSION
	if [ $? -ne 0 ] ; then
	    echo "The $VERSION version is not installed on this host. You must specify one of the following versions: "
	    echo "$AVAILABLES"
	    exit 1
	fi
    fi

    # Set DIRECTORYSERVER_COMMAND
    DIRECTORYSERVER_COMMAND=`grep "^$VERSION|" $REGISTRY | $AWK_CMD -F "|" '{print $2 }'`
}

#########################################
# print_default 
#########################################
print_default(){
    get_default
    if [ $get_default_rc -eq 0 ] ; then
	echo "Default is:" `cat $REGISTRY | grep "^$DEFAULT" | $AWK_CMD  -F "|" '{print $1" "$2}'`
    elif [ $get_default_rc -eq 1 ]  ; then
	echo "Error: no default version defined"	
	return 1
    elif  [ $get_default_rc -eq 2 ]  ; then
	echo "Error: several default versions defined"	
	return 1
    fi
    return 0
}

#########################################
# main 
##########################################

init
available_versions

DSCMD=`basename $0`

ARGS="$@"
if [ -z "$ARGS" ] ; then
	usage 1
fi
while true
do
    case "$1" in
	-g | -getdefaultversion )
	    if [ $# -ne 1 ] ; then
		usage 1
	    fi
	    print_default
	    exit $?
	    ;;
	-l | -listversions )
	    if [ $# -ne 1 ] ; then
		usage 1
	    fi
	    if [ $available_versions_rc -eq 0 ] ; then 
		echo "The following versions are available:"
		echo "$AVAILABLES"
	    else 
		echo "Error: no version available"
	    fi
	    exit $available_versions_rc
	    ;;
	-u | -useversion )
	    shift
	    if [ -z "$1" ]; then
		echo
		usage 1
	    fi
	    VERSION="$1"
	    ;;
	-d | -setdefaultversion )
	    if [ $# -ne 2 ] ; then
		usage 1
	    fi
	    shift
	    VERSION="$1"
	    set_default
	    exit
	    ;;	
	* )
	    break
	    ;;
    esac
    shift
done

get_directoryserver_command
$DIRECTORYSERVER_COMMAND "$@"


