1#!/bin/sh
2# SPDX-License-Identifier: LGPL-2.1-or-later
3set -eu
4
5# Try to guess the build directory:
6# we look for subdirectories of the parent directory that look like ninja build dirs.
7
8if [ -n "${BUILD_DIR:=}" ]; then
9    realpath "$BUILD_DIR"
10    exit 0
11fi
12
13root="$(dirname "$(realpath "$0")")"
14
15found=
16for i in "$root"/../*/build.ninja; do
17    c="$(dirname "$i")"
18    [ -d "$c" ] || continue
19    [ "$(basename "$c")" != mkosi.builddir ] || continue
20
21    if [ -n "$found" ]; then
22        echo "Found multiple candidates, specify build directory with \$BUILD_DIR" >&2
23        exit 2
24    fi
25    found="$c"
26done
27
28if [ -z "$found" ]; then
29    echo "Specify build directory with \$BUILD_DIR" >&2
30    exit 1
31fi
32
33realpath "$found"
34