#!/bin/bash

# Mapsforge map creation (with coastlines) - Modified by Mikael

# Osmosis folder
OSMOSIS=/opt/osmosis/bin/osmosis

# Input folder
INPUTS=/opt/osmosis/inputs

# Change to desired region
REGION=bretagne

# Use existing files, but if necessary can download...
# echo "Downloading $1..."
# wget -nv -N -P "$WORK_PATH" http://download.geofabrik.de/$1-latest.osm.pbf || exit 1
# wget -nv -N -P "$WORK_PATH" http://download.geofabrik.de/$1-latest.osm.pbf.md5 || exit 1
# (cd "$WORK_PATH" && exec md5sum -c "$NAME-latest.osm.pbf.md5") || exit 1
# wget -nv -N -P "$WORK_PATH" http://download.geofabrik.de/$1.poly || exit 1

# Bounds

BBOX=$(perl poly2bb.pl "$INPUTS/$REGION/$REGION.poly")
BBOX=(${BBOX//,/ })
BOTTOM=${BBOX[0]}
LEFT=${BBOX[1]}
TOP=${BBOX[2]}
RIGHT=${BBOX[3]}

# Start position

CENTER=$(perl poly2center.pl "$INPUTS/$REGION/$REGION.poly")
CENTER=(${CENTER//,/ })
LAT=${CENTER[0]}
LON=${CENTER[1]}

# Land

ogr2ogr -overwrite -progress -skipfailures -clipsrc $LEFT $BOTTOM $RIGHT $TOP "$INPUTS/$REGION/${REGION}_land.shp" "$INPUTS/land-polygons-split-4326/land_polygons.shp"
python shape2osm.py -l "$INPUTS/$REGION/${REGION}_land" "$INPUTS/$REGION/${REGION}_land.shp"

# Sea

cp sea.osm "$INPUTS/$REGION/"
sed -i "s/\$BOTTOM/$BOTTOM/g" "$INPUTS/$REGION/sea.osm"
sed -i "s/\$LEFT/$LEFT/g" "$INPUTS/$REGION/sea.osm"
sed -i "s/\$TOP/$TOP/g" "$INPUTS/$REGION/sea.osm"
sed -i "s/\$RIGHT/$RIGHT/g" "$INPUTS/$REGION/sea.osm"

CMD="$OSMOSIS --rb file=$INPUTS/$REGION/$REGION-latest.osm.pbf \
              --rx file=$INPUTS/$REGION/sea.osm --s --m"
for f in $INPUTS/$REGION/${REGION}_land*.osm; do
  CMD="$CMD --rx file=$f --s --m"
done
CMD="$CMD --wb file=$INPUTS/$REGION/${REGION}_merge.pbf omitmetadata=true"
echo $CMD
$CMD

exit 0

# Map

if [ "$SKIP_MAP_CREATION" != "true" ]; then
  CMD="$OSMOSIS_HOME/bin/osmosis --rb file=$WORK_PATH/merge.pbf"
  [ $MAP_TRANSFORM_FILE ] && CMD="$CMD --tt file=$MAP_TRANSFORM_FILE"
  CMD="$CMD --mw file=$WORK_PATH/$NAME.map \
                 type=$2 \
                 bbox=$BOTTOM,$LEFT,$TOP,$RIGHT \
                 map-start-position=$LAT,$LON \
                 map-start-zoom=8 \
                 tag-values=$TAG_VALUES \
                 threads=$THREADS"
  [ $3 ] && CMD="$CMD preferred-languages=$3"
  [ $MAP_TAG_CONF_FILE ] && CMD="$CMD tag-conf-file=$MAP_TAG_CONF_FILE"
  echo $CMD
  $CMD || exit 1

  # Check map size

  if [ -f "$MAPS_PATH/$NAME.map" ]; then
    OLD_SIZE=$(wc -c < "$MAPS_PATH/$NAME.map")
    NEW_SIZE=$(wc -c < "$WORK_PATH/$NAME.map")
    if [ $NEW_SIZE -lt $(($OLD_SIZE * 70 / 100)) ]; then
      echo "$WORK_PATH/$NAME.map creation is significantly smaller."
      exit 1
    fi
  fi
  mv "$WORK_PATH/$NAME.map" "$MAPS_PATH/$NAME.map"
fi

# POI

if [ "$SKIP_POI_CREATION" != "true" ]; then
  CMD="$OSMOSIS_HOME/bin/osmosis --rb file=$WORK_PATH/$NAME-latest.osm.pbf \
                                 --pw file=$WORK_PATH/$NAME.poi"
  [ $POI_TAG_CONF_FILE ] && CMD="$CMD tag-conf-file=$POI_TAG_CONF_FILE"
  echo $CMD
  $CMD || exit 1
  mv "$WORK_PATH/$NAME.poi" "$POIS_PATH/$NAME.poi"
fi

# Post-process

rm -rf "$WORK_PATH"
