#!/bin/sh

map="$1"
index="$2"

LC_COLLATE=C
LC_ALL=C
export LC_COLLATE LC_ALL

if [ -z "$index" ]; then
	printf 'usage: find ... -name \*.bz2 | %s <map-in> <index-out>\n' "$0" >&2
	exit 2
fi

tmp="`dirname $index`/tmp.$$"
rm -rf "$tmp"
mkdir -p "$tmp" || exit 1
dir="`dirname $0`"

batch=0
while read file; do
	case "$file" in
	*.bz2) bzip2 -dc "$file" ;;
	*.gz)  gzip -dc "$file" ;;
	*.Z)   uncompress -dc "$file" ;;
	*)     cat "$file" ;;
	esac |
	"$dir/goo-ripper" --geocode "$map" "$batch" - | 
	sort -f > "$tmp/$batch.rip"
	batch=`expr $batch + 1`
done

sort -mf "$tmp"/*.rip | "$dir/idx-index" "$index"
sort -mf "$tmp"/*.rip | "$dir/idx-index" "$index"

