#######################################
#NODEFILE: planetlab nodelist
#BOOTFILE: bootconfig file
#ALGORITHM1: Chord / Kademlia
#ALGORITHM2: Chord / Kademlia
#SERVERIP: boot server IP
#EXEC: upload executable
#CFG: upload config file
#PORT1: port1 / port2 for alg1 / alg2
#PORT2: port1 / port2 for alg1 / alg2
#FAILED: file with list of failed sites.
#######################################


PLANETRSA=planet_rsa
TARGETDIR=/home/columbia_salman/p2p_gaurav/

i=1;
NODEFILE=""
BOOTFILE=""
ALGORITHM1=""
ALGORITHM2=""
SERVERIP=""
EXEC=0
CFG=0
SUFFIX1=""
SUFFIX2=""
PORT1="7080"
PORT2="7080"
FAILED=""
while [ $# -ge 1 ]; 
do
   case $1 in

     -l)   shift ; NODEFILE="$1";;
     -s)   shift ; BOOTFILE="$1";; 	
     -a1)   shift; ALGORITHM1="$1";;      
     -a2)   shift; ALGORITHM2="$1";;      
     -ip)  shift;  SERVERIP="$1";;
     -exec) EXEC=1;;
     -cfg)   CFG=1;;
     -p1)   shift; PORT1="$1";;	
     -p2)   shift; PORT2="$1";;	
     -failed) shift; FAILED="$1";;
   esac

   shift
done 

SUFFIX1="${ALGORITHM1}${PORT1}";
SUFFIX2="${ALGORITHM2}${PORT2}";

#read bootstrap server list file
if [ -z $BOOTFILE ];then
   echo "No server"
else

exec 3<&0
exec 0<$BOOTFILE

read line
echo $line
if [ $EXEC -eq 1 ];then
  scp -i ${PLANETRSA} ../src/p2pmain columbia_salman@${line}:${TARGETDIR}
fi
if [ $CFG -eq 1 ];then
  config_file1="bootconfig${SUFFIX1}.cfg"
  cp bootconfig.txt ${config_file1}
  perl -p -i -e "s/algorithm/$ALGORITHM1/" ${config_file1}
  perl -p -i -e "s/port/$PORT1/" ${config_file1}
  scp -i ${PLANETRSA} ${config_file1} columbia_salman@${line}:${TARGETDIR}  
if [ ! -z $ALGORITHM2 ];then
  config_file2="bootconfig${SUFFIX2}.cfg"
  cp bootconfig.txt ${config_file2}
  perl -p -i -e "s/algorithm/$ALGORITHM2/" ${config_file2}
  perl -p -i -e "s/port/$PORT2/" ${config_file2}
  scp -i ${PLANETRSA} ${config_file2} columbia_salman@${line}:${TARGETDIR}  
  rm ${config_file2}
fi
  rm ${config_file1}
fi

fi 

#read site-list file

if [ -z $NODEFILE ];then
     echo "No client."
     exit 1
else

exec 3<&0
exec 0<$NODEFILE

while read line
do
  if [ ! -z $FAILED ];then
   grep $line $FAILED
   retval=$?;
   if [ $retval == 0 ];then
	echo "ignore $line"
	continue;
   fi
  fi

   echo $line
if [ $EXEC -eq 1 ];then
   scp -i ${PLANETRSA} ../src/p2pmain columbia_salman@${line}:${TARGETDIR}
   if [ $? != 0 ];then
      echo $line >> failedsites 
      continue;
   fi
fi
if [ $CFG -eq 1 ];then
   config_file1="${line}${SUFFIX1}.cfg"
   cp configsample.txt $config_file1
   ./generate_cfg_file.pl $ALGORITHM1 $SERVERIP ${config_file1} ${line} $PORT1
   perl -p -i -e "s/port/${PORT1}/" ${config_file1}
   scp -i ${PLANETRSA} ${config_file1} columbia_salman@${line}:${TARGETDIR}  
   if [ $? != 0 ];then
      echo $line >> failedsites 
      continue;
   fi
if [ ! -z $ALGORITHM2 ];then
  config_file2="${line}${SUFFIX2}.cfg"
  cp ${config_file1} ${config_file2}
  perl -p -i -e "s/$ALGORITHM1/$ALGORITHM2/" ${config_file2}
  perl -p -i -e "s/$PORT1/$PORT2/" ${config_file2}
  scp -i ${PLANETRSA} ${config_file2} columbia_salman@${line}:${TARGETDIR}  
   if [ $? != 0 ];then
      echo $line >> failedsites 
      continue;
   fi
  rm ${config_file2}
fi
   rm $config_file1
fi
done

fi
