#!/bin/tcsh

set directory = `ls |grep crab_`
set totalnrevts = `grep total_number_of_events crab.cfg | tr "=" "\n"|grep -v total_number_of_events`
set nrjobevts = `grep events_per_job crab.cfg | tr "=" "\n"|grep -v events_per_job`
set nrjobs    = $totalnrevts
@ nrjobs /= $nrjobevts

echo "Will adapt the skipEvents parameter in directory $directory, which contains $nrjobs jobs of $nrjobevts events"

cd $directory/share/
cd `ls -a|grep bossFiles`

foreach job(`seq 1 1 $nrjobs`)  
  cp BossArchive_${job}.tar BossArchive_${job}.tar_old
  mkdir BossArchive_${job}
  mv BossArchive_${job}.tar BossArchive_${job}
  cd BossArchive_${job}
  tar -xf BossArchive_${job}.tar
  rm BossArchive_${job}.tar
  
  #adapt CMSSW.cfg
  mv CMSSW.cfg CMSSW.cfg_old
#  grep -B4 "PoolSource" CMSSW.cfg_old  >> CMSSW.cfg
  grep -B4 "H2RootNtplSource" CMSSW.cfg_old  >> CMSSW.cfg
  echo "{" >> CMSSW.cfg
  set maxEvent = $nrjobevts
  @ maxEvent *= $job
  set skipEvents = $nrjobevts
  @ skipEvents *= ($job - 1)
  echo "untracked int32 maxEvents = $nrjobevts" >> CMSSW.cfg
  echo "untracked uint32 skipEvents = $skipEvents" >> CMSSW.cfg
  grep -A1000 "fileNames" CMSSW.cfg_old  >> CMSSW.cfg
#  rm CMSSW.cfg_old

  
  tar cf BossArchive_${job}.tar *
  mv BossArchive_${job}.tar ..
  cd ..
  rm -r BossArchive_${job}

end

cd ../../..
