#!/bin/ksh
#
# ident	"@(#)nhinstall	1.33	06/01/25 SMI"
#
# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

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

# this script needs super-user 
user_id="`id | awk '{print $1}'`"
user_id="`expr $user_id : 'uid=\([0-9][0-9]*\)'`"
if [ $user_id != 0 ]; then
    $ECHO "You have to be the super-user to run Netra HA Suite installation."
    $ECHO "Please log in as root."
    exit 1
fi

#$ECHO "Netra HA Suite 2.1 Update 2 installation"

# $CAT <<END_OF_COPYRIGHT_NOTICE
# 
# Copyright 2006 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 2006 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  -r <directory> [ -h ] [ -l logfile ] [ stage ]"
    $ECHO ""
    $ECHO "  Installation and configuration of Solaris and NetraHAS 2.1 Foundation Services"
    $ECHO ""
    $ECHO "  arguments:"
    $ECHO "    -h this help"
    $ECHO "    -r <directory path for customer installation configuration files>"
    $ECHO "    [ -l <log file> ]"
    $ECHO ""
    $ECHO "  stage:"
    $ECHO "    reset     reset all stages information (to restart a full installation from"
    $ECHO "              scratch)"
    $ECHO "    clear     unshare all directories and remove all temporary files"
    $ECHO "    add <node id> [ <node id> ...]"
    $ECHO "              install and configure the listed nodes on a running cluster"
}

#----------------------------------------------------
#
#  log message
#
#----------------------------------------------------

log_msg () {

    if [ ! -z $NH_LOG_FILE ]
    then
        DATE=`/usr/bin/date '+%H:%M:%S'`
	$ECHO "$DATE $1" >> $NH_LOG_FILE
	if [ "$?" != 0 ]
	then
	    $ECHO "ERROR: Cannot append log file $NH_LOG_FILE"
	    exit 1
	fi
    fi
}

#----------------------------------------------------
#
#  print message
#
#----------------------------------------------------

print_msg () {
    $ECHO "$1"
    log_msg "$1"
}

#----------------------------------------------------
#
#  print error
#
#----------------------------------------------------

print_error () {
    $ECHO "ERROR: $1"
    log_msg "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 () {

    if [ -z "$stage" ]
    then
	stage=all
    fi

    case $stage in
    'all')
	;;
    'reset')
	;;
    'clear')
	;;
    'add')
        if [ -z "$node_list" ]
	then
	    print_error "id of node to add must be given"
	    help
	    exit 1
	fi
	NHINSTALL_NODE_LIST=$node_list
	;;
    *)
	print_error "$stage: invalid stage"
	help
	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 is exported and unshare it
#
#----------------------------------------------------
check_and_unshare () {
    dirname=$1

    if [ "$NHINSTALL_TESTING" == "conf" ]
    then
        return
    fi

    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
}

#----------------------------------------------------
#
#  process common definition
#
#----------------------------------------------------

process_common() {

    #
    # WORKING_DIR is used internally to define 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

#    print_msg ". Working directory: $WORKING_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.
    #

    ROOT_WORKING_DIR=$WORKING_DIR
    WORKING_DIR=$WORKING_DIR/nhfiles
    process_var_dir WORKING_DIR

    NHINSTALL_STATUS_FILE="$WORKING_DIR/nhinstall_status_file.dat"
}

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

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

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

#
#  Directory location
#
WHENCE=`whence $0`
NHINSTALL_SBIN=`dirname $WHENCE`
NHINSTALL_ROOT=$NHINSTALL_SBIN/../../..
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

NH_LOG_FILE=""
NHINSTALL_COMMON_FILE=$NHINSTALL_CONF/common.conf

#
# check if parameters are valid
#

while [ ! -z "$1" ]
do
    case $1 in
    -r)
	shift
	if [ -z "$1" ]
	then
	    print_error "directory path is missing"
	    help
	    exit 1
	fi
	# 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
	    print_error "$1: not a valid directory"
	    exit 1
        fi
	;;
    -l)
	shift
	if [ -z "$1" ]
	then
	    print_error "log file name is missing"
	    help
	    exit 1
	fi
	NH_LOG_FILE=$1
	;;
    -h)
	help
	exit 0
	;;
    -*)
	print_error "$1: invalid option"
	help
	exit 1
	;;
    *)
	if [ "$stage" == "add" ]
	then
	    node_list="$node_list $1"
        else
	    if [ -z "$stage" ]
	    then
	        stage=$1
	    else
	        print_error "$1: invalid argument"
		help
		exit 1
	    fi
        fi
	;;
    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

NH_OS_FILE_FULLNAME=$NHINSTALL_CONF/os_description.conf

#
# 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_COMMON_FILE
. $NHINSTALL_COMMON_FILE

PERL="perl -w -I $NHINSTALL_LIB/tools/lib -I $NHINSTALL_LIB/tools/install_os/common"


#
# perform checking: depends on the target
#

case $stage in

    'all')
	process_common
	check_file $NH_CLUSTER_FILE_FULLNAME
        cd $NHINSTALL_LIB
	$PERL $NHINSTALL_LIB/tools/lib/nhinstall.pl
	;;

    'add')
	process_common
	check_file $NH_CLUSTER_FILE_FULLNAME
        cd $NHINSTALL_LIB
	$PERL $NHINSTALL_LIB/tools/lib/nhinstall.pl
	;;


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

    'reset')
	if [ ! -z $NH_LOG_FILE ]
	then
	    /usr/bin/cat /dev/null > $NH_LOG_FILE
	fi
        print_msg "Resetting of progress indicators"
	print_msg "Installation setting"
	process_common
	if [ -f $NHINSTALL_STATUS_FILE ]
	then
	    $RM $NHINSTALL_STATUS_FILE
	fi
	print_msg "Reset done"
	;;
esac
