#!/bin/sh

# Finds pacstrap in $PATH, or download some version of it if not found.
# This script prefers to use local copies that are already downloaded, over
# system-wide installations, in case you want to test on some specific
# outdated version of pacstrap.

LOCALDIR=`pwd`/arch-install-scripts
if test -e "$LOCALDIR/pacstrap"
then
    echo "Found $LOCALDIR/pacstrap in current directory"

    # Run pacstrap without installing it to the system.
    bash "$LOCALDIR/pacstrap" "$@"
elif IT=`command -v pacstrap`
then
    echo "Found $IT in PATH"

    # Use the version of pacstrap that was already installed.
    pacstrap "$@"
else
    echo "pacstrap not found. Downloading to $LOCALDIR."
    git clone https://gitlab.archlinux.org/archlinux/arch-install-scripts
    make -C arch-install-scripts pacstrap

    # Run pacstrap without installing it to the system.
    bash "$LOCALDIR/pacstrap" "$@"
fi

