#!/bin/sh
#
FE_Id=51
while [ ${FE_Id} -le 412 ] ; do
      #echo 'Check FEB' ${FE_Id}
      data_in=/data/bfys/otroper/FE-Tester/FE_${FE_Id}_2000
      dirwww=/www/pub/experiments/bfys/lhcb/outerTracker/Electronics/Production/FE-Plots/
      dirold=/www/pub/experiments/bfys/lhcb/outerTracker/FE-Plots/FE_${FE_Id}
      dir=/www/pub/experiments/bfys/lhcb/outerTracker/Electronics/Production/FE-Plots/FE_${FE_Id}
      cd ${dirwww} 
      #------------------------------
      # Check if the FE was tested:
      #------------------------------
      test -d ${data_in}
      FE_exist=`echo $?`
      if [ ${FE_exist} -eq 0 ] ; then
        # Last test:                 
        Test_Id=`ls ${data_in} | sed -n 's/^'"test"'\([0-9][0-9]*\)/\1/p' | sort -n | tail -1`
	#echo '--> last test:' ${Test_Id}
        #------------------------------
        # Check if the test was tested:
        #------------------------------
	test -d ${data_in}/test${Test_Id}
	Test_exist=`echo $?`
        #-------------------------------------
        # Check if the test is already copied:
        #-------------------------------------
	test -d ${dir}/test${Test_Id}
	Test_copied=`echo $?`
	if [ ${Test_copied} -eq 1 ] ; then
	    mkdir ${dir}
	    echo '-------------------------------------------------'
	    echo 'Test not copied: FE' ${FE_Id} 'Test' ${Test_Id}
	else 
	    #echo '------------------------------------'
	    #echo 'Plots already present: FE' ${FE_Id} 'Test' ${Test_Id} ' (update link)'
	    rm ${data_in}/test${Test_Id}
	    cd ${data_in}
	    ln -s ${dir}/test${Test_Id} .
	    #ls -ltr ${dir}
	    cd ${dirwww} 
	    #echo ${dir}/test${Test_Id}/ps
	    #ls ${dir}/test${Test_Id}/ps
	fi
      else
	  echo '------------------------------------'
	  echo 'No test: FE' ${FE_Id}  
      fi
      FE_Id=`expr ${FE_Id} + 1`
done


