#!/bin/ksh
#
# Copyright 2001 Sun Microsystems, Inc.  All rights reserved.
#
#

ECHO=/usr/bin/echo
CAT=/usr/bin/cat
MKDIR=/usr/bin/mkdir
SHARE=/usr/sbin/share
UNSHARE=/usr/sbin/unshare
RM=/usr/bin/rm

$ECHO "Netra HA Suite 2.0 installation"

# $CAT <<END_OF_COPYRIGHT_NOTICE
# 
# Copyright  2001 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.  Third-party software, including font
# technology, is copyrighted and licensed from Sun suppliers.  Portions may be
# derived from Berkeley BSD systems, licensed from U. of CA. Sun, Sun
# Microsystems, the Sun logo, Java, JMX, Netra, Solaris, Sun StorEdge,
# iPlanet, docs.sun.com, AnswerBook2, JDK and Solaris JumpStart are trademarks
# or registered trademarks of Sun Microsystems, Inc. in the U.S. and other
# countries.  All SPARC trademarks are used under license and are trademarks
# or registered trademarks of SPARC International, Inc. in the U.S. and other
# countries.  Federal Acquisitions: Commercial Software - Government Users
# Subject to Standard License Terms and Conditions.
# 
# Copyright  2001 Sun Microsystems, Inc. Tous droits rservs.
# Distribu par des licences qui en restreignent l'utilisation.  Le logiciel
# dtenu par des tiers, et qui comprend la technologie relative aux polices de
# caractres, est protg par un copyright et licenci par des fournisseurs de
# Sun.  Des parties de ce produit pourront tre drivs des systmes Berkeley
# BSD licencis par l'Universit de Californie.  Sun, Sun Microsystems, le
# logo Sun, Java, JMX, Netra, Solaris, Sun StorEdge, iPlanet, docs.sun.com,
# JDK, AnswerBook2 et Solaris JumpStart sont des marques de fabrique ou des
# marques dposes de Sun Microsystems, Inc. aux Etats-Unis et dans d'autres
# pays. Toutes les marques SPARC sont utilises sous licence et sont des
# marques de fabrique ou des marques dposes de SPARC International, Inc. aux
# Etats-Unis et dans d'autres pays.
#
#END_OF_COPYRIGHT_NOTICE

#set -x

#----------------------------------------------------
#
#  usage
#
#----------------------------------------------------
help () {
    $ECHO "Usage: $0  [ arguments ] [ stage ]"
    $ECHO ""
    $ECHO "  arguments:"
    $ECHO "    -h this help"
    $ECHO "    -r <directory path for customer installation configuration files>"
    $ECHO "    -l <log file>"
    $ECHO ""
    $ECHO "  stage:"
    $ECHO "    all (default):        installation of Solaris and NetraHAS 2.0 Foundation Services"
    $ECHO "    application-services: installation of application services on a NetraHAS 2.0 cluster"
    $ECHO "    reset:                reset all stages information (to restart a full installation from scratch)"
    $ECHO "    clear:                unshare all directories and remove all temporary files"
}

print_error () {
    $ECHO "ERROR: $1"
}

#----------------------------------------------------
#
#  check that a file is readable
#
#----------------------------------------------------
check_file () {
    if [ ! -r $1 ]
    then
	print_error "Can't read $1"
	exit 1
    fi
}

#----------------------------------------------------
#
# check that a directory exists
#
#----------------------------------------------------

check_dir () {
    if [ ! -d $1 ]
    then
	print_error "Can't acess directory $1"
	exit 1
    fi
}

#----------------------------------------------------
#
# check that the stage is correct
#
#----------------------------------------------------
check_stages () {

    case $stage in
    'all')
	;;
    'reset')
	;;
    'clear')
	;;
    'application-services')
	;;
    *)
	print_error "$stage: invalid stage"
	exit 1
	;;
    esac
}

#----------------------------------------------------
#
# create working directories
# 
# the argument is the name and not the value
# (to display clear messages)
#
#----------------------------------------------------

process_var_dir () {
    var=$1
    dirname=`eval 'echo $'$var''`

    if [ -z "$dirname" ]
    then
	print_error "variable $var not defined"
	exit 1
    fi
    if [ ! -d $dirname ]
    then
	$MKDIR -p $dirname
	if [ $? -ne 0 ]
	then
	    print_error "$dirname: directory creation failed (please check the $var variable)"
	    exit 1
	fi
    fi
}

