#!/bin/bash
#
#  osmcoastline_readmeta [COASTLINEDB]
#

set -euo pipefail

OSMCOASTLINE_VERSION=@OSMCOASTLINE_VERSION@

SQLEXEC="sqlite3"

if [[ $# -ge 1 && ( $1 == "--help" || $1 == "-h" ) ]]; then
    echo "Usage: osmcoastline_readmeta [COASTLINEDB]"
    exit 0
fi

if [[ $# -ge 1 && ( $1 == "--version" || $1 == "-v" ) ]]; then
    echo "osmcoastline_readmeta version $OSMCOASTLINE_VERSION"
    echo "Copyright (C) 2012-2022  Jochen Topf <jochen@topf.org>"
    echo "License: GNU GENERAL PUBLIC LICENSE Version 3 <https://gnu.org/licenses/gpl.html>."
    echo "This is free software: you are free to change and redistribute it."
    echo "There is NO WARRANTY, to the extent permitted by law.";
    exit 0
fi

if [[ $# -eq 0 ]]; then
    DBFILE=testdata.db
else
    DBFILE=$1
fi

if [[ ! -e "$DBFILE" ]]; then
    echo "Can't open '$DBFILE'"
    exit 1
fi

function sql {
    $SQLEXEC -column "$DBFILE" | sed -e 's/^/  /'
}

function sql_select {
    echo -n "  $1: "
    $SQLEXEC "$DBFILE" "$2"
}

function sql_meta {
    sql_select "$1" "SELECT $2 FROM meta;"
}

function sql_options {
    sql_select "$1" "SELECT $2 FROM options;"
}

printf "Options used to create this data:\n\n"

sql_options "Overlap (--bbox-overlap/-b)" overlap
sql_options "Close gaps in coastline smaller than (--close-distance/-c)" close_distance
sql_options "Max points in polygons (--max-points/-m)" max_points_in_polygons
sql_options "Split large polygons" "CASE split_large_polygons WHEN 0 THEN 'no' ELSE 'yes' END"

sql_select "Spatial reference system (--srid/-s)" "SELECT CASE srid WHEN 4326 THEN '4326 (WGS84)' WHEN 3857 THEN '3857 (Mercator)' ELSE srid END FROM geometry_columns WHERE f_table_name='land_polygons';"

printf "\nMetadata:\n\n"

sql_meta "Database created at" timestamp
sql_meta "Runtime (minutes)" "CAST(round(CAST(runtime AS REAL)/60) AS INT)"
sql_meta "Memory usage (MB)" memory_usage
sql_meta "Ways tagged natural=coastline" num_ways
sql_meta "Number of nodes where coastline is not closed (before fixing)" num_unconnected_nodes
sql_meta "Coastline rings" num_rings
sql_meta "Coastline rings created from a single way" num_rings_from_single_way
sql_meta "Coastline rings created from more then one way" "num_rings - num_rings_from_single_way"
sql_meta "Number of rings fixed (closed)" num_rings_fixed
sql_meta "Number of rings turned around" num_rings_turned_around
sql_meta "Number of land polygons before split" num_land_polygons_before_split
sql_meta "Number of land polygons after split" "CASE num_land_polygons_after_split WHEN 0 THEN 'NOT SPLIT' ELSE num_land_polygons_after_split END"

printf "\nErrors/warnings (Points):\n\n"
printf ".width 3 20\nSELECT count(*), error FROM error_points GROUP BY error;" | sql

printf "\nErrors/warnings (LineStrings):\n\n"
printf ".width 3 20\nSELECT count(*), error FROM error_lines GROUP BY error;" | sql

printf "\nOutput:\n\n"
echo "SELECT count(*), 'land_polygons' FROM land_polygons;" | sql
echo "SELECT count(*), 'water_polygons' FROM water_polygons;" | sql
echo "SELECT count(*), 'lines' FROM lines;" | sql
echo "SELECT count(*), 'rings' FROM rings;" | sql

echo

