#!/bin/bash

#./Dirigent.sh -r PingCheck
function PingCheck(){
    $LogFunctionStart
    for Dev in $ALLDevs;
        do 
            ping -q -c 1 $Dev >/dev/null;
            if [ $? -ne 0 ]; then 
                LogIt "Ping $Dev ...KO, problem"; critical_error "ping $Dev KO";exit 1
            else 
                LogIt "Ping $Dev ...OK;"
            fi;
       done;\
    $LogFunctionEnd
}

function Broadcast(){
    LogIt "********* Broadcasting `echo "$1" | tr a-z A-Z`"
    for Dev in $RASPs;do
        LogIt "Musician RASP:$Dev .. playing"
        if [ `ssh golem@$Dev "source $Dev.sh;$@"|tail -1` != "OK" ]; then critical_error "Broadcast $Dev $1 KO";exit 1;fi &
	done
	for Dev in $DASs;do
        LogIt "Musician DAS:$Dev .. playing"
        if [ `cd $SHM0/DASs/$Dev/; source notation.sh;$@|tail -1` != "OK" ]; then critical_error "Broadcast $Dev $1 KO";exit 1;fi &
	done
	wait
    LogIt "*********`echo "$1" | tr a-z A-Z` end"
}

function pull_newest_gitlab(){
    $LogFunctionStart
    cd ~
    if ! [ -d .git/ ] ; then    # Repository not initialized
        tmpdir=$(mktemp -d)
        git clone --no-checkout $GitlabUpstream $tmpdir
        mv $tmpdir/.git .
        rmdir $tmpdir
        git reset --hard HEAD
    fi
    git pull
    cd -                        # go back
    $LogFunctionEnd

        
}

function RsyncDevices(){
    $LogFunctionStart
    for Dev in $RASPs; do
       echo $Dev sync ..
       #rsync --copy-links  -r -e ssh $HOME/Infrastructure/$Dev.sh $HOME/Tools.sh $HOME/Commons.sh $HOME/Drivers golem@$Dev:
       rsync --copy-links  -r -e ssh Infrastructure Tools.sh Commons.sh Drivers golem@$Dev:
    done
    $LogFunctionEnd
}

function RsyncDeviceFromDirigent(){
      echo "Syncing $ThisDev.sh ."
      rsync golem@Dirigent:$HOME/$SUBDIR/$ThisDev.sh .
      rsync golem@Dirigent:$HOME/Commons.sh .
      rsync golem@Dirigent:$HOME/Tools.sh .
}

function mRelax(){
    sleep 0.1
}

function Relax(){
    sleep 1
}

function KillAllGMs(){
#./Dirigent.sh -k
    killall -u golem
}

function EditHosts(){
#./Dirigent.sh -r EditHosts (obsolete, better ./Dirigent.sh -r DistributeHosts)
    $LogFunctionStart
    for Dev in Dirigent $RASPs; do echo doing Dev: $Dev; ssh golem@$Dev -Y xterm -e "sudo emacs -nw /etc/hosts";done
    $LogFunctionEnd
}

function DistributeHosts(){
    #./Dirigent.sh -r DistributeHosts
    $LogFunctionStart
    for Dev in $RASPs; do echo doing Dev: $Dev; rsync --rsync-path="sudo rsync"  Support/RASPSetup/hosts pi@$Dev:/etc/hosts;done
    $LogFunctionEnd

}
