#!/usr/bin/bash
#############################################################################
#  Copyright (C) 2013-2015 Lawrence Livermore National Security, LLC.
#  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
#  Written by Albert Chu <chu11@llnl.gov>
#  LLNL-CODE-644248
#
#  This file is part of Magpie, scripts for running Hadoop on
#  traditional HPC systems.  For details, see https://github.com/llnl/magpie.
#
#  Magpie is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  Magpie is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with Magpie.  If not, see <http://www.gnu.org/licenses/>.
#############################################################################


source ${MAGPIE_SCRIPTS_HOME}/magpie/exports/magpie-exports-submission-type
source ${MAGPIE_SCRIPTS_HOME}/magpie/exports/magpie-exports-dirs
source ${MAGPIE_SCRIPTS_HOME}/magpie/exports/magpie-exports-user
source ${MAGPIE_SCRIPTS_HOME}/magpie/lib/magpie-lib-node-identification
source ${MAGPIE_SCRIPTS_HOME}/magpie/lib/magpie-lib-setup
source ${MAGPIE_SCRIPTS_HOME}/magpie/lib/magpie-lib-paths

if Magpie_am_I_master
then
    echo "*******************************************************"
    echo "* Performing Post Setup"
    echo "*******************************************************"
fi

if [ "${HADOOP_SETUP}" == "yes" ]
then
    #
    # Perform HDFS format if necessary
    #

    # will set hadoopnoderank
    if Magpie_am_I_a_hadoop_node \
        && Magpie_hadoop_filesystem_mode_is_hdfs_type
    then
        # will set magpie_hdfs_format_file
        Magpie_calculate_hdfs_format_file_path ${hadoopnoderank}

        if [ ! -f ${magpie_hdfs_format_file} ]
        then
            # Only master will format the node
            if Magpie_am_I_master
            then
                echo "*******************************************************"
                echo "* Formatting HDFS Namenode"
                echo "*******************************************************"
                cd $HADOOP_HOME
                    # Don't check version, legacy 0.2X versioning could be used
                if [ -x "${hadoopcmdprefix}/hdfs" ]
                then
                    echo 'Y' | ${hadoopcmdprefix}/hdfs namenode -format
                else
                    echo 'Y' | ${hadoopcmdprefix}/hadoop namenode -format
                fi
                touch ${magpie_hdfs_format_file}
            else
                # If this is the first time running, make everyone else wait
                # until the format is complete
                sleep 30
            fi
        fi
    fi
fi

if Magpie_am_I_master
then
    echo "*******************************************************"
    echo "* Post Setup Complete"
    echo "*******************************************************"
fi

exit 0