#! /bin/bash
# Last edited on 2021-04-21 17:20:28 by jstolfi

ifile="US_Trasury_Gold_all_years-20210421.csv"
ofile="US_Trasury_Gold.txt"

cat ${ifile} \
  | gawk \
      ' BEGIN{ odt = ""; nf = 13 }
        //{ 
          if (FNR == 1) { 
            FS = "!"; OFS = "!"; 
            gsub(/[,]/, "!", $0) 
          } else {
            FS = "|"; OFS = "|"; 
            gsub(/["][,]["]/, "\"|\"", $0)
          }
          gsub(/[-,]*[ ][-,]*/, "_", $0)
          gsub(/[_][_]+/, "_", $0)
          gsub(/["]/, "", $0)
          if ( NF != nf ) {
            printf "** bad format [%s]\n", $0 > "/dev/stderr"
            exit(1)
          }
          # Shorten fields: 
          gsub(/Record_Date/, "Date", $0)
          gsub(/Facility_Description/, "Fac", $0)
          gsub(/Form_Description/, "Form", $0)
          gsub(/Location_Description/, "Loc", $0)
          gsub(/Fine_Troy_Ounces/, "Oz", $0)
          gsub(/Book_Value/, "USD", $0)
          gsub(/Source_Line_Number/, "No", $0)
          gsub(/Fiscal_Year/, "FYr", $0)
          gsub(/Fiscal_Quarter_Number/, "FQ", $0)
          gsub(/Calendar_Year/, "Yr", $0)
          gsub(/Calendar_Quarter_Number/, "Q", $0)
          gsub(/Calendar_Month_Number/, "Mo", $0)
          gsub(/Calendar_Day_Number/, "Dy", $0)
          gsub(/Mint_Held_Gold_Deep_Storage/, "Mint_DS", $0)
          gsub(/Gold_Bullion/, "Bull", $0)
          gsub(/Denver_CO/, "Den", $0)
          gsub(/Fort_Knox_KY/, "FKn", $0)
          gsub(/West_Point_NY/, "WPt", $0)
          gsub(/Mint_Held_Gold_Working_Stock/, "Mint_Wk", $0)
          gsub(/Gold_Coins/, "Coin", $0)
          gsub(/All_Locations_Coins_blanks_miscellaneous/, "All", $0)
          gsub(/Federal_Reserve_Bank_Held_Gold/, "Fed", $0)
          gsub(/Federal_Reserve_Banks_NY_Vault/, "FNY", $0)
          gsub(/Federal_Reserve_Banks_Display/, "Fds", $0)
          
          # Insert  total and dash lines:
          dt = $1
          if ( dt != odt ) {
            if ( odt != "" ) { 
              if ( odt != "Date" ) {
                print_dashes(nf)
                print_subtotal(nf)
              }
              print_dashes(nf)
            }
            odt = dt
          }
          print
        }
        END {
          print_dashes(nf)
          print_subtotal(nf)
        }
        function print_dashes(nf, i) {
          for (i = 0; i < nf-1; i++) { printf "--+" }
          printf "--\n"
        }
        function print_subtotal(nf, i) {
          printf "SUBTOTAL"
          for (i = 0; i < nf-1; i++) { printf "|0" }
          printf "\n"
        }
      ' \
  | txtable-reformat \
  > ${ofile}

