#!/bin/sh
# install AUR packages
basebuilddir="$HOME/.simpleaur/Build"
pacmanexist=$(which pacman 2> /dev/null)
lockfile="$basebuilddir/simpleaur.lock"

ctrl_c() {
    printf "\n\n\e[1;31m%-6s\e[m%s\n" "Keyboard Interrupt detected, removing lock file and leaving..."
    rm "$lockfile"
    exit
}

trap ctrl_c SIGINT

#Prevent two instances running at same time
if [ -f "$lockfile" ]; then
    printf "\n\e[1;31m%-6s\e[m%s\n" "simpleaur is running, leaving."
    printf "\n\e[1;31m%-6s\e[m%s\n" "If you are sure that simpleaur is not running, delete [ '$lockfile' ]"
    exit
else
    touch "$lockfile"
fi
#Make sure that you are using an ArchLinux system
if [ -x "$pacmanexist" ]; then
    continue
else
    printf "\n\e[1;31m%-6s\e[m%s\n" "Error: You are not using ArchLinux, pacman was not found in ($PATH)"
    rm "$lockfile"
    exit
fi

#Check if the base Build DIR exists, if not create it.
if [ ! -d "$basebuilddir" ]; then
    mkdir -p "$basebuilddir"
fi

if [[ "$@" ]]; then
    cd "$basebuilddir" || exit
else
    printf "\n\e[1;31m%-6s\e[m%s\n" "Error: Please, pass AUR package(s) name(s) to simpleaur."
    rm "$lockfile"
    exit
fi

# Check if the package was already downloaded and then download the PKGBUILDs accordly to user needs.
for ipackage in "$@"; do
    if [ -d "$basebuilddir/$ipackage" ]; then
        printf "\n\e[1;32m%-6s\e[m\n%s\n" "PKGBUILD for [ '$ipackage' ] exist in the [ '$basebuilddir/$ipackage' ] directory, if you want to update the package because an update was found by simplecheck, then you need remove the existing directory to update the package, otherwise you are reinstaling the same package version that's already installed."
        read -p "Remove the [ '$basebuilddir/$ipackage' ] directory? [Y/n] " rmolddir
        if [ "$rmolddir" = "y" ]; then
            printf "\n\e[1;32m%-6s\e[m\n%s\n" "Info: removing the old directory..."
            rm -rf "$basebuilddir/$ipackage"
            git clone "https://aur.archlinux.org/${ipackage}.git"
        elif [ "$rmolddir" = "n" ]; then
            continue
        else
            printf "\n\e[1;32m%-6s\e[m\n%s\n" "Wrong option, continuing..."
            continue
        fi
    else
        git clone "https://aur.archlinux.org/${ipackage}.git"
    fi
done


# Check PKGBUILDs.
for ipackage in "$@"; do
    cd "$basebuilddir/$ipackage"
    read -s -r -n 1 -p "View PKGBUILD for [ "$ipackage" ]? [Y/n] "$'\n' answer
    [[ "${answer,,}" =~ ^(yes|y)$ ]] && ${EDITOR} PKGBUILD
done

cd "$basebuilddir"

# Install packages.
for ipackage in "$@"; do
    #Check if PKGBUILD exist
    if [ -f "$ipackage/PKGBUILD" ]; then
        cd "$ipackage"
        makepkg -srci --noconfirm
        if [ "$?" -eq 0 ]; then
            cd "$basebuilddir"
            continue
        else
            printf "\n\e[1;31m%-6s\e[m%s\n" "Info: Installation not completed."
            cd "$basebuilddir"
        fi
    else
        printf "\n\e[1;31m%-6s\e[m%s\n" "Error: The PKGBUILD file for [ $ipackage ] does not exist, please check your internet connection and make sure that the AUR package name exists, you can use simplesearch for that."
        printf "\n\e[1;32m%-6s\e[m\n%s\n" "Info: Removing the failed build directory..."
        rm -rf "$basebuilddir/$ipackage"
    fi
done

#Ask to the user if want to remove the directory created for git
read -p "Remove previously compiled packages? [Y/n]? " yn
#Take the answer from the user input and decide what to do.
if [ "$yn" = "y" ]; then
    printf "\n\e[1;32m%-6s\e[m\n%s\n" "Info: Removing build directory..."
    rm -rf "$basebuilddir/*"
else
    printf "\n\e[1;32m%-6s\e[m\n%s\n" "Info: Instalation completed."
fi
rm -f "$lockfile"
exit
