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

#########################################
# getPkgBase
#
##########################################

getPkgBase () {
    pkginfo -r $1 2> /dev/null
}


#########################################
# getMultiplePkgInfo
#
##########################################

getMultiplePkgInfo () {
    pkginfo "$1.*"
}


#########################################
# getPkgList
#
# NSPR
# NSS
# ICU
# LDAP
# SASL
# JSS
#
##########################################

getPkgList () {
    PKGS="
	SUNWpr 
	SUNWtls
	SUNWicu
	SUNWldk
	SUNWsasl
	SUNWjss
    "
}


#########################################
# getAdmServPkgList
#
#
##########################################
getAdmServPkgList () {
    PKGS="
	SUNWasvc 
	SUNWasvcp
	SUNWasvu
    "
}

##########################################
# set_env_vars
#
##########################################
set_env_vars () {

    pkg_get_basedir SUNWasvc
    USR_ADM_PUBLIC=$BASEDIR/usr/sbin
    USR_ADM=$BASEDIR/usr/sadm/mps/admin/v5.2
    ETC_ADM=$BASEDIR/etc/mps/admin/v5.2
    SERVER_ROOT_CONF=$ETC_ADM/shared/config/serverroot.conf
    SERVER_ROOT=$BASEDIR/var/mps/serverroot
    if [ -f $SERVER_ROOT_CONF ]; then
       SERVER_ROOT=`cat $SERVER_ROOT_CONF`
    fi

}

#########################################
# pkg_check
#
# check that required packages are
# correctly installed :
#
##########################################

pkg_check () {

    rc=0

    getPkgList
    
    basedirs=""

    missing=""
    for pkg in ${PKGS}; do
	num=`getMultiplePkgInfo "${pkg}" | sort | uniq | wc -l`
	if [ $num -eq 0 ]; then
	    missing="$missing$pkg "
	    continue
	fi
    done

    # check for missing required packages
    if [ -n "$missing" ]; then
	echo "ERROR: The following packages are not installed:"
	echo $missing
	echo
	rc=1
    fi

    return $rc

}


#########################################
# as_pkg_check
#
# check that required packages are
# correctly installed
#
##########################################
as_pkg_check () {
    rc=0

    getAdmServPkgList
    
    basedirs=""

    missing=""
    for pkg in ${PKGS}; do
	num=`getMultiplePkgInfo "${pkg}" | sort | uniq | wc -l`
	if [ $num -eq 0 ]; then
	    missing="$missing$pkg "
	    continue
	fi
	instances=`getMultiplePkgInfo "$pkg" | awk '{ print $2; }'`
	for n in $instances; do
	    basedir=`getPkgBase $n`
	    if [ -z "$basedirs" ]; then
		basedirs=$basedir
	    else
		basedirs="$basedirs\n$basedir"
	    fi
	done
    done

    # check for missing required packages
    if [ -n "$missing" ]; then
	echo "ERROR: The following packages are not installed:"
	echo $missing
	echo
	rc=1
    fi
 
    # check for unique basedir
    num=`echo $basedirs | sort | uniq | wc -l`
    if [ $num -ne 1 ]; then
	echo "ERROR: The installed packages do not have a unique basedir."
	echo
	rc=1
    fi

    return $rc
}

#########################################
# pkg_get_basedir
#
# set BASEDIR variable 
##########################################
pkg_get_basedir () {
    pkg=$1
    num=`getMultiplePkgInfo "$pkg" | sort | uniq | wc -l`
    if [ $num -eq 0 ]; then
	echo "ERROR: Package ${pkg} is not installed"
	exit 1
    fi
    BASEDIR=`getPkgBase "$pkg" | head -1`
}


#########################################
# is_configured 
#
# return non-zero if configuration has been done
#
##########################################
is_configured () {

    if [ -f $SERVER_ROOT/admin-serv/config/adm.conf ]; then
	return 1
    fi
    return 0
}


#########################################
# exit_if_not_configured 
#
# exit if the configuration has not been done
#
##########################################
exit_if_not_configured () {

    is_configured
    if [ $? -eq 0 ]; then
	echo "You must configure Administration Server first."
	echo "Run ${USR_ADM_PUBLIC}/mpsadmserver configure."
	exit 1
    fi

}


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

# Check other packages

pkg_check
if [ $? -ne 0 ]; then
    exit 1
fi

# Admin server part

as_pkg_check
if [ $? -ne 0 ]; then
    exit 1
fi

set_env_vars

exit_if_not_configured
$SERVER_ROOT/bin/admin/admconfig "$@"
