#!/usr/bin/env perl
=head1 NAME

sortSkewt.pl - Print out, for some XML file (i.e. g000780.xml), the file modification date
for this file in each of the directories D1 .. D10 (in UNIX time, seconds).
So for each of the directories for each day, the directory name string and
unix time is printed out for the file modification date for the filename
id passed into this script.

=head1 DESCRIPTION

Usage: sortSkewt.pl id=gXXXXXX
       sortSkewt.pl images_dir=

Where "X" is a digit from 0-9.

Example of usage at the command-line:

$ ./sortSkewt.pl id=g000725
Content-type: text/html

D20
D2
D3
D5
...
D4

$ ./sortSkewt.pl images_dir=
Content-type: text/html

/cData/Atmosphere/Soundings/skewt/images/

=head1 AUTHOR

Gerasimos Michalitsianos Dec 2018.

=head1 HISTORY

Jun 2023   Removed the output observation time. Added images_dir parameter.

=head1 METHODS

=cut

use warnings;
use strict;
use CGI;
use FindBin;
use lib "$FindBin::Bin";
use SkewtTable;

my $cgi = CGI->new;
my @params = $cgi->param ();
die "There should be exacly one argument passed via GET"
   unless (@params == 1);

# There should be only one argument to this script:
# a string "id=gXXXXXX" , a string with a 6-digit integer.
# Or "images_dir="

die 'Argument to this script should be a string "id" or "images_dir"'
   unless ($params[0] =~ /id|images_dir/);

print $cgi->header;

if ($params[0] eq 'images_dir') {
   print (SKEWT_DIR. IMAGES_DIR. "/\n");
   exit 0;
}

# get XML ID string (gXXXXXX) as the single argument
# to this script via GET method. Make sure it is "g"
# following by 6 digits.
my $xmlID;
die 'Input argument should be a string "gXXXXXX"'
  unless (($xmlID) = $cgi->param ("id") =~ /^g(\d+)$/) ;

# call initializeation method in SkewtTable.pm
# to estabish and create all initialization parameters.
die "Unable to read the Skew-T configuration file"
  unless ReadConfigFile ();

# Get the sorted directories array
my $dirsSorted = SortSkewt ($xmlID);

# Print out sorted directories separated by newlines
print join "\n", @$dirsSorted;

# End sortSkewt.pl
