#!/usr/bin/env bash

set -e  # Exit immediately if a command exits with a non-zero status
set -o pipefail

echo "🔄 Uuendan pakettide nimekirja..."
sudo apt update

echo "⬇️  Paigaldan vajalikud sõltuvused..."
sudo apt install -y build-essential cmake libboost-dev libboost-system-dev \
  libboost-filesystem-dev libexpat1-dev zlib1g-dev libbz2-dev libpq-dev \
  libproj-dev lua5.3 liblua5.3-dev git nlohmann-json3-dev

echo "📁 Kloonime osm2pgsql repositooriumi..."
if [ -d "osm2pgsql" ]; then
  echo "⚠️  Kataloog 'osm2pgsql' on juba olemas. Kasutan olemasolevat."
else
  git clone https://github.com/openstreetmap/osm2pgsql.git
fi

cd osm2pgsql

echo "🛠  Loome build-kataloogi..."
mkdir -p build
cd build

echo "⚙️  Käivitame cmake..."
cmake -DCMAKE_CXX_STANDARD=14 ..

echo "🔨 Käivitame make..."
make -j$(nproc)

echo "📦 Paigaldame osm2pgsql..."
sudo make install

echo "✅ Paigaldus on lõpetatud. Kontrollime versiooni:"
osm2pgsql --version
