#!/usr/bin/env sh
set -eu

ROOT="$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)"

usage() {
  cat <<'EOF'
Usage: ./bin/install [makepkg options]

Build and install Omairc as an Arch Linux package. Any options are passed to
makepkg, for example:

  ./bin/install --noconfirm
EOF
}

case "${1:-}" in
  -h|--help)
    usage
    exit 0
    ;;
esac

for command in git makepkg tar; do
  if ! command -v "$command" >/dev/null 2>&1; then
    echo "$command is required to build and install the Omairc package." >&2
    exit 1
  fi
done

if [ "$(id -u)" -eq 0 ]; then
  echo "Run bin/install as your regular user; makepkg will use sudo when needed." >&2
  exit 1
fi

VERSION="$("$ROOT/bin/version")"

WORK="$(mktemp -d "${TMPDIR:-/tmp}/omairc-package.XXXXXX")"
trap 'rm -rf -- "$WORK"' EXIT HUP INT TERM

{
  git -C "$ROOT" ls-files -z
  if ! git -C "$ROOT" ls-files --error-unmatch version.pri >/dev/null 2>&1; then
    printf '%s\0' version.pri
  fi
} | tar -C "$ROOT" --null -T - \
    --transform "s,^,omairc-$VERSION/," \
    -czf "$WORK/omairc-$VERSION.tar.gz"

cp "$ROOT/PKGBUILD" "$WORK/PKGBUILD"
cp "$ROOT/version.pri" "$WORK/version.pri"
sed -i 's|^source=.*|source=("$pkgname-$pkgver.tar.gz")|' "$WORK/PKGBUILD"

cd "$WORK"
makepkg -fsi "$@"
