#!/usr/bin/env bash

# need a Python 3.6+ environment with Psycopg (run 01_setup_conda_env.sh to create Conda environment)
conda deactivate
conda activate geo

# ---------------------------------------------------------------------------------------------------------------------
# edit these to taste - NOTE: you can't use "~" for your home folder, Postgres doesn't like it
# ---------------------------------------------------------------------------------------------------------------------

AWS_PROFILE="minus34"
OUTPUT_FOLDER_2020="/Users/$(whoami)/tmp/geoscape_202508_gda2020"
GNAF_2020_PATH="/Users/$(whoami)/Downloads/g-naf_aug25_allstates_gda2020_psv_1020"
BDYS_2020_PATH="/Users/$(whoami)/Downloads/AUG25_AdminBounds_GDA_2020_SHP"

echo "---------------------------------------------------------------------------------------------------------------------"
echo "Run gnaf-loader and locality boundary clean"
echo "---------------------------------------------------------------------------------------------------------------------"

python3 "/Users/$(whoami)/git/minus34/gnaf-loader/load-gnaf.py" --pgport=5432 --pgdb=geo --max-processes=6 --gnaf-tables-path="${GNAF_2020_PATH}" --admin-bdys-path="${BDYS_2020_PATH}" --srid=7844 --gnaf-schema gnaf_202508_gda2020 --admin-schema admin_bdys_202508_gda2020 --previous-gnaf-schema gnaf_202508 --previous-admin-schema admin_bdys_202508
python3 "/Users/$(whoami)/git/iag_geo/psma-admin-bdys/locality-clean.py" --pgport=5432 --pgdb=geo --max-processes=6 --output-path=${OUTPUT_FOLDER_2020} --admin-schema admin_bdys_202508_gda2020

# upload locality bdy files to S3
aws --profile=${AWS_PROFILE} s3 sync ${OUTPUT_FOLDER_2020} s3://minus34.com/opendata/geoscape-202508-gda2020 --exclude "*" --include "*.zip" --acl public-read

# done in GDA94 script (files not processed for GDA2020 data as the result is 99.99999.....% the same)
#echo "---------------------------------------------------------------------------------------------------------------------"
#echo "create concordance file"
#echo "---------------------------------------------------------------------------------------------------------------------"
#
## create concordance file and upload to S3
#mkdir -p "${OUTPUT_FOLDER_2020}"
#python3 /Users/$(whoami)/git/iag_geo/concord/create_concordance_file.py --pgdb=geo --admin-schema="admin_bdys_202508_gda2020" --gnaf-schema="gnaf_202508_gda2020" --output-path=${OUTPUT_FOLDER_2020}
#aws --profile=${AWS_PROFILE} s3 sync ${OUTPUT_FOLDER_2020} s3://minus34.com/opendata/geoscape-202508-gda2020 --exclude "*" --include "*.csv" --acl public-read

echo "---------------------------------------------------------------------------------------------------------------------"
echo "dump postgres schemas to a local folder"
echo "---------------------------------------------------------------------------------------------------------------------"

/Applications/Postgres.app/Contents/Versions/14/bin/pg_dump -Fc -d geo -n gnaf_202508_gda2020 -p 5432 -U postgres -f "${OUTPUT_FOLDER_2020}/gnaf-202508.dmp" --no-owner
echo "GNAF schema exported to dump file"
/Applications/Postgres.app/Contents/Versions/14/bin/pg_dump -Fc -d geo -n admin_bdys_202508_gda2020 -p 5432 -U postgres -f "${OUTPUT_FOLDER_2020}/admin-bdys-202508.dmp" --no-owner
echo "Admin Bdys schema exported to dump file"

echo "---------------------------------------------------------------------------------------------------------------------"
echo "copy Postgres dump files to AWS S3 and allow public read access (requires AWSCLI installed & AWS credentials setup)"
echo "---------------------------------------------------------------------------------------------------------------------"

aws --profile=${AWS_PROFILE} s3 sync ${OUTPUT_FOLDER_2020} s3://minus34.com/opendata/geoscape-202508-gda2020 --exclude "*" --include "*.dmp" --acl public-read

