#!/bin/sh
#
# gettle      -----      Manfred Bester  14Mar95
#
# This POSIX shell script can be invoked manually or by cron at a specified time.
# It calls up 'archive.afit.af.mil' (129.92.1.66) and gets the most recent 
# files with Keplerian two-line element sets, as used with SatTrack. 
# After placing the files into SatTrack/tle, program 'tlexgen' is invoked. 
# This program generates an extra file 'tlex.dat' with extended two-line 
# element sets. It basically concatenates several tle files into a larger data 
# base which is used in turn as the default data base for SatTrack. Duplicate 
# entries are eliminated by the object number. The tle files that are 
# concatenated are specified in the input file 'tlelist.dat' in SatTrack/data. 
# Finally, program 'selectsat' is invoked to generate an abbreviated list of 
# two-line element sets for a specified satellite group (e.g. am, sci, wx), 
# which may be used for further distribution to users interested in that 
# particular satellite group. 
#
# If SatTrack is not installed in the user's home directory, the variable 
# 'satdir' needs to be changed to the name of the directory that SatTrack is 
# installed under. The variable 'satuser' is the UID of the user who is allowed 
# to write into the system-wide SatTrack directory, i.e. the person in charge 
# of maintaining SatTrack. 
#
# KD6PAG -- 6 Feb 1999
#
# Minor changes to replace 'archive.afit.af.mil', which no longe carries
# KEPS, to 'ftp.celestrak.com'.  This site no longer carries a full line
# of satellites (too much bandwith), so 'amateur.txt' has been added an
# an additional file to get,   Don't forget to update 'tlelist.dat' to
# include this file as well.
#
# Hamish Moffatt VK3SB <hamish@debian.org> -- 15 Oct 1999
#
# Ported to POSIX. Modified for Debian GNU/Linux.
#
FTPHOST=ftp.celestrak.com
SATDIR=/var/lib/SatTrack/tle

USERID=$USER@`hostname -f`

mkdir -p $SATDIR
cd $SATDIR

if [ x$1 == "x-v" ]; then 
    echo $USERID
    echo cd $SATDIR
    echo calling $FTPHOST ...
fi

# Fetch the files
ftp $1 -n $FTPHOST << !
user anonymous -$USERID
cd /elements/
get tle-new.txt  tle.new
get gorizont.txt gorizont.new
get visual.txt   visual.new
get amateur.txt  amateur.new
quit
!

if [ x$1 == "x-v" ]; then
    echo saving data files ...
fi

# Keep backups
mv tle.dat tle.bak
mv amateur.dat amateur.bak

# Rename the new files
mv tle.new      tle.dat
mv gorizont.new gorizont.dat
mv tvro.new     tvro.dat
mv visual.new   visual.dat
mv amateur.new  amateur.dat

if [ x$1 == "x-v" ]; then 
    echo running 'maketlex' ...
fi

# update tlex
maketlex

if [ x$1 == "x-v" ]; then 
    echo done.
fi

