#!/usr/pkg/bin/ruby26
# $Id: importNOS.rb 6708 2018-01-06 23:59:51Z flaterco $
# Main program for importing NOS data.  Import is divided into 3 passes to
# allow necessary fix-ups and checkpointing in between.

def usage
  puts "Usage: importNOS.rb N"
  puts "N=1  tidal reference stations"
  puts "N=2  tidal subordinate stations"
  puts "N=3  current subordinate stations"
  exit
end

usage() if ARGV.length != 1
pass = ARGV[0].to_i
usage() if pass.nil? or pass < 1 or pass > 3

# pg result has a #check method, but I find that the result is nil whenever
# something goes wrong, so what is the point of the check method?

# Standard/installed packages
require "set"
require "json"
require "pg"

# Breakouts from importNOS to create a thin veneer of cohesiveness.
require_relative "importNOS_Util"
require_relative "importNOS_JsonReaders"
require_relative "importNOS_Geogroups"
require_relative "importNOS_Queries"
require_relative "importNOS_DodgyCode"  # [TM]
require_relative "importNOS_TideRefs"
require_relative "importNOS_TideSubs"
require_relative "importNOS_CurSubs"

# We have:
# harcon.json, metadata maybe corresponding to old constants header
# tides.json, metadata corresponding to old stations list
# geogroups-tides.json, corresponding to headers in old stations list
# stations-by-group-tides.json, to assign stations to geogroups
# tiderefs/*/datums.json, the datums
# tiderefs/*/harcon.json, the harmonic constants

db = PG::Connection.open(:dbname => "harmbase2")
case pass
when 1
  importTideRefs(db)
when 2
  importTideSubs(db)
when 3
  importCurSubs(db)
else
  raise "Bogus pass number"
end
