#! /bin/bash
MYNAME=$(basename $0)
MYDIR=$(dirname $0)
MYDIR=$(readlink -m "$MYDIR")
VERSION="0.0.4"
VERBOSE=""
LOGFILE=""
SERVER=""

if [ -f "$MYDIR/utilityfunctions.sh" ]; then
    source $MYDIR/utilityfunctions.sh
else
    echo "File '$MYDIR/utilityfunctions.sh' was not found." >&2
    exit 5
fi

function printHelpAndExit()
{
cat <<EOF
Usage:
  $MYNAME [OPTION]... 

  $MYNAME - Concurrent ssh to multiple servers.

  -h, --help           Print this help and exit.
  -v, --version        Print version information and exit.
  --verbose            Print more messages.
  --log-file=FILE      Store all the messages in the given file too.
 
  --all                Try to SSH to all known servers.
  --idle               SSH to the idle server(s).
  --online             SSH to all online servers.
  --server=LIST        SSH to the given server(s).

EOF
    exit 0
}


ARGS=$(\
    getopt \
        -o hvs:c:l \
        -l "help,verbose,version,log-file:,server:,blades,all,idle,online" \
        -- "$@")

if [ $? -ne 0 ]; then
    exit 6
fi

eval set -- "$ARGS"
while true; do
    case "$1" in
        -h|--help)
            shift
            printHelpAndExit
            ;;

        --verbose)
            shift
            VERBOSE="true"
            VERBOSE_OPTION="--verbose"
            ;;

        -v|--version)
            shift
            VERSION_OPTION="--version"
            ;;

        --log-file)
            shift
            LOGFILE=$(readlink -m "$1")
            shift
            ;;

        --server)
            shift
            SERVER=$(echo "$1" | tr ',' ' ')
            shift
            ;;

        --all)
            shift
            ALL_OPTION="true"
            ;;

        --online)
            shift
            ONLINE_OPTION="true"
            ;;
        
        --idle)
            shift
            IDLE_OPTION="true"
            ;;

        --blades)
            shift
            if [ -f "$HOME/.pip/blades" ]; then
                SERVER=$(cat "$HOME/.pip/blades" | tr ',' ' ')
            else
                SERVER="blade01 blade02 blade03 blade04 blade05 "
                SERVER+="blade06 blade07 blade08 blade09 blade10"
                echo "$SERVER" >"$HOME/.pip/blades"
            fi
            ;;

        --)
            shift
            break
            ;;

        *)
            ;;
    esac
done

command_line="clusterssh"
option_found=""

if [ -n "$ALL_OPTION" ]; then
    for server in $(pip-server-control --list); do
        command_line+=" $server"
    done

    option_found="true"
elif [ -n "$ONLINE_OPTION" ]; then
    for server in $(pip-server-control --list --online); do
        command_line+=" $server"
    done

    option_found="true"
elif [ -n "$IDLE_OPTION" ]; then
    for server in $(pip-server-control --list --idle); do
        command_line+=" $server"
    done

    option_found="true"
elif [ -n "$SERVER" ]; then
    for server in $(echo $SERVER | tr ',' ' '); do
        command_line+=" $server"
    done

    option_found="true"
fi

$command_line