#----------------------------------------------------
#
# check if a directory exists and if it can be exported
#
#----------------------------------------------------
check_var_share () {
    var=$1
    dirname=`eval 'echo $'$var''`
    mode=$2

    if [ -z "$dirname" ]
    then
	print_error "variable $var not defined"
	exit 1
    fi
    if [ ! -d $dirname ]
    then
	print_error "$dirname doesn't exist (please check the $var variable)"
	exit 1
    fi
    $SHARE -F nfs -o $mode,anon=0 $dirname
    if [ $? -ne 0 ]
    then
	print_error "$dirname can't be shared: please check"
	exit 1
    fi
}

#----------------------------------------------------
#
# check if a directory is exported and unshare it
#
#----------------------------------------------------
check_and_unshare () {
    dirname=$1

    if [ ! -z "$dirname" ]
    then
	$UNSHARE $dirname
    fi
}

#----------------------------------------------------
#
# check if a directory exists and remove it
#
#----------------------------------------------------
check_and_remove() {
    dirname=$1

    if [ -d "$dirname" ]
    then
	$RM -Rf $dirname
    fi
}

#----------------------------------------------------
#
# check if a directory exists and remove it
#
#----------------------------------------------------
check_display_setting() {

    $ECHO "Installation setting"

    if [ -z "$AUTO_REBOOT" ]
    then
	$ECHO ". Auto reboot: NO (default)"
	AUTO_REBOOT=NO
    elif [ "$AUTO_REBOOT" != "NO" ] && [ "$AUTO_REBOOT" != "YES" ]
    then
	$ECHO "AUTO_REBOOT: incorrect setting"
	exit 1
    else
	$ECHO ". Auto reboot: $AUTO_REBOOT"
    fi

    if [ -z "$OPTIMIZED_INSTALL" ]
    then
	$ECHO ". Optimized install: NO (default)"
	OPTIMIZED_INSTALL=NO
    elif [ "$OPTIMIZED_INSTALL" != "NO" ] && [ "$OPTIMIZED_INSTALL" != "YES" ]
    then
	$ECHO "OPTIMIZED_INSTALL: incorrect setting"
	exit 1
    else
	$ECHO ". Optimized install: $OPTIMIZED_INSTALL"
    fi

    if [ -z "$SOLARIS_INSTALL" ]
    then
	$ECHO ". Solaris install: YES (default)"
	SOLARIS_INSTALL=YES
    elif [ "$SOLARIS_INSTALL" != "NO" ] && [ "$SOLARIS_INSTALL" != "YES" ]
    then
	$ECHO "SOLARIS_INSTALL: incorrect setting"
	exit 1
    else
	$ECHO ". Solaris install: $SOLARIS_INSTALL"
    fi
}

#----------------------------------------------------
#
#  MAIN
#
#----------------------------------------------------

#
# define variables only used by the shell script
# before all variables are exported
#
# Default stage: all
stage=all

# force the export of all newly create variables
set -a

#
#  Directory location
#
WHENCE=`whence $0`
NHINSTALL_SBIN=`dirname $WHENCE`
NHINSTALL_LIB=$NHINSTALL_SBIN/../lib/ins
NHINSTALL_CONF=$NHINSTALL_SBIN/../lib/ins/conf

#
# default values for file and directory names
#

#
# user specific
#
# user configuration directory
NH_USR_CONF_DIR=

#
# file name for user configuration
#
# environment file name
NH_ENV_FILE=env_installation.conf
# cluster configuration file name
NH_CLUSTER_FILE=cluster_definition.conf
# add-on description file
NH_ADDON_FILE=addon.conf

#
# product specific
#
# partial node profile
NHINSTALL_PROFILE_FILE=$NHINSTALL_CONF/node.prof.template
# product configuration file
NHINSTALL_PRODUCT_FILE=$NHINSTALL_CONF/product_description.conf

NH_LOG_FILE=""

#
# check if parameters are valid
#

while [ ! -z "$1" ]
do
    case $1 in
    '-r')
	shift
	# transform a relative path to an absolute path because
	# the current directory will be change
	if [ -d $1 ]
	then
	    cd $1
	    NH_USR_CONF_DIR=`pwd`
	else
	    $ECHO "$1: not a valid directory"
	    exit 1
        fi
	;;
    '-p')
	shift
	NHINSTALL_PRODUCT_FILE=$1
	;;
    '-l')
	shift
	NH_LOG_FILE=$1
	;;
    '-h')
	help
	exit 0
	;;
    '-[.]*')
	$ECHO "$1: invalid argument"
	exit 1
	;;
    *)
	stage=$1
	;;
    esac
    shift
