######################################
######################################
######################################
###  ____  ____   _    ____  _  __ ###
### / ___||  _ \ / \  |  _ \| |/ / ###
### \___ \| |_) / _ \ | |_) | ' /  ###
###  ___) |  __/ ___ \|  _ <| . \  ###
### |____/|_| /_/   \_\_| \_\_|\_\ ###
###                                ###
######################################
######################################
######################################

startsparkslave() {

      # go back to root directory
      cd 

      ##################################################################
      # search master IP
      ##################################################################

      success=0
      COUNTER=0
      
      while [ $success -eq 0 ]
      do
          COUNTER=$((COUNTER + 1))

          if [ $COUNTER -eq  1 ]; then IPX=$ip01; fi
          if [ $COUNTER -eq  2 ]; then IPX=$ip02; fi
          if [ $COUNTER -eq  3 ]; then IPX=$ip03; fi
          if [ $COUNTER -eq  4 ]; then IPX=$ip04; fi
          if [ $COUNTER -eq  5 ]; then IPX=$ip05; fi
          if [ $COUNTER -eq  6 ]; then IPX=$ip06; fi
          if [ $COUNTER -eq  7 ]; then IPX=$ip07; fi
          if [ $COUNTER -eq  8 ]; then IPX=$ip08; fi
          if [ $COUNTER -eq  9 ]; then IPX=$ip09; fi                                                                                       
          if [ $COUNTER -eq 10 ]; then IPX=$ip10; fi
          if [ $COUNTER -eq 11 ]; then IPX=$ip11; fi
          if [ $COUNTER -eq 12 ]; then IPX=$ip12; fi
          if [ $COUNTER -eq 13 ]; then IPX=$ip13; fi
          if [ $COUNTER -eq 14 ]; then IPX=$ip14; fi
          if [ $COUNTER -eq 15 ]; then IPX=$ip15; fi
          if [ $COUNTER -eq 16 ]; then IPX=$ip16; fi
          if [ $COUNTER -eq 17 ]; then IPX=$ip17; fi
          if [ $COUNTER -eq 18 ]; then IPX=$ip18; fi
          if [ $COUNTER -eq 19 ]; then IPX=$ip19; fi                                                                                       
          if [ $COUNTER -eq 20 ]; then IPX=$ip20; fi
          if [ $COUNTER -eq 21 ]; then IPX=$ip21; fi
          if [ $COUNTER -eq 22 ]; then IPX=$ip22; fi
          if [ $COUNTER -eq 23 ]; then IPX=$ip23; fi
          if [ $COUNTER -eq 24 ]; then IPX=$ip24; fi
          if [ $COUNTER -eq 25 ]; then IPX=$ip25; fi
          if [ $COUNTER -eq 26 ]; then IPX=$ip26; fi
          if [ $COUNTER -eq 27 ]; then IPX=$ip27; fi
          if [ $COUNTER -eq 28 ]; then IPX=$ip28; fi
          if [ $COUNTER -eq 29 ]; then IPX=$ip29; fi                                                                                       
          if [ $COUNTER -eq 30 ]; then IPX=$ip30; fi                      
          if [ $COUNTER -eq 31 ]; then IPX=$ip31; fi
          if [ $COUNTER -eq 32 ]; then IPX=$ip32; fi
          if [ $COUNTER -eq 33 ]; then IPX=$ip33; fi
          if [ $COUNTER -eq 34 ]; then IPX=$ip34; fi
          if [ $COUNTER -eq 35 ]; then IPX=$ip35; fi
          if [ $COUNTER -eq 36 ]; then IPX=$ip36; fi    

          ssh root@$IPX "test -e .I_AM_MASTER"
          
          if [ $? -eq 0 ]; then
              # your file exists          
                master_ip=$IPX
                success=1
          fi

      done          
      ##################################################################
      
      while IFS='' read -r line || [[ -n "$line" ]]; 
      do
              
        slave_cores=$line;
      
      done < ".cores";

      ##################################################################      

      rm -fr $HOMEPATH.spark/conf/spark-env.sh 
      touch  $HOMEPATH.spark/conf/spark-env.sh

      echo "SPARK_MASTER_IP="$master_ip >> $HOMEPATH.spark/conf/spark-env.sh
      echo "SPARK_MASTER_HOST="$master_ip >> $HOMEPATH.spark/conf/spark-env.sh            
      echo "SPARK_WORKER_CORES="$slave_cores >> $HOMEPATH.spark/conf/spark-env.sh

      chmod a+x $HOMEPATH.spark/conf/spark-env.sh
      
      ##################################################################

      #echo "change dir to $HOMEPATH.spark/sbin/"

      cd $HOMEPATH.spark/sbin/

      master_ip_port="spark://$master_ip:7077"

      echo "This is slave  IP:"$my_own_ip
      echo "Attempt master IP"$master_ip 
      echo "Number of cores: "$slave_cores

      ./start-slave.sh $master_ip_port

      cd
}
alias start_spark_slave=startsparkslave
alias sss=startsparkslave

########################################################################

startsparkmaster() {

      # remove slave command from spark sbin folder
      cd
      rm -fr $HOMEPATH.spark/sbin/start-slave.sh
      rm -fr $HOMEPATH.spark/sbin/start-slaves.sh  
      rm -fr $HOMEPATH.spark/logs/*
      # This folder blows up sometimes
      rm -fr $HOMEPATH.spark/work/* 
      
      ##################################################################      

      rm -fr $HOMEPATH.spark/conf/spark-env.sh 
      touch  $HOMEPATH.spark/conf/spark-env.sh

      echo "SPARK_MASTER_IP="$master_ip >> $HOMEPATH.spark/conf/spark-env.sh
      echo "SPARK_MASTER_HOST="$master_ip >> $HOMEPATH.spark/conf/spark-env.sh
      echo "SPARK_LOCAL_IP="$my_own_ip >> $HOMEPATH.spark/conf/spark-env.sh

      chmod a+x $HOMEPATH.spark/conf/spark-env.sh
      
      ##################################################################

      # start master command
      cd
      cd $HOMEPATH.spark/sbin/
      ./start-master.sh --host $my_own_ip
      cd

      echo "I_AM_MASTER">.I_AM_MASTER
}
alias start_spark_master=startsparkmaster

######################################################

stopsparkall() {

      # remove slave command from spark sbin folder
      cd
      $HOMEPATH.spark/sbin/stop-all.sh
      $HOMEPATH.spark/sbin/stop-master.sh
      $HOMEPATH.spark/sbin/stop-slave.sh
      $HOMEPATH.spark/sbin/stop-slaves.sh        

      rm -fr .I_AM_MASTER
}
alias stop_spark_all=stopsparkall


#####################################
#####################################
#####################################