#!/usr/bin/env bash
set -euo pipefail

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT"

BUILD_DEB=false
SKIP_TESTS=false

for arg in "$@"; do
  case "$arg" in
    --deb) BUILD_DEB=true ;;
    --skip-tests) SKIP_TESTS=true ;;
    *) echo "Unknown option: $arg"; exit 1 ;;
  esac
done

echo "Building posprint for Linux (x86_64)..."

if [[ "$SKIP_TESTS" != "true" ]]; then
  cargo test
fi

cargo build --release
BIN="$ROOT/target/release/posprint"
echo "OK: $BIN"
ls -lh "$BIN"

if [[ "$BUILD_DEB" == "true" ]]; then
  if ! command -v cargo-deb &>/dev/null; then
    echo "Installing cargo-deb..."
    cargo install cargo-deb --locked
  fi
  cargo deb
  echo "Debian packages:"
  ls -lh "$ROOT/target/debian/"*.deb
fi
