#!/bin/sh
# aginies at mandrakesoft.com
# version: 0.2 
# launch with "c" to get color while running


## color
if [ "$1" = "c" ]; then
    NORMAL=`echo -en "\\033[0;32m"`
    SUCCESS=`echo -en "\\033[1;32m"`
    INFO=`echo -en "\\033[1;34m"`
    WARNING=`echo -en "\\033[1;33m"`
    WHITE=`echo -en "\\033[0;39m"`
else 
    NORMAL=
    SUCCESS=
    INFO=
    WARNING=
    WHITE=
fi



UBIN="/usr/bin"
TMPLOG="/tmp/log"
CONFFILE=/etc/clusterserver.conf
MAUI="/usr/bin/maui"

rm -rf ${TMPLOG}
mkdir -p ${TMPLOG}
rm -rf  /tmp/nocd


clear
echo " |---------------------------------|" 
echo " |   AUTO SETUP CLUSTER SERVER !   |"
echo " |---------------------------------|"
echo 
#RE=`cat /etc/mandrake-release | grep CLIC`
#if [ -z "$RE" ]; then 
#    if [ ! -z "$DISPLAY" ]; then
#	/usr/bin/draktab_config
#    fi
##    echo " You can adjust VAR with drakcluster.pl"
#else
    echo " You can adjust your var in $CONFFILE"
#fi
echo
echo " This script is going to automaticaly execute in order:"
echo " setup_xconfig.pl doall"
echo " setup_install_cluster.pl doall"
echo " setup_server_cluster.pl gennodeone"
echo " setup_server_cluster.pl doall"
echo " setup_dns.pl master"
echo " setup_nis.pl set_server"
echo " setup_dhcpdconf_server.pl doall"
echo " setup_pxe_server.pl build"
echo " setup_pxe_server.pl setup linux"
echo " setup_pbs.pl doall"
echo " setup_admin.pl doall"
echo " setup_compute.pl doall"
echo " setup_pbs_client $HOSTNAME"
if [ -e "$MAUI" ]; then
    echo " setup_maui.pl doall"
fi
echo " setup_postfix.pl set"
echo " setup_add_nodes_to_dhcp.pl -n number_of_nodes"
echo
echo " Be sure that the CDROM is present (first launch)"
# eject
echo " (press ENTER when ready)"
read
mount /dev/cdrom0 /mnt/cdrom


echo " - Stoping PBS to avoid Problem of ARP"
service openpbs stop
echo " - Stoping Gmond to avoid gstat core pb"
service gmond stop 


launch_script() {
        if [ -f /tmp/nocd ]; then break ; exit; fi
        clear
        rm -f ${TMPLOG}/${1}.log
        echo ${INFO}
        echo
        echo "  ---------------------------------"
        echo "   LAUNCH: ${1}"
        echo
        echo "   LOG can be found in:"
        echo "   ${TMPLOG}/${1}.log"
        echo "  ---------------------------------"
        echo 
        sleep 2
        echo ${NORMAL}
        ${UBIN}/${1} ${2} ${3} 2> ${TMPLOG}/${1}.log

# test size of the log
SIZELOG=`du -s ${TMPLOG}/${1}.log | awk '{print $1}'`
if [ "${SIZELOG}" -eq "0" ]; then
        rm -f ${TMPLOG}/${1}.log
fi
}

test_script_log() {
if [ -f "${TMPLOG}/${1}.log" ]; then
        echo ${WARNING}
        echo 
        echo "     ${1} finish with LOG"
        echo "     Look in ${TMPLOG}/${1}.log for more information"
        echo
        echo " (press ENTER key to continue)"
        read
        echo ${NORMAL}
else
        echo ${SUCCESS}
        echo  
        echo " ${1} finish with SUCCESS"
        echo
        echo " (press ENTER key to continue)"
        read
        echo ${NORMAL}
fi
}

echo 
launch_script setup_xconfig.pl doall
test_script_log setup_xconfig.pl

launch_script setup_install_cluster.pl doall
test_script_log setup_install_cluster.pl

launch_script setup_server_cluster.pl gennodeone

launch_script setup_server_cluster.pl doall
test_script_log setup_server_cluster.pl

launch_script setup_dns.pl master
test_script_log setup_dns.pl

launch_script setup_nis.pl set_server
test_script_log setup_nis.pl

launch_script setup_dhcpdconf_server.pl doall
test_script_log setup_dhcpdconf_server

launch_script setup_pxe_server.pl build
launch_script setup_pxe_server.pl setup auto
test_script_log setup_pxe_server

launch_script setup_pbs.pl doall
test_script_log setup_pbs.pl

launch_script setup_admin.pl doall
test_script_log setup_admin.pl

launch_script setup_compute.pl doall
test_script_log setup_compute.pl

echo " - Restarting pbs service"
service pbs_server restart

if [ -e "$MAUI" ]; then
    launch_script setup_maui.pl doall
    echo " - Please enter maui user password:"
    passwd maui
    echo
    test_script_log setup_maui.pl
    else
    # fake maui configuration to avoid pb with all others scripts
    mkdir -p /var/spool/maui
    touch /var/spool/maui/maui.cfg
    perl -Mmaui_cluster -e "maui_config()"
    perl -Mmaui_cluster -e "maui_node()"
fi

launch_script setup_postfix.pl set
test_script_log setup_postfix.pl

clear
echo " - Restarting gmond, gmetad, pbs_mom on server"
service gmond start
service gmetad restart
service pbs_mom restart

echo "add user admin (smartd and pbs)\n";
adduserNis.pl -l admin -p "\$1\$pNpuzDnO\$iZIgCN/LNI41GhqY9son50"
echo " - Please enter admin user password:"
passwd admin

echo "rebuilding NIS map"
cd /var/yp
make

clear
echo ${INFO}
TESTLOG=`ls ${TMPLOG}/ | grep log`
if [ ! -z "${TESTLOG}" ]; then
        echo " found log(s) in ${TMPLOG}:"
        ls ${TMPLOG}/*.log
fi
echo ${WHITE} 
echo " Server ${SUCCESS}READY${WHITE}, now install a client"
echo
echo " Install a golden node and replicate it with ka tools"
echo " When all nodes are ready, and if they have two network interfaces do a:"
echo "setup_add_nodes_to_dhcp.pl -c"
echo " Your nodes will be ready to use their compute interface"
echo

if [ ! -z "$DISPLAY" ]; then
    nohup xterm -title "Now collecting MAC address of First node" -ls -reverse -e setup_add_nodes_to_dhcp.pl -n 1
else
    setup_add_nodes_to_dhcp.pl -n 1
fi

echo
echo " - Restarting dhcpd server"
service dhcpd restart
echo
if [ -f	"/usr/bin/idesk" ]; then
    nohup /usr/bin/idesk &
fi

echo " (press ENTER key to exit)"
read
