#!/bin/bash

#variable depends with the actual version &/ installation directory
TahoeHome='~/allmydata-tahoe-1.5.0'

#IP adress of the introducer 
introducer='192.168.1.3'
#echo $introducer
# -- clients à configurer --
clients[0]='192.168.1.3'
clients[1]='192.168.1.4'
clients[2]='192.168.1.5'


if [ "${1}" = "a" ]; then
 # Configuration of the introducer 
 echo 'introducer configuration'
 if [ "${2}" = "start" ]; then
  ssh user@$introducer $TahoeHome'/bin/tahoe start ~/introducer/'
 elif [ "${2}" = "stop" ]; then
  ssh user@$introducer $TahoeHome'/bin/tahoe stop ~/introducer/'
 else
  echo './script.sh a start|stop'
 fi
elif [ "${1}" = "b" ]; then
 echo 'Clients configuration'
 if [ "${2}" = "start" ]; then
  if [ "${3}" = "c" ]; then
   scp user@$introducer:~/introducer/introducer.furl introducer.furl
  fi  
  for (( i=0; i<${#clients[@]}; i++ ));
  do
   if [ "${3}" = "c" ]; then #copy of the file 'introducer.furl' to clients directory
    scp introducer.furl user@${clients[$i]}:~/.tahoe/introducer.furl
   fi
     ssh user@${clients[$i]} $TahoeHome'/bin/tahoe start'
  done
 elif [ "${2}" = "stop" ]; then
  for (( i=0; i<${#clients[@]}; i++ ));
  do
     ssh user@${clients[$i]} $TahoeHome'/bin/tahoe stop'
  done
 else
  echo './script.sh b start|stop [c]'
 fi
elif [ "${1}" = "c" ]; then
 echo 'Configuration ssh de '$2
 if [ "${2}" = "all" ]; then
  if [ "${3}" = "0" ]; then
   ssh-keygen
  fi

  scp ~/.ssh/id_rsa.pub user@$introducer:/home/user/.ssh/authorized_keys

  for (( i=0; i<${#clients[@]}; i++ ));
  do
     ssh-copy-id -i ~/.ssh/id_rsa.pub user@${clients[$i]}
   #scp ~/.ssh/id_rsa.pub user@${clients[$i]}:/home/user/.ssh/authorized_keys
  done
 elif [ "${2}" = "+" ]; then
  ssh-copy-id -i ~/.ssh/id_rsa.pub user@${3}
  #ssh user@${3} $TahoeHome'/bin/tahoe stop ~/introducer/'
 else
  echo './script.sh c all|+ [0|@IP]'
 fi
else
 echo "
./script.sh a start|stop 
 to stat or stop the introducer

./script.sh b start|stop [c] 
 to start or stop a client 
    
 c    to copy 'introducer.furl' into the clients tahoe folder before starting

./script.sh c all|+ [0|@IP] 
 to configure ssh (to do not type password in each remote command)
    
 all to configure all nodes (introducer + clients)
     
 0 if the first time you call this command
  otherwise it modify your ssh key
 + to configure an additional node with address @IP 
  (./script.sh c + 192.168.2.4)   
"
fi