#! /bin/bash
# Copyright (C) 2018-2020 Merspieler, merspieler _at_ airmail.cc
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

root_path="$( cd "$(dirname "$0")" ; pwd -P )/.."

which parallel > /dev/null
if [ $? == 1 ]; then
	echo "Please install parallel"
	exit 1
fi

num_jobs=4
prefix=""
first=1
while [[ $# -gt 0 ]]
do
key="$1"

case $key in
	-j|--jobs)
		num_jobs="$2"
		shift # past argument
		shift # past value
	;;
	-p|--prefix)
		prefix="$2"
		shift # past argument
		shift # past value
	;;
	-h|--help)
		echo "usage: chunk-import.sh <pbf-path> [OPTIONS]"
		echo "Imports every chunk into own db"
		echo ""
		echo "OPTIONS"
		echo "  -j, --jobs     Number of parallel jobs. Default: 4"
		echo "  -p, --prefix   Database prefix to be used"
		echo "  -h, --help     Shows this help and exit"
		exit 0
	;;

	*)
		if [ $first == "1" ]; then
			pbf_path="$1"
			shift # past pbf-path
			first=0
		else
			echo "Unknown option $key"
			exit 1
		fi
	;;
esac
done

if [ -z "$pbf_path" ]; then
	echo "No pbf path was given. See chunk-import -h for details"
	exit 1
fi

ls "$pbf_path"/*.osm.pbf | sed -e "s/.osm.pbf//" -e "s:^.*/::" | parallel -j$num_jobs --eta "$root_path/create-db $prefix{} && $root_path/read-pbf $prefix{} {}.osm.pbf"