#!/usr/bin/dash

# program that prompts the user to install a package if it is not already installed

if [ -z "$1" ]; then
    echo "no package to check"
    exit
fi

checkpackage() {
    if command -v "$1" || pacman -Qi "$1"; then
        echo "package $1 is installed"
        return 0
    else
        echo "package $1 missing"
        return 1
    fi
}

for i in $@
do
    echo "processing package $i"
    checkpackage "$i" && continue
    if ! imenu -c "the extra package $i is required. Download now?"; then
        echo "package will not be installed"
        exit 1
    fi
    INSTALLPACKAGES="true"
    if ! checkinternet; then
        imenu -e "internet is required"
        exit 1
    fi
done

if [ -n "$INSTALLPACKAGES" ]
then
    echo "running command: yay -S --needed --noconfirm $@; sleep 2; exit"
    st -e "bash" -c "yay -S --needed --noconfirm $@; sleep 2; exit"
fi


for i in $@
do
    checkpackage "$i" || exit 1
done

