#!/bin/bash
# Contains logic to import files regardless of how many there are.
# If there's no file, don't do anything with the database.
# If the table already exists in the database, don't try to re-create it.
#
# Usage:
#   bin/import-file [csv_path] [table]
#   bin/import-file downloads/csv A-Contributions
set -euo pipefail

if [ -z "${DATABASE_NAME:-""}" ]; then
  echo "Error: Set DATABASE_NAME."
  exit 1
fi

if [ $# -eq 0 ]; then
  echo "Usage: bin/import-file [csv_path] [table]"
  exit 1
fi

csv_path=$1
table_name=$2
filename_glob=$csv_path'/*'${table_name}'.csv'
table_exists=
if psql disclosure-backend -c '\d "'${table_name}'"' >/dev/null 2>&1; then
  table_exists=true
fi

if ls $filename_glob 2>/dev/null >/dev/null; then
  ./bin/create-table $DATABASE_NAME $csv_path $table_name

  # insert data
  csvstack $filename_glob 2> /dev/null | \
    csvsql --db postgresql:///$DATABASE_NAME --tables $table_name --insert --no-inference --no-create
  echo -n '  Removing empty Tran_Date... '
  ./bin/clean "$DATABASE_NAME" "$table_name"
  echo
  echo -n '  Fixing pending Filer_IDs... '
  ./bin/fix-pending "$DATABASE_NAME" "$table_name"
else
  echo 'Found no files to import'
fi
