#!/usr/bin/perl -w
# 
# Convert a local TLE into a pocketSat .PDB import db.
#
# Thanks to Jay Anderson for fixing the bugs.
#
require "getopts.pl";

# DB_OVERWRITE attribute is 16 (but doesn't seem to work right)
# $dbAttr = 16;
$dbAttr = 0;
$dbType = "iTLE";
$creatorID = "pSat";

$pdbHeadStruct = "a32nnNNNNNNa4a4NNn";
$recListStruct = "NN";

if($#ARGV<1)
{
    print "usage: tle2pdb tleFile pdbFile DBInfo\n";
    print "   ie: tle2pdb sats.txt sats.pdb \"Satellite Data\"\n";
    exit;
}

#
# read the tle file
#

$tleFile = shift(@ARGV);
$pdbFile = shift(@ARGV);
$DBInfo = shift(@ARGV);

open IN, $tleFile;
# read the actual data
@tleData = <IN>;
close(IN);

#
# assign names
#
@lt = localtime;
$month = $lt[4]+1;
$day = $lt[3];
$year = $lt[5];
$dbName = "$DBInfo $month/$day/$year";

#
# Do the data
#

$recCount = $#tleData + 1;
$pdbHeader = pack($pdbHeadStruct, $dbName, $dbAttr, 1, time, time, 0, 0, 0, 0, 
                  $dbType, $creatorID, 0, 0, $recCount);

$dataLoc = length($pdbHeader) + 8 * $recCount;

for($i=0;$i<$recCount;$i++)
{
    $recList[$i] = pack($recListStruct, $dataLoc, 0);
    $dataLoc += length($tleData[$i]);
}

open OUT, ">./$pdbFile";
binmode OUT;
print OUT $pdbHeader;
print OUT @recList;
print OUT @tleData;
close OUT;





