0
  5   ˆl–Ým_J
i?u	Aã"¸©‹Fÿ�þB7È³e�¤‘¶ßÄóÃƒqà‹ÂÁÀ œ    #!/bin/sh

#
# PostGIS - Spatial Types for PostgreSQL
# http://postgis.net
#
# Copyright (C) 2022 Sandro Santilli <strk@kbt.io>
#
# This is free software; you can redistribute and/or modify it under
# the terms of the GNU General Public Licence. See the COPYING file.
#

set -e

usage() {
  echo "Usage: $(basename $0) [ --skip-layers | --only-layers ] [ -f <dumpfile> ] <toponame>"
}



SKIP_LAYERS=no
ONLY_LAYERS=no
TOPONAME=
DUMPFILE=-

while test -n "$1"; do
  if test "$1" = '--skip-layers'; then
    SKIP_LAYERS=yes
    shift
  elif test "$1" = '--only-layers'; then
    ONLY_LAYERS=yes
    shift
  elif test "$1" = '-f'; then
    shift
    DUMPFILE="$1"
    shift
  elif test -z "${TOPONAME}"; then
    TOPONAME=$1
    shift
  else
    echo "Unrecognized option $1" >&2
    usage >&2
    exit 1
  fi
done

test -n "${TOPONAME}" || {
  usage >&2
  exit 1
}

if test "${SKIP_LAYERS}" = "yes" -a "${ONLY_LAYERS}" = "yes"; then
  echo "--skip-layers conflicts with --only-layers" >&2
  exit 1
fi

TMPDIR=${TMPDIR-/tmp}
WORKDIR=${TMPDIR}/pgtopo_import_$$
mkdir -p ${WORKDIR}

cleanup() {
  echo "Cleaning up" >&2
  rm -rf ${WORKDIR}
}

trap 'cleanup' 0

#################################################
# Uncompress the dumpfile
#################################################
tar xzf ${DUMPFILE} -C ${WORKDIR} >&2
DUMPDIR="${WORKDIR}/pgtopo_export"
test -d "${DUMPDIR}" || {
  echo "Missing 'pgtopo_export' dir in export file zip" >&2
  exit 1
}

#################################################
# Verify version
#################################################

DUMPVERSION=$(cat ${DUMPDIR}/pgtopo_dump_version)
test ${DUMPVERSION} -eq '1' || {
  echo "Unknown pgtopo dump version ${DUMPVERSION}" >&2
  exit 1
}

#################################################
# Start a transaction
#################################################

echo "BEGIN;"

if test "$ONLY_LAYERS" = "no"; then # {
  #################################################
  # Create topology
  #################################################

  TOPO_SRID=$(cut -f1 ${DUMPDIR}/topology)
  TOPO_PREC=$(cut -f2 ${DUMPDIR}/topology)
  TOPO_HASZ=$(cut -f3 ${DUMPDIR}/topology)
  cat<<EOF
  SELECT topology.CreateTopology(
    '${TOPONAME}',
    '${TOPO_SRID}',
    '${TOPO_PREC}',
    '${TOPO_HASZ}'
  );
EOF

  #################################################
  # Load primitives data
  #################################################

  # Load faces
  cat <<EOF
  COPY "${TOPONAME}".face (
    face_id,
    mbr
  ) FROM STDIN;
EOF
  cat ${DUMPDIR}/face
  echo "\\."

  # Load nodes
  cat <<EOF
  COPY "${TOPONAME}".node (
    node_id,
    geom,
    containing_face
  ) FROM STDIN;
EOF
  cat ${DUMPDIR}/node
  echo "\\."

  # Load edges
  cat <<EOF
  COPY "${TOPONAME}".edge_data (
    edge_id,
    start_node,
    end_node,
    next_left_edge,
    abs_next_left_edge,
    next_right_edge,
    abs_next_right_edge,
    left_face,
    right_face,
    geom
  ) FROM STDIN;
EOF
  cat ${DUMPDIR}/edge_data
  echo "\\."

  #################################################
  # Set id sequences of primitive tables
  #################################################
  cat <<EOF
    SELECT pg_catalog.setval(
      '"${TOPONAME}".node_node_id_seq',
      max(node_id),
      true
    )
    FROM "${TOPONAME}".node;

    SELECT pg_catalog.setval(
      '"${TOPONAME}".edge_data_edge_id_seq',
      max(edge_id),
      true
    )
    FROM "${TOPONAME}".edge_data;

    SELECT pg_catalog.setval(
      '"${TOPONAME}".face_face_id_seq',
      NULLIF(max(face_id), 0),
      true
    )
    FROM "${TOPONAME}".face;
EOF

fi # } ONLY_LAYERS != "no"

if test "$SKIP_LAYERS" = "no"; then # {

  #################################################
  # Create schemas of layer tables, if any
  #################################################

  for sch in $(cut -f2 ${DUMPDIR}/layers | sort -u); do
    echo "CREATE SCHEMA IF NOT EXISTS \"${sch}\";"
  done

  #################################################
  # Restore all layer tables
  ##################