#!/bin/bash

#-----------------------------------------------------------
# $Id: distexe.sh 112 2009-03-18 22:36:26Z rosiere $
#-----------------------------------------------------------

# Need : test, echo, cd, dirname, basename, ssh

#-----[ distexe_usage ]-------------------------------------
function distexe_usage ()
{
    echo "Usage     : ${0} file [ dir ]";
    echo "Arguments : ";
    echo " * file       : list of command";
    echo " * dir        : work directory (default is current directory).";
#   echo " * nb_process : number of process (default (and maximum) is the number of processor)";
    echo "";
    echo "Note      : ";
    echo " * File content list of command. Each line is execute by an host.";
    echo "   A line can content many shell command.";
    echo "   Don't forgot the final end of line (else the last command is not executed)";
    echo "   example : ";
    echo '   echo "export PATH=${PATH}:${HOME}/my_appli/bin; my_appli 13"  > command_file.txt';
    echo '   echo "export PATH=${PATH}:${HOME}/my_appli/bin; my_appli 21" >> command_file.txt';
    echo '   echo "export PATH=${PATH}:${HOME}/my_appli/bin; my_appli  7" >> command_file.txt';
    echo '   echo ""                                                      >> command_file.txt';    
    echo " * For each command, a directory (in work directory) is created : Task_X (X is the number of line in command file).";
    echo "   The command is execute in this directory. (Warning, set your environment !).";
    echo "   Two file is generate : \"distexe.output\" is the output (standard and error) of the execution, and \"distexe.command\" is the command lunch.";
#   echo " * A command empty (no command on a line of file) is a synchronisation with all process"
    echo " * The environment variable DISTEXE_HOSTS list hosts to execute command";
    echo "   After each host, a number is to the number of process. It's optionnal, the default value is the number of processor.";
    echo "   example :";
    echo '   export DISTEXE_HOSTS="localhost/1 nod/4 gdi/2"';
    echo " * All hosts must have network file systems (per example nfs or samba)";
    echo "   Work directory must be in this network file systems !";
    echo "";
    exit;
}

#-----[ distexe_test_usage ]--------------------------------
function distexe_test_usage ()
{
    if test ${#} -ne 1 -a ${#} -ne 2; then
	distexe_usage;
    fi;

    if test -z "${MORPHEO_SCRIPT}"; then
        echo "Environment variable MORPHEO_SCRIPT is not set";
        distexe_usage;
    fi;

    if test ! -f ${1}; then
	echo "File \"${1}\" is invalid";
	distexe_usage;
    fi;

    if test ! -s ${1}; then
	echo "File \"${1}\" is empty.";
	distexe_usage;
    fi;

    if test ${#} -eq 2; then
        if test ! -d ${2}; then
	    echo "Directory \"${2}\" is invalid.";
	    distexe_usage;
        fi;
    fi;

}

#-----[ distexe ]-------------------------------------------
function distexe ()
{
    distexe_test_usage ${*};

    # construct file command with absolute path
    cd $(dirname ${1});
    local FILE_CMD=${PWD}/$(basename ${1});
    cd -;

    # Absoulte path of work directory
    local    DIR_EXE;
    if test ${#} -eq 2; then
        cd ${2};
        DIR_EXE=${PWD};
        cd -;
    else
        DIR_EXE=${PWD};
    fi;

    local hosts="${DISTEXE_HOSTS}";

    for line in ${hosts}; do
        local    host=$(echo ${line} | cut -d/ -f1);
        local -i nb_process=$(echo ${line} | cut -d/ -f2);

        echo " * station : ${host} (${nb_process}) ... ";

        # lunch service
        local cmd="export MORPHEO_SCRIPT=${MORPHEO_SCRIPT};${MORPHEO_SCRIPT}/execute_n.sh ${DIR_EXE} ${FILE_CMD} ${nb_process};";
        ssh ${host} ${cmd} &
    done;
}

#-----[ Corps ]---------------------------------------------
distexe ${*};
