#!/usr/bin/perl
# tlg2pdb written by Marty Pagel, IUMVF, 10/23/96

$counter = 1;
$filename = shift @ARGV;

if (-d "tlg2pdb_dir") {
	`rm -r tlg2pdb_dir`;
}	
`mkdir tlg2pdb_dir`;

open (TLGFILE, "$filename.tlg");
while (<TLGFILE>) {
	($linetype,$atomnum,$atomname,$resname,$resnum,$x,$y,$z,$remainder) = split(' ',$_,9);

        if ($linetype =~ /COMMAND:/ && $atomnum =~ /traj/) {
                $oktoprint = "Y";
        }
	if ($linetype =~ /COMMAND:/ && $atomnum =~ /stop/) {
		`rm -f tlg2pdb_dir/$filename$counter.pdb`;
		$counter--;
		die "\nTotal number of frames = $counter\n";
        }

	if ($oktoprint =~ /Y/) {
		if (-f "tlg2pdb_dir/$filename$counter.pdb") {
			open (NEWPDBFILE, ">> tlg2pdb_dir/$filename$counter.pdb");
		}
		else {
			open (NEWPDBFILE, "> tlg2pdb_dir/$filename$counter.pdb");
		}
		
		if ($linetype =~ /REMARK/ || $linetype =~ /ATOM/) {
			printf NEWPDBFILE "%-4s%7s%4s%5s%6s%12.3f%8.3f%8.3f\n",$linetype,$atomnum,$atomname,$resname,"     1",$x,$y,$z;	
		}
		if ($linetype =~ /END/) {
			print NEWPDBFILE "END\n";
			print "Processed frame $counter\n";
			$counter++;	
		}
	}
}