done

check_dir $NH_USR_CONF_DIR
NH_CLUSTER_FILE_FULLNAME=$NH_USR_CONF_DIR/$NH_CLUSTER_FILE
NH_ENV_FILE_FULLNAME=$NH_USR_CONF_DIR/$NH_ENV_FILE
NH_ADDON_FILE_FULLNAME=$NH_USR_CONF_DIR/$NH_ADDON_FILE

#
# check the required files are present (Makefile errors are not easy to use)
#
check_stages

#
# define user environment and product characteristics
# (add-on file is not mandatory)
#
check_file $NH_ENV_FILE_FULLNAME
. $NH_ENV_FILE_FULLNAME
check_file $NHINSTALL_PRODUCT_FILE
. $NHINSTALL_PRODUCT_FILE

#
# WORKING_DIR is used internally to define STAGES_DIR, STAGES_DIKSLESS_DIR and
# JUMPSTART_DIR
# so if it was not defined, other variables are probably
# not what the user is expecting for
#
if [ -z "$WORKING_DIR" ]
then
    print_error "variable WORKING_DIR not defined"
    exit 1
fi

process_var_dir STAGES_DIR
process_var_dir STAGES_DISKLESS_DIR

#
# to ease the clean-up without changing current design: 
# WORKING_DIR is redefined as a sub-directory after other sub-directories
# have been defined
# So, except directories, no file are created on the WORKING_DIR
# as specified by the user.
#

WORKING_DIR=$WORKING_DIR/nhfiles
process_var_dir WORKING_DIR

#
# perform checking: depends on the target
#


case $stage in

    'all')
	if [ ! -z $NH_LOG_FILE ]
	then
	    $ECHO "" >> $NH_LOG_FILE
	    $ECHO "Netra HA Suite 2.0 Solaris and Foundation Services installation" >> $NH_LOG_FILE
	    $ECHO "" >> $NH_LOG_FILE
	fi
	check_display_setting
	check_file $NH_CLUSTER_FILE_FULLNAME
	check_var_share NHAS2_PRODUCT_DIR ro
	if [ "$SOLARIS_INSTALL" != "NO" ]
	then
	    check_var_share SOLARIS_DIR ro
	    check_var_share JDK_DIR ro
	fi
	process_var_dir JUMPSTART_DIR
	check_var_share JUMPSTART_DIR rw
        cd $NHINSTALL_LIB
        /usr/ccs/bin/make -s target_os=solaris_8 $stage
	;;

    'application-services')
	if [ ! -z $NH_LOG_FILE ]
	then
	    $ECHO "" >> $NH_LOG_FILE
	    $ECHO "Netra HA Suite 2.0 Application Services installation" >> $NH_LOG_FILE
	    $ECHO "" >> $NH_LOG_FILE
	fi
	check_display_setting
	check_file $NH_CLUSTER_FILE_FULLNAME
	check_var_share NHAS2_PRODUCT_DIR ro
        cd $NHINSTALL_LIB
        /usr/ccs/bin/make -s target_os=solaris_8 $stage
	;;

    'clear')
	if [ ! -z $NH_LOG_FILE ]
	then
	    /usr/bin/cat /dev/null > $NH_LOG_FILE
	fi
        $ECHO "Clearing of temporary files and working directories"
	check_and_unshare $NHAS2_PRODUCT_DIR
	check_and_unshare $JUMPSTART_DIR
	if [ "$SOLARIS_INSTALL" != "NO" ]
	then
	    check_and_unshare $SOLARIS_DIR
	    check_and_unshare $JDK_DIR
	fi
	check_and_remove $STAGES_DIR
	check_and_remove $STAGES_DISKLESS_DIR
	check_and_remove $JUMPSTART_DIR
	check_and_remove $WORKING_DIR
        $ECHO "Clearing performed"
        ;;

    'reset')
	if [ ! -z $NH_LOG_FILE ]
	then
	    /usr/bin/cat /dev/null > $NH_LOG_FILE
	fi
        $ECHO "Resetting of progress indicators"
        cd $NHINSTALL_LIB
        /usr/ccs/bin/make -s target_os=solaris_8 $stage
	$ECHO "Reset done"
	;;
esac
