#!/bin/sh
#  This script will generate the public_eod.html file with the
#   necessary EOD values to populate the dropdown menus in the 
#   following webpages:
#       https://voyager-dev.sci.gsfc.nasa.gov/rates.html
#       https://voyager-dev.sci.gsfc.nasa.gov/flux.html
#       https://voyager-dev.sci.gsfc.nasa.gov/spectra.html
#       https://voyager.gsfc.nasa.gov/rates.html
#       https://voyager.gsfc.nasa.gov/flux.html
#       https://voyager.gsfc.nasa.gov/spectra.html


# This function will take in a directory, grab the most recent file from it, and 
#   strip the date as MM-DD-YYYY from the third line.  This is the format of the 
#   v*_archive_*.YYYY_detail files that we're getting the end-of-data dates from.
#   Sample header (top 5 lines)
#       voyager-1
#       2022-01-01T07:45:00
#       2022-04-30T23:45:00    <----- THIS LINE is what we're grabbing
#       146
#       1 IAS (A1.A2.^C4.^B2.^G1)

create_mm_dd_yyyy(){
    directory=$1
    #fname="$(ls -Atr $directory/*detail | tail -1)"
    fname="$(ls -AXr $directory*detail | head -1)"
    end_date_zulu="$(sed '3q;d' $fname)"

    yyyy=${end_date_zulu:0:4}
    mm=${end_date_zulu:5:2}
    dd=${end_date_zulu:8:2}

    echo "$mm-$dd-$yyyy"
}

# Hard-coded output file.  TODO: do we want this as an input?  I don't think so
output_file="/data/zdata_1/spdf_archive/public/pub/data/voyager/voyager1/crs/rates_only/QuickLook/public_eod.html"

#  Hard-coded paths to the directories containing the rates and flux detail files
voyager1_rates_dir="/data/zdata_1/spdf_archive/public/pub/data/voyager/voyager1/crs/rates_only/archive_rates/"
voyager1_flux_dir="/data/zdata_1/spdf_archive/public/pub/data/voyager/voyager1/crs/flux_only/z_le_2/"

voyager2_rates_dir="/data/zdata_1/spdf_archive/public/pub/data/voyager/voyager1/crs/rates_only/archive_rates/"
voyager2_flux_dir="/data/zdata_1/spdf_archive/public/pub/data/voyager/voyager1/crs/flux_only/z_le_2/"

#  Get the mm-dd-yyyy for each of the above directories
mmddyyyy_v1r=$(create_mm_dd_yyyy $voyager1_rates_dir)
mmddyyyy_v1f=$(create_mm_dd_yyyy $voyager1_flux_dir)
mmddyyyy_v2r=$(create_mm_dd_yyyy $voyager2_rates_dir)
mmddyyyy_v2f=$(create_mm_dd_yyyy $voyager2_flux_dir)


# Compile the HTML file as a series of strings
open_tags="";
open_tags="$open_tags\n<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">"
open_tags="$open_tags\n<html lang=\"en\">"
open_tags="$open_tags\n<head>"
open_tags="$open_tags\n<title>Public</title>"
open_tags="$open_tags\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">"
open_tags="$open_tags\n<meta name=\"title\" lang=\"en-US\" content=\"Voyager Cosmic Ray Subsystem\">"
open_tags="$open_tags\n<meta name=\"description\" lang=\"en-US\" content=\"Provides Mission information and Cosmic Ray Data from the Voyager Cosmic Ray Subsystems\">"
open_tags="$open_tags\n<meta name=\"orgcode\" lang=\"en-US\" content=\"672\">"
open_tags="$open_tags\n</head>"
open_tags="$open_tags\n<body bgcolor=\"#ffffff\" text=\"#000000\" link=\"#404679\" vlink=\"#981b1e\" alink=\"#424243\">\n"


comment_str="<p><b>Note:</b> All dates below are in MM/DD/YYYY format.</p>\n"
v1r_str="<p>Voyager-1 Rates - $mmddyyyy_v1r</p>\n"
v1f_str="<p>Voyager-1 Flux - $mmddyyyy_v1f</p>\n"
v2r_str="<p>Voyager-2 Rates - $mmddyyyy_v2r</p>\n"
v2f_str="<p>Voyager-2 Flux - $mmddyyyy_v2f</p>\n"
close_tags="</body></html>"

# Print all the strings to a single file
outstr="$open_tags$comment_str$v1r_str$v1f_str$v2r_str$v2f_str$close_tags"
echo -e "$outstr" > $output_file

