#!/bin/csh -f
grep HEAD $1 > $2
grep REMAR $1 >> $2
grep CRYST $1 >> $2
awk '$1=="ATOM" || $1=="HETATM" {print $0}' $1 | awk '{id=substr($0,1,20); xx=substr($0,24,56); print id,"A",xx}' | awk '{id=substr($0,1,20); printf "%20s %1s %3d    %8.3f%8.3f%8.3f%6.2f%6.2f\n",id,"A",$6,$7,$8,$9,$10,$11}' >> $2
echo TER >> $2
awk '$1=="ATOM" || $1=="HETATM" {print $0}' $1 | awk '{id=substr($0,1,20); xx=substr($0,24,56); print id,"A",xx}' |awk '{id=substr($0,1,20); printf "%20s %1s %3d    %8.3f%8.3f%8.3f%6.2f%6.2f\n",id,"B",$6,-$7+93.33,-$8+93.33,$9,$10,$11}' >> $2
echo TER >> $2
awk '$1=="ATOM" || $1=="HETATM" {print $0}' $1 | awk '{id=substr($0,1,20); xx=substr($0,24,56); print id,"A",xx}' |awk '{id=substr($0,1,20); printf "%20s %1s %3d    %8.3f%8.3f%8.3f%6.2f%6.2f\n",id,"C",$6,-$8+93.33,$7,$9,$10,$11}' >> $2
echo TER >> $2
awk '$1=="ATOM" || $1=="HETATM" {print $0}' $1 | awk '{id=substr($0,1,20); xx=substr($0,24,56); print id,"A",xx}' |awk '{id=substr($0,1,20); printf "%20s %1s %3d    %8.3f%8.3f%8.3f%6.2f%6.2f\n",id,"D",$6,$8,-$7+93.33,$9,$10,$11}' >> $2
echo TER >> $2
sed 's/H1  HOH/HW1 HOH/g' $2 > xXx ; mv xXx $2
sed 's/H2  HOH/HW2 HOH/g' $2 > xXx ; mv xXx $2
