#! /usr/bin/gawk -f
# Last edited on 2014-06-28 02:31:53 by stolfilocal

BEGIN {
  # Reads a table with {NAME} {INIDATE} {INITIME} {FINDATE} {FINTIME}
  # Writes commands that rename the raw and cleaned transaction log
  # files with each {NAME} to {INIDATE}-{INIHM}--{FINDATE}-{FINHM}-{EX}-{CR}-{TAG}.txt
  # where {EX} is the exchange, {CR} the price currency, and {TAG} is "raw", "raw-apple", or "all",
  # or whatever tag it had before.
}

/^20[0-9][0-9]/ { 
  if (NF != 5) { data_error(("bad num fields")); }
  name = $1;
  idt = $2;
  itm = $3;
  fdt = $4;
  ftm = $5;
  
  # Extract relevant fields from the {name}:
  ndt = date_from_name(name);    
  if ((ndt != idt) && (ndt != fdt)) { data_warning(("inconsistent date in name")); }
  ex = exchange_from_name(name); 
  tag = tag_from_name(name);
  if (tag == "") { tag = "all"; }
  
  # Get the working currency code:
  cr = currency_from_exchange(ex);

  # Condense the times:
  itmx = trim_time(itm,+1);
  ftmx = trim_time(ftm,-1);
  
  if ((idt " " itmx) > (fdt " " ftmx))
  { data_warning(("interval crossover \"" idt " " itmx "\" \"" fdt " " ftmx "\"")); }
  
  # Output command: 
  printf "rename_trans %s %s-%s--%s-%s-%s-%s  %s\n", name, idt, itmx, fdt, ftmx, ex, cr, tag;
  
  next;
}

// { data_error(("bad format")); }

function date_from_name(name,  ndt)
  { # Extracts the date from the start of the file {name}.
    ndt = name;
    # remove "-{tag}" suffix, if any:
    gsub(/-[a-z][-a-z]*$/, "", ndt);
    # remove "-{HHMM}-{ex}":
    gsub(/-[0-9][0-9][0-9][0-9]-[A-Z][A-Z0-9][A-Z0-9][A-Z0-9]$/, "", ndt);
    # Check format "{yyyy}-{mm}-{dd}":
    if (ndt !~ /^20[1-9][0-9]-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$/) 
      { data_error(("invalid date \"" ndt "\" in name \"" name "\"")); }
    return ndt;
  }

function exchange_from_name(name,  ex)
  { # Extracts the exchange name from the file {name}.
    ex = name;
    # remove "-{tag}" suffix, if any:
    gsub(/-[a-z][-a-z]*$/, "", ex);
    # remove "{yyyy}-{mm}-{dd}-{HHMM}-":
    gsub(/^20[1-9][0-9]-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])-[0-9][0-9][0-9][0-9]-/, "", ex);
    # Check format:
    if (ex !~ /^[A-Z][A-Z0-9][A-Z0-9][A-Z0-9]$/) 
      { data_error(("invalid exchange name \"" ex "\" in name \"" name "\"")); }
    # Fix "BTCN" in old file names to "BTCC":
    if (ex == "BTCN") { ex = "BTCC"; }
    return ex;
  }

function tag_from_name(name,  tag)
  { # Extracts the final tag from the file {name}, exccluding the "-"; returns "" if none.
    tag = name;
    # remove "{yyyy}-{mm}-{dd}-{HHMM}-":
    gsub(/^20[1-9][0-9]-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])-[0-9][0-9][0-9][0-9]-/, "", tag);
    # remove "{ex}-":
    gsub(/^[A-Z][A-Z0-9][A-Z0-9][A-Z0-9][-]?/, "", tag);
    # Check format:
    if ((tag != "") && (tag !~ /^[a-z][-a-z]*[a-z]$/)) 
      { data_error(("invalid tag \"" tag "\" in name \"" name "\"")); }
    return tag;
  }

function currency_from_exchange(ex,  cr)
  { # Returns the national currency used for this exchange.
    if ((ex == "HUBI") || (ex == "OKCO") || (ex == "BTER") || (ex == "BTCC"))
      { cr = "CNY"; }
    else if ((ex == "BSTP") || (ex == "MGOX") || (ex == "BTCE") || (ex == "BFIN")) 
      { cr = "USD"; }
    else
      { data_error(("unknown exchange name \"" ex "\"")); }
    return cr;
  }

function trim_time(tm,dtm,  h,m,tmx)
  { # Converts {tm} from "{HH}:{MM}:{SS}" to "{HH}{MM}", adds {dtm} minutes (may be negative).
    if (tm !~ /^([01][0-9]|2[0-3])[:][0-5][0-9][:]([0-5][0-9]|60)$/)
      { data_error(("invalid time \"" tm "\"")); }
    # Extract hour {h} and minute {m}: 
    h = tm; 
    gsub(/[:][0-9]+[:][0-9]+$/, "", h);
    m = tm; 
    gsub(/[:][0-9]+$/, "", m);
    gsub(/^[0-9]+[:]/, "", m);
    # Increment the minutes and normalize: 
    m = m + dtm;
    while (m >= 60) { m = m - 60; h = h + 1; }
    while (m < 0) { m = m + 60; h = h - 1; }
    if ((h > 23) || (h < 0)) 
      { data_error(("hour over/underflow \"" h ":" m "\" from time \"" tm "\"")); }
    return sprintf("%02d%02d", h, m);
  }

function data_error(msg)
  { printf "%s:%s: ** %s\n", FILENAME, FNR, msg > "/dev/stderr"; 
    printf "  «%s»\n", $0 > "/dev/stderr"; 
    abort = 1;
    exit(abort);
  } 

function data_warning(msg)
  { printf "%s:%s: !! %s\n", FILENAME, FNR, msg > "/dev/stderr"; 
    printf "  «%s»\n", $0 > "/dev/stderr"; 
  } 
          
function arg_error(msg)
  { printf "** %s\n", msg > "/dev/stderr"; 
    abort = 1;
    exit(abort);
  }