#!/usr/bin/perl
#################################################################################
#       Patrik Burkhalter,                                                      # 
#	Wed Apr 28 19:40:37 CEST 2004						#
#################################################################################
#                                                                               #
#   This program is free software; you can redistribute it and/or modify        #
#   it under the terms of the GNU General Public License as published by        #
#   the Free Software Foundation; either version 2 of the License, or           #
#   (at your option) any later version.                                         #
#                                                                               #
#   This program is distributed in the hope that it will be useful,             #
#   but WITHOUT ANY WARRANTY; without even the implied warranty of              #
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
#   GNU General Public License for more details.                                #
#                                                                               #
#   You should have received a copy of the GNU General Public License           #
#   along with this program; if not, write to the Free Software                 #
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   #
#                                                                               #
#################################################################################
# Changelog:
#
#

@SATELLITES = ();

#
# Add as many as you need
#
# Caution: localfilename must end with .tle (xplanet want this)
#
# Hint: 
#
$ISS->{'remotepath'}    = 'http://www.celestrak.com/NORAD/elements';
$ISS->{'remotefile'}    = 'stations.txt';
$ISS->{'localpath'}     = '/usr/local/xplanet/share/xplanet/satellites';
$ISS->{'localfilename'} = 'iss.tle';
$ISS->{'opt'}           = '"" image=iss.png transparent={0,0,0} color=yellow altcirc=0 trail={orbit,-20,0,5}';
$ISS->{'do'}            = '25544';
push(@SATELLITES, $ISS);

$ISS_REL->{'remotepath'}    = 'http://www.celestrak.com/NORAD/elements';
$ISS_REL->{'remotefile'}    = 'stations.txt';
$ISS_REL->{'localpath'}     = '/usr/local/xplanet/share/xplanet/satellites';
$ISS_REL->{'localfilename'} = 'iss_rel.tle';
$ISS_REL->{'opt'}           = 'color=purple';
$ISS_REL->{'dont'}          = '25544';
push(@SATELLITES, $ISS_REL);

$NEW->{'remotepath'}    = 'http://www.celestrak.com/NORAD/elements';
$NEW->{'remotefile'}    = 'tle-new.txt';
$NEW->{'localpath'}     = '/usr/local/xplanet/share/xplanet/satellites';
$NEW->{'localfilename'} = 'new.tle';
$NEW->{'opt'}           = 'color=orange trail={orbit,-2,0,2}';
push(@SATELLITES, $NEW);

$HST->{'remotepath'}    = 'http://www.celestrak.com/NORAD/elements';
$HST->{'remotefile'}    = 'science.txt';
$HST->{'localpath'}     = '/usr/local/xplanet/share/xplanet/satellites';
$HST->{'localfilename'} = 'science.tle';
$HST->{'opt'}           = '"" image=hst.png transparent={0,0,0}';
$HST->{'do'}            = '20580';
push(@SATELLITES, $HST);

##############################

foreach $satellit (@SATELLITES)
{
	$string = '';

	$remotepath    = $satellit->{'remotepath'};
	$remotefile    = $satellit->{'remotefile'};
	$localpath     = $satellit->{'localpath'};
	$localfilename = $satellit->{'localfilename'};
	$color         = $satellit->{'color'};
	$opt           = $satellit->{'opt'};
	$do            = $satellit->{'do'};
	$dont          = $satellit->{'dont'};

	if($do ne '' && $dont ne '') { print "Error: Do _OR_ dont supported in 1 dataset\n"; exit(0);}

	# Test file/paths
	if(!(-w $localpath))      { print "Error: localpath: $localpath does not exists or not writable\n"; exit(1); } 
	if($localfilename eq '')  { print "Error: a localfilename not defined\n"; exit(1); }
	if($remotepath eq '')     { print "Error: a remotepath not defined\n"; exit(1); }
	if($remotefile eq '')     { print "Error: a remotefile not defined\n"; exit(1); }
	if($opt eq '')            { $opt = 'color=grey'; }

	$res = `/usr/local/bin/wget -N $remotepath/$remotefile -P $localpath 2>&1`;
	if($res =~/no\ newer/)	{ print "File $remotefile already up to date\n"; }
	$res = `cp $localpath/$remotefile $localpath/$localfilename  2>&1`; 

	$data_file = "$localpath/$localfilename";
	open(DAT, $data_file) || die("Could not open file!");
	@raw_data = <DAT>;
	close(DAT);

	foreach $line (@raw_data)
	{
		$add = 'no';

		if($line =~/^2/)
		{
			@s  = split(/\ /, $line);
			$id = $s[1];

			if($do ne '')
			{
				if($do =~/$id/)
				{
					$add = 'yes';
				}
			}
			elsif($dont ne '')
			{
				if($dont =~/$id/)
				{
					$add = 'no';
				}
				else
				{
					$add = 'yes';
				}
			}
			else
			{
				$add = 'yes';
			}

			
			if($add eq 'yes')
			{
				$string = $string . $id ." $opt "."\n";
			}


		}
	}
	
	$file = "$localpath/$localfilename";
	$file =~ s/\.tle$//;


	open (FILEHANDLE, ">$file") or die "no such file";
	print FILEHANDLE $string;
	close (FILEHANDLE);
}

