@echo off
set argWant=1
REM count input args, provide usage help
set argC=0
for %%x in (%*) do Set /A argC+=1
if %argC% neq %argWant%  goto :showusage 

echo.
REM --------------------------------------------------------
REM SAVE CURRENT DATE/TIME INTO VARIABLES
set mm=%date:~4,2%
set dd=%date:~7,2%
set yyyy=%date:~10,4%
set yr2=%date:~12,2%
echo date: %mm% %dd% %yyyy% %yr2%
set ihour=%time:~0,1%
set hour=%time:~0,2%
set minute=%time:~3,2%
set intsec=%time:~6,2%
echo time: %hour% %minute% %intsec% (%ihour%)
if "%ihour%" equ " " set hour=0%hour:~-1%
set stday=%mm%/%dd%/%yyyy%
set sttime=%hour%:%minute%
echo.

REM --------------------------------------------------------
REM SET DIRECTORY FOR LOG, AND SCHEDULE CREATION
::  > create directory on local system
::  > copy all generated files to this directory
set mydir=%mm%%dd%%yr2%_%hour%
echo trying to create directory C:\antenna\calruns\%mydir%
if exist C:\antenna\calruns\%mydir% (
  echo ERROR directory already exists
  echo ERROR   exiting now
  goto :eof
)
mkdir C:\antenna\calruns\%mydir%
echo directory created
echo.

REM --------------------------------------------------------
REM INITIALIZE LOG FILE FOR THIS TEST
set logfile=C:\antenna\calruns\%mydir%\test.log
REM set logfile=C:\blah.log
echo ========================= > %logfile%
echo test initiated by %1 >> %logfile%
date /T >> %logfile%
time /T >> %logfile%

REM initalize/clear environment variables
set antLong=
set antShort=
set serial=
set radome=
set ptuArm=
set orient=
set delay=
set notes=

REM QUERY USER FOR ANTENNA DATA
echo.
echo Hi %1
echo.
echo Please enter the following antenna information:
echo (if you type in something wrong, you can manually
echo   correct the information later in %logfile%)
echo.
echo Antenna model long name, eg Topcon Chokering model CR-G3)
set /p antLong=
echo Antenna model (short name, eg TPSCR.G3; proposed IGS code)
set /p antShort=
echo Antenna serial number
set /p serial=
echo Radome code (hit enter if no radome)
set /p radome=
echo Length (in cm) and model of PTU extension (enter 0 if no extension)
set /p ptuArm=
echo Antenna orientation on PTU (N/S/E/W)
set /p orient=
set ocode=0
if /i "%orient%" equ "N" set ocode=1
if /i "%orient%" equ "E" set ocode=2
if /i "%orient%" equ "S" set ocode=3
if /i "%orient%" equ "W" set ocode=4
if %ocode%==0 (
  echo INVALID ORIENTATION CODE %orient%
  echo exiting now
  goto :eof
)
echo Notes? (anything interesting to add?)
set /p notes=

REM WRITE TEST METADATA TO LOG FILE
echo =$ %antLong% >> %logfile%
echo =$ %antShort% %radome% >> %logfile%
echo == antenna under test: %serial% >> %logfile%
echo == orientation: %orient%>> %logfile%
echo == length of PTU arm: %ptuArm% cm >> %logfile%
echo == Notes: %notes% >> %logfile%


REM --------------------------------------------------------
REM PICK UP BROADCAST NAVIGATION FILE
REM -- construct name of navigation file
C:\antenna\ptuSteer\fsteerplain\ymd2doy.exe %yyyy% %mm% %dd% > tmp.txt
set /P doy=< tmp.txt
del tmp.txt
set navfile=brdc%doy%0.%yr2%n
echo %navfile%

REM -- pick up nav file if don't already have on disk
echo.
IF EXIST C:\antenna\ptuSteer\%navfile%.gz (
echo have compressed nav file on disk
gzip -df C:\antenna\ptuSteer\%navfile%.gz 
move C:\antenna\ptuSteer\%navfile% C:\antenna\calruns\%mydir%
) ELSE IF EXIST C:\antenna\ptuSteer\%navfile% (
echo have nav file on disk
move C:\antenna\ptuSteer\%navfile% C:\antenna\calruns\%mydir%
) ELSE (
echo picking up %navfile%
echo.
REM -- write script to pick up file
set scr=CORSbrdc.txt
del %scr%
echo user anonymous>> %scr%
echo andria.bilich@noaa.gov>> %scr%
echo binary>> %scr%
echo cd /cors/rinex/%yyyy%/%doy%>> %scr%
echo mget %navfile%*>> %scr%
echo quit>> %scr%
REM -- run FTP for script
ftp -ni -s:%scr% alt.ngs.noaa.gov
REM ftp -ni -s:%scr% cors.ngs.noaa.gov
echo.
REM -- unzip file; force overwrite
echo uncompressing file using GZIP
gzip -df %navfile%.gz 
move C:\antenna\ptuSteer\%navfile% C:\antenna\calruns\%mydir%
)
IF NOT EXIST C:\antenna\calruns\%mydir%\%navfile% (
 echo ERROR problem picking up nav file %navfile%
 echo ERROR  from CORS FTP; exiting now
 goto :eof
)
echo.

