#################################################################
#
# Makefile to build the whole integration test suites
# on the Install Server host for Testing Purposes
#
#
# Copyright 2001 Sun Microsystems, Inc. All rights reserved.
#
##################################################################

MKDIR = /usr/bin/mkdir
RM = /usr/bin/rm
ECHO = /usr/bin/echo
TOUCH = /usr/bin/touch
SUM = /usr/bin/sum
DIFF = /usr/bin/diff

INSTALL_LIB_TOOLS=$(NHINSTALL_LIB)/tools/lib/

#
# the first time, ask for diskless nodes environment
# and compute the checksum for the cluster definition file
# (file ASK_FOR_DISKLESS_DONE is used to know if it's the first launch
# for next runs, the checksum is controlled)
#
# NB: comparison is done by using bdiff: it does cause make to abort when files are
#     differences (it does not set the exit code).
#
# test if diskless enviromnent was under installation (after a recovery)
# and call the script in charge of re-mounting the remote directories when
# the installation is optimized (this file is deleted at the end of the diskless installation)

initall:
	$(NHINSTALL_LIB)/tools/lib/check_conf.pl
	if [ ! -f $(STAGES_DIR)/ASK_FOR_DISKLESS_DONE ] ;\
	then \
	    NB=`$(NHINSTALL_LIB)/tools/lib/diskless_found.pl` ;\
	    if [ "$${NB}" -eq "0" ] ; \
	    then \
	      answer="" ;\
	      while [ "$${answer}" != "y" ] && [ "$${answer}" != "n" ] ;\
	      do \
		$(ECHO) "No diskless defined." ;\
		$(ECHO) "Do you want to install diskless environment anyway [y/n]: \c" ;\
		read answer ;\
		if [ "$${answer}" != "y" ] && [ "$${answer}" != "n" ] ;\
		then \
		    $(ECHO) "Incorrect reply" ; \
		fi ; \
	      done ; \
	      if [ "$${answer}" = "n" ] ;\
	      then \
	        $(TOUCH) $(STAGES_DIR)/NO_DISKLESS_ENV ;\
	      fi ; \
	    fi ; \
	    $(TOUCH) $(STAGES_DIR)/ASK_FOR_DISKLESS_DONE ;\
	    $(SUM) $(NH_CLUSTER_FILE_FULLNAME) > $(STAGES_DIR)/CLUSTERSUM ;\
	else \
	    $(SUM) $(NH_CLUSTER_FILE_FULLNAME) > $(STAGES_DIR)/TMPSUM ;\
	    res=`bdiff $(STAGES_DIR)/TMPSUM $(STAGES_DIR)/CLUSTERSUM ` ;\
	    if [ "$${res}" != "" ] ;\
	    then \
	        $(ECHO) "$(NH_CLUSTER_FILE_FULLNAME) changed since last run." ;\
	        $(ECHO) "This may cause the installation to be inconsistent." ;\
		$(ECHO) "Perform a reset to restart the installation from the beginning." ;\
	        answer="" ;\
	        while [ "$${answer}" != "y" ] && [ "$${answer}" != "n" ] ;\
	        do \
		  $(ECHO) "Do you want to continue the installation anyway [y/n]: \c" ;\
		  read answer ;\
		  if [ "$${answer}" != "y" ] && [ "$${answer}" != "n" ] ;\
		  then \
		    $(ECHO) "Incorrect reply" ; \
		  fi ; \
	        done ; \
	        if [ "$${answer}" = "n" ] ;\
	        then \
		  exit 1 ;\
	        fi ;\
	        $(SUM) $(NH_CLUSTER_FILE_FULLNAME) > $(STAGES_DIR)/CLUSTERSUM ;\
            fi ;\
	fi
	if [ -f $(STAGES_DISKLESS_DIR)/DISKLESS_INSTALL_RUNNING ] ;\
	then \
	    $(INSTALL_LIB_TOOLS)/mount_nmen.pl \
		-n 1 \
		$(DEBUG_FLAG) ;\
	fi

#
#  clear all progress indicators
#
reset:
	if [ -d $(STAGES_DIR) ] ;\
	then $(RM) -Rf $(STAGES_DIR) ;\
	fi
	if [ -d $(STAGES_DISKLESS_DIR) ] ;\
	then $(RM) -Rf $(STAGES_DISKLESS_DIR) ;\
	fi

install-solaris:
	cd found-services ; $(MAKE) install-solaris

install-foundation-services:
	cd found-services ; $(MAKE) install-foundation-services

install-application-services:
	cd appli-services ; $(MAKE) install-application-services

reboot-cluster:
	if [ ! -f $(STAGES_DIR)/NO_DISKLESS_ENV ] ;\
	then \
	    $(SILENT) echo "On the console of each diskless node" ;\
	    $(SILENT) echo "- Type: halt" ;\
	    $(SILENT) echo "- Wait for console prompt ok" ;\
	    $(SILENT) echo "Answer y when ready !" ;\
	    ANSWER="n" ; while [ \"$${ANSWER}\" != \"y\" ] ;\
		do read ANSWER ;\
		done ;\
	fi
	$(SILENT) echo "On the console of vice master node"
	$(SILENT) echo "- Type: halt"
	$(SILENT) echo "- Wait for console prompt ok"
	$(SILENT) echo "Answer y when ready !"
	ANSWER="n" ; while [ \"$${ANSWER}\" != \"y\" ] ;\
		do read ANSWER ;\
		done
	$(SILENT) echo "On the console of master node"
	$(SILENT) echo "- Type: reboot"
	$(SILENT) echo "- Wait for Console Login Prompt "
	$(SILENT) echo "Answer y when ready !"
	ANSWER="n" ; while [ \"$${ANSWER}\" != \"y\" ] ;\
		do read ANSWER ;\
		done
	$(SILENT) echo "On the console of the former vice master node"
	$(SILENT) echo "- type: boot"
	$(SILENT) echo "- Wait for Console Login Prompt "
	$(SILENT) echo "Answer y when ready !"
	ANSWER="n" ; while [ \"$${ANSWER}\" != \"y\" ] ;\
		do read ANSWER ;\
		done
	if [ ! -f $(STAGES_DIR)/NO_DISKLESS_ENV ] ;\
	then \
	    $(SILENT) echo "On the console of each diskless node" ;\
	    $(SILENT) echo "- type: boot" ;\
	fi
	$(SILENT) echo "When rebooted, your cluster is ready !"

boot-diskless:
	if [ ! -f $(STAGES_DIR)/NO_DISKLESS_ENV ] ;\
	then \
	    $(SILENT) echo "Setup eeprom boot params on your diskless nodes :" ;\
	    $(SILENT) echo "Type at boot prompt :" ;\
	    $(SILENT) echo "	setenv local-mac-address? true" ;\
	    $(SILENT) echo "	setenv auto-boot-retry? true" ;\
	    $(SILENT) echo "	setenv boot-device net:dhcp,,,,,5 net2:dhcp,,,,,5" ;\
	    $(SILENT) echo "You can now boot your diskless nodes" ;\
	fi
	$(SILENT) echo "Your cluster is now ready."

foundation-services: initall install-foundation-services boot-diskless

application-services: initall install-application-services reboot-cluster

all: initall install-solaris install-foundation-services boot-diskless
