#! /usr/bin/gawk -f
# Last edited on 2019-10-27 19:21:54 by jstolfi

# Reads a bunch of names of files with price series data.
# Writes the time ranges in a format suitable for gnuplot.
#
# Each file name must be "{TDLO}--{TDHI}-{EX}-{CR}-{TU}.txt
# where {TDLO} and {TDHI} are the start and end datetimes. Each of them
# must be "{yyyy}-{mm}-{dd}" if {TU} is "01d" of multiple thereof;
# of "{yyyy}-{mm}-{dd}-{HH}{MM}" if {TU} is "01h", "01m", or multiple thereof.
#
# Each output line is "{TDLO} {TDHI} {EX} {CR} {TU}"
# where {TDLO} and {TDHI} have been reformatted to "{yyyy}-{mm}-{dd} {HH}:{MM}:{SS}"
# independently of the {TU}.

/^[ ]*([\#]|$)/ { 
  # Blank or comment line, ignore
  next
}

/^20*/ {
  olin = $0 # Save original line
  gsub(/[.]txt$/, "", $0)
  gsub(/--/, " ", $0)
  $0 = gensub(/-([A-Z])/, " \\1", "g", $0)
  $0 = gensub(/([A-Z])-/, "\\1 ", "g", $0)
  if (NF != 5) { printf "** bug NF = %d\n  \"%s\"\n  \"%s\"\n", NF, olin, $0 > "/dev/stderr"; exit(1) }
  for (i = 1; i <= 2; i++){
    d = $(i)
    if (length(d) == 10) { d = (d "-0000") } 
    if (length(d) == 15) 
      { d = (substr(d,1,13) ":" substr(d,14,2)) }
    else 
      { printf "** bug length \"%s\"\n", olin > "/dev/stderr"; exit(1) }
    $(i) = d
  }
  gsub(/[-:]/, " ", $0)
  if (NF != 13) { printf "** bug NF new = %d\n  \"%s\"\n  \"%s\"\n", NF, olin, $0 > "/dev/stderr"; exit(1) }
  printf "%s-%s-%s %s:%s:00 %s-%s-%s %s:%s:00 %s %s %s\n", $1,$2,$3,$4,$5, $6,$7,$8,$9,$10, $11, $12, $13
  next
}

// { 
  printf "** bug unrecognized format \"%s\"\n", $0 > "/dev/stderr"; exit(1)
}