REM --------------------------------------------------------
REM START NIST TIME CLOCK STEERING
echo killing old NIST Time clock (if it exists)
taskkill /im NIST-Time-software.exe
echo starting NIST Time clock in foreground
echo wait for popup window to say ADJUSTED then come back to this window
C:\antenna\NISTclock\NISTtime.lnk
echo.
pause
taskkill /im NIST-Time-software.exe

REM --------------------------------------------------------
REM WRITE METADATA TO MASTER LOG FILE
set masterLog="C:\Documents and Settings\AntCal\Desktop\ptuLog4.txt"
echo ---new_run--- >> %masterLog%
echo START %stday% %sttime% >> %masterLog%
echo antenna: %antShort% ; %serial% >> %masterLog%
echo orientation %orient% >> %masterLog%
echo dir %mydir% >> %masterLog%
echo doy %doy% >> %masterLog%


REM --------------------------------------------------------
REM GENERATE SCHEDULE FILE
REM -- create schedule file
echo.
echo Creating schedule file (this could take several minutes)
echo. >> %logfile%
cd C:\antenna\calruns\%mydir%
C:\antenna\ptuSteer\fsteersocket\f_steersocket.exe %ocode% C:\antenna\ptuSteer\myrun.inp %navfile% >> %logfile%
REM C:\antenna\ptuSteer\fsteersocket\f_steersocket.exe %ocode% C:\antenna\ptuSteer\myrun15.inp %navfile% >> %logfile%
echo Done creating schedule file (see logfile for details)
echo.
copy PTU_Schedule C:\antenna\ptuSchedule

REM -- give user the chance to change "*" into zeros
echo.
echo HEY YOU, GO TO C:\antenna\ptuSchedule
echo   AND CHECK PTU_Schedule FOR STARS, CHANGE THEM
echo   INTO ZEROS (one zero per line)
echo HURRY, YOU HAVE ONLY 10 MINUTES!
echo.
pause

REM --------------------------------------------------------
REM WRITE BATCH FILE WHICH WILL MOVE FILES AND CLEANUP AFTER
REM  PROGRAM EXECUTION HAS ENDED; SCHEDULE TASK
echo.
echo Creating file to conduct cleanup after PTU motion has ended
set clnfile=C:\antenna\calruns\%mydir%\cleanup.bat
echo @echo off > tmp.txt
echo set mydir=%mydir% >> tmp.txt
echo set logfile=%logfile% >> tmp.txt 
echo set masterLog=%masterLog%  >> tmp.txt
copy tmp.txt+"C:\antenna\ptuSteer\cleanupBase.txt" %clnfile%
del tmp.txt


REM -- schedule task to do cleanup after PTU motions have finished
echo Scheduling end task to cleanup after test ends
for /f "tokens=1,2,3,4,5" %%a in (fort.85) do (
 echo.
 echo -------------------------------------
 echo NOTE: this test will end %%b/%%c/%%a at %%d:%%e:00 UTC
 echo NOTE: this test will end %%b/%%c/%%a at %%d:%%e:00 UTC >> %logfile%
 echo -------------------------------------
 echo.
 echo schtasks /create /tn cleanup%mydir% /tr %clnfile% /sc once /sd %%b/%%c/%%a /st %%d:%%e:00 /ru "System"
 echo schtasks /create /tn cleanup%mydir% /tr %clnfile% /sc once /sd %%b/%%c/%%a /st %%d:%%e:00 /ru "System" >> %logfile%
 schtasks /create /tn cleanup%mydir% /tr %clnfile% /sc once /sd %%b/%%c/%%a /st %%d:%%e:00 /ru "System"
 echo.
)

REM --------------------------------------------------------
REM RUN SCHEDULE FILE

cd C:\antenna\ptuSchedule
REM -- failsafe to avoid overwriting old Position output file
if exist Position (
  move Position Position.SAVEME
  echo WARNING PREVIOUS POSITION FILE WAS NOT MOVED TO NEW DIRECTORY
)
REM -- start schedule execution
echo STARTING PTU MOTIONS
echo.
run
echo.
echo FINISHED RUNNING PTU MOTIONS
echo.


goto :eof


REM --------------------------------------------------------
REM add these lines to any script with arg checking
goto :eof

:showUsage
 echo [USAGE] %0 your_first_name
 echo.
 goto :eof