#!/bin/bash
VERSION=0.1.7
if which yay >/dev/null; then
    AUR="yay"
elif which pacaur >/dev/null; then
    AUR="pacaur"
else
    AUR="pacman"
fi
case $1 in
    "version")
	shift
	echo "aur helper version ${VERSION}"
	;;
    "clear" | "clean")
	shift
	$AUR -Sc
	;;
    "ls" | "list" | "freeze")
	shift
	$AUR -Qqe
	;;
    "up" | "upgrade" | "update")
	shift
	$AUR -Suy
	;;
    "search")
	shift
	$AUR -Ss $@
		;;
    "install")
	shift
	$AUR -S $@
	;;
    "rm" | "remove" | "delete" | "uninstall")
	shift
	$AUR -R $@
	;;
    "info")
	shift
	$AUR -Si $@
	;;
    "orphans")
	shift
	$AUR -Qtdq
	;;
    "optimize")
	shift
	sudo pacman-optimize
	;;
    "deps" | "dependencies")
	shift
	$AUR -Si $@ | awk -F'[:<=>]' '/^Depends/ {print $2}' | xargs -n1 | sort -u
	;;
    "changelog")
	shift
	$AUR -Qc $@
	;;
    "refresh-bin")
	shift
	if which sudo >/dev/null && which curl >/dev/null; then
	    sudo curl -L "https://raw.githubusercontent.com/avelino/aur/master/aur" -o /usr/local/bin/aur
	    sudo chmod +x /usr/local/bin/aur
	else
	    echo "The package sudo or curl is not installed"
	fi
	;;
    "install-bin")
        shift
        $AUR -S go && \
        go get github.com/Jguer/yay && \
        go install github.com/Jguer/yay
    ;;
    "refresh-keys")
        shift
        sudo pacman-key --refresh-keys
    ;;
    "clear-cache")
        shift
        sudo pacman -Scc
    ;;
    *)
	shift
	$AUR $@
	;;
esac
