#! /bin/csh
#
# gettle      -----      Manfred Bester  14Mar95
#
# This C 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. 
#
#
#
set ftphost=129.92.2.1
set satdir=/usr/local
set satuser=root
#
#
#
set DOFTP=N
set DOLIST=N
set VERBOSE=''
foreach PARM ($*)
    switch (.$PARM)
	case .-f:
	case .--ftp:
		set DOFTP=Y
	case .-l:
	case .--list:
		set DOLIST=Y
		breaksw
	case .--noftp:
		set DOFTP=N
	case .--nolist:
		set DOLIST=N
		breaksw
	case .--noverbose:
		set VERBOSE=''
		breaksw
	case .-v:
	case .--verbose:
		set VERBOSE='-v'
		breaksw
	default:
		echo "Usage: `basename $0` [-f|--ftp|--noftp] [-v|--verbose|--noverbose]"
		echo
		echo "Ignoring invalid parameter ${PARM}"
		breaksw
    endsw
end
#
#
set userid=$USER@`hostname`.`domainname`
#
#
if ($USER == $satuser) then 
    set satdirusr=$satdir
else
    set satdirusr=$HOME
    mkdir -p $satdirusr/SatTrack/tle
endif
#
cd $satdirusr/SatTrack/tle
#
if ($VERBOSE == -v) then 
    echo
    echo $userid
endif
#
if ($DOFTP == Y) then
    echo calling $ftphost ...
#
    ftp $VERBOSE -n $ftphost <<.EOF
user anonymous -$userid
cd /pub/space
get tle.new
get gorizont.tle
get other.tle
get tvro.tle
get visual.tle
quit
.EOF
#
    if ($VERBOSE == -v) then 
	echo saving data files ...
    endif
#
    if (-f tle-9.bak) then
	rm $VERBOSE -f tle-9.bak
    endif
    foreach NO (8 7 6 5 4 3 2 1 0)
	if (-f tle-$NO.bak) then
	    mv $VERBOSE tle-$NO.bak tle-`echo $NO 1+p | dc`.bak
	endif
    end
#
    "mv" $VERBOSE tle.dat tle-0.bak
    "mv" $VERBOSE tle.new tle.dat
    "mv" $VERBOSE gorizont.tle gorizont.dat
    "mv" $VERBOSE other.tle debris.dat
    "mv" $VERBOSE tvro.tle tvro.dat
    "mv" $VERBOSE visual.tle visual.dat
#
    cd ../data
    if (-f tlelist-9.bak) then
	rm $VERBOSE -f tlelist-9.bak
    endif
endif
#
if ($DOLIST == Y) then
    foreach NO (8 7 6 5 4 3 2 1 0)
	if (-f tlelist-$NO.bak) then
	    mv $VERBOSE tlelist-$NO.bak tlelist-`echo $NO 1+p | dc`.bak
	endif
    end
    mv $VERBOSE tlelist.dat tlelist-0.bak
    cd ../tle
    ls -1t *.* | grep -v 'tle[sx].dat' | grep -v sts.dat > ../data/tlelist.dat
#
endif
#
if ($VERBOSE == -v) then 
    echo running 'maketlex' ...
endif
#
maketlex
#
if ($VERBOSE == -v) then 
    echo running 'maketles' ...
endif
#
maketles ham
#
if ($VERBOSE == -v) then 
    echo running 'satfilter' ...
endif
#
satfilter leoe
satfilter leom
satfilter leop
#
if ($VERBOSE == -v) then 
    echo
    echo done.
endif
