#!/bin/tcsh
set file=$1
set numstruc = `grep -c ENERGY $file`
set numatom = `grep -c ATOM $file`
#echo $numstruc $numatom
#set numTER = `grep -c TER $file`
set numatom = `echo "($numatom / $numstruc)+3" | bc`
#set numatom = `echo $numatom | cut -c 1-5`
set i = 1
set j = 2
echo "TER" > TER

set num1 = `echo $numatom + 1 | bc`
set num2 = $num1
rm structur*.pdb

echo $i $numstruc $numatom $num1 $num2

rm tempo
echo "NUMATOM" $numatom
grep -A $numatom ENERGY $file > tempo

while ($i <= $numstruc)
echo a$i
tail -99999999999 tempo | head -$num2 | tail -$num1 > structur$i.pdb
set num2 = `echo $num2 + $num1 +1 | bc`
echo b$i $num1 $num2
set i = `echo $i+1 | bc`
end

set i = 1
set j = 2
while ($j <= $numstruc)
grep -v TER structur$j.pdb > structure$i.pdb
grep " A " structure$i.pdb > structure${i}a.pdb
egrep " B | C " structure$i.pdb > structure${i}b.pdb
/users2/vetinari/pulchra306/pulchra -q -f -g structure${i}a.pdb
/users2/vetinari/pulchra306/pulchra -q -f -g structure${i}b.pdb

cat structure${i}a.rebuilt.pdb TER structure${i}b.rebuilt.pdb > structure${i}.pdb

set i = `echo $i+1 | bc`
set j = `echo $j+2 | bc`
rm structur$i.pdb structure$i.pdb structure${i}a.pdb structure${i}b.pdb structure${i}a.rebuilt.pdb structure${i}b.rebuilt.pdb
end
rm TER
