#!/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/>.
#############################################################################

# These are functions to be called by magpie-run

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-log
source ${MAGPIE_SCRIPTS_HOME}/magpie/lib/magpie-lib-paths

Magpie_run_start_mahout () {
    if [ "${MAHOUT_SETUP}" == "yes" ] && [ "${magpie_run_prior_startup_successful}" == "true" ]
    then
        if [ "${magpie_run_hadoop_setup_successful}" == "0" ]
        then
            Magpie_output_internal_warning "Attempt to setup Mahout without Hadoop being setup"
        fi

        if [ "${MAHOUT_OPTS}X" != "X" ]
        then
            if ! echo ${MAHOUT_OPTS} | grep -q -E "java.io.tmpdir"
            then
                export MAHOUT_OPTS="${MAHOUT_OPTS} -Djava.io.tmpdir=${MAHOUT_LOCAL_SCRATCHSPACE_DIR}/tmp"
            fi
        else
            export MAHOUT_OPTS="-Djava.io.tmpdir=${MAHOUT_LOCAL_SCRATCHSPACE_DIR}/tmp"
        fi

        echo "*******************************************************"
        echo "*"
        echo "* Mahout Information"
        echo "*"
        echo "* To run Mahout directly, follow above instructions for"
        echo "* to your allocation then:"
        echo "*"
        if echo $MAGPIE_SHELL | grep -q csh
        then
            echo "*   setenv MAHOUT_HOME \"${MAHOUT_HOME}\""
            if [ "${MAHOUT_OPTS}X" != "X" ]
            then
                echo "*   setenv MAHOUT_OPTS \"${MAHOUT_OPTS}\""
            fi
        else
            echo "*   export MAHOUT_HOME=\"${MAHOUT_HOME}\""
            if [ "${MAHOUT_OPTS}X" != "X" ]
            then
                echo "*   export MAHOUT_OPTS=\"${MAHOUT_OPTS}\""
            fi
        fi
        echo "*"
        echo "* Then you can do as you please.  For example to run a job:"
        echo "*"
        echo "*   \$MAHOUT_HOME/${mahoutcmdprefix}/mahout ..."
        echo "*"
        if [ "${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}X" != "X" ]
        then
            echo "* If running interactively, sourcing"
            echo "*"
            echo "* ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}"
            echo "*"
            echo "* will set most common environment variables for your job."
            echo "*"
        fi
        echo "*******************************************************"

        if [ "${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}X" != "X" ]
        then
            if echo $MAGPIE_SHELL | grep -q csh
            then
                echo "setenv MAHOUT_HOME \"${MAHOUT_HOME}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                if [ "${MAHOUT_OPTS}X" != "X" ]
                then
                    echo "setenv MAHOUT_OPTS \"${MAHOUT_OPTS}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                fi
            else
                echo "export MAHOUT_HOME=\"${MAHOUT_HOME}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                if [ "${MAHOUT_OPTS}X" != "X" ]
                then
                    echo "export MAHOUT_OPTS=\"${MAHOUT_OPTS}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                fi
            fi
            echo "" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
        fi
    fi
}

Magpie_run_mahout () {
    if [ "${MAHOUT_JOB}" == "clustersyntheticcontrol" ]
    then
        echo "*******************************************************"
        echo "* Running Clustersyntheticcontrol"
        echo "*******************************************************"
        ${MAGPIE_SCRIPTS_HOME}/magpie/run/magpie-run-execute script ${MAGPIE_SCRIPTS_HOME}/magpie/job/magpie-job-mahout-clustersyntheticcontrol &
        local scriptpid=$!
        Magpie_wait_script_sigusr2_on_job_timeout ${scriptpid}
    else
        Magpie_output_internal_error "MAHOUT_JOB = ${MAHOUT_JOB} not handled"
    fi
}
