ambevar-dotfiles/.scripts/abs-wrapper

176 lines
3.8 KiB
Bash
Executable File

#!/bin/sh
## User config
UPSTREAM_SOURCE="srcdir"
## End of user config
_printhelp ()
{
cat <<EOF
Usage: ${1##*/} [OPTIONS] PACKAGES
This script can perform multiple operations:
1. Fetch PKGBUILD.
2. Fetch source code.
3. Build package.
4. Installing package.
5. Cleaning folder from build data.
Default operation is to fetch PKGBUILD for provided packages.
Operations or hierarchically dependant and check for existing data. E.g. if a
PKGBUILD is already there and you want to install the package, then it will
fetch source code first, then build and finally install.
It will fetch the PKGBUILD from the ABS-tree or yaourt, depending on what is
available.
Options:
-b: Build.
-c: Clean source folders from temp file (keep upstream source and built package).
-f: Force operation (overwrite, rebuild, reinstall).
-h: Display this help.
-i: Install package.
-s: Fetch source code.
Example:
${1##*/} -icf \$(pacman -Qmq)
Force fetching PKGBUILD and source code, force building, install and clean build folder for all foreign installed packages.
EOF
}
MAKEPKT_OPT=""
PACMAN_OPT="--needed"
OPT_BUILD=0
OPT_CLEAN=0
OPT_FORCE=0
OPT_INSTALL=0
OPT_SOURCE=0
while getopts ":bcfhis" opt; do
case $opt in
b)
OPT_BUILD=1 ;;
c)
MAKEPKG_OPT="$MAKEPKG_OPT -c"
OPT_CLEAN=1 ;;
f)
MAKEPKG_OPT="$MAKEPKG_OPT -f"
PACMAN_OPT=""
OPT_FORCE=1;;
h)
_printhelp "$0"
exit 1 ;;
i)
OPT_INSTALL=1 ;;
s)
OPT_SOURCE=1 ;;
?)
_printhelp "$0"
exit 1 ;;
:)
echo "Missing argument."
_printhelp "$0"
exit 1 ;;
esac
done
shift $(($OPTIND - 1))
if [ $# -eq 0 ]; then
echo "Missing argument."
_printhelp "$0"
exit 1
fi
if [ -z "$(command -v makepkg)" ]; then
echo "You need makepkg to run this script."
exit 1
fi
HAS_ABS=0
HAS_YAOURT=0
[ -n "$(command -v abs)" ] && HAS_ABS=1
[ -n "$(command -v yaourt)" ] && HAS_YAOURT=1
if [ $HAS_ABS -eq 0 ] && [ $HAS_YAOURT -eq 0 ]; then
echo "Please install either an ABS-tree or yaourt."
exit 1
fi
if [ $HAS_ABS -eq 1 ] && [ -f "/etc/abs.conf" ]; then
. "/etc/abs.conf"
[ "$ABSROOT" = "" ] && ABSROOT="/var/abs/"
fi
################################################################################
abs_pkgbuild ()
{
pacman -Si $i >/dev/null 2>&1
if [ $? -eq 0 ] && [ $HAS_ABS -eq 1 ] && [ -d "$ABSROOT" ]; then
cp -r /var/abs/*/$i .
else
yaourt -G "$i" --noconfirm
fi
}
abs_source ()
{
(cd "$i" && \
mkdir -p "$UPSTREAM_SOURCE" && \
SRCDEST="$UPSTREAM_SOURCE" makepkg -o)
}
abs_build ()
{
(cd "$i" && \
mkdir -p "$UPSTREAM_SOURCE" && \
SRCDEST="$UPSTREAM_SOURCE" makepkg -rs $MAKEPKG_OPT --nocheck --noconfirm)
}
abs_clean ()
{
(cd "$i" && \
echo "Removing src/ and pkg/ folders." && \
rm -rf "src" "pkg")
}
for i ; do
## Fetch PKGBUILD.
if [ ! -f "$i/PKGBUILD" ] || [ $OPT_FORCE -eq 1 ]; then
abs_fetch "$i"
fi
## Skip because $i does not exist.
[ ! -d "$i" ] && continue
## Fetch source code.
if [ $OPT_SOURCE -eq 1 ]; then
abs_source "$i"
fi
## Build.
if [ $OPT_BUILD -eq 1 ]; then
abs_build "$i"
fi
## Clean build folder.
if [ $OPT_CLEAN -eq 1 ]; then
abs_clean "$i"
fi
done
## Install packages. We put the install part outside the loop so that we prompt
## for password one time only.
if [ $OPT_INSTALL -eq 1 ]; then
sudo sh -c '
(cd "$0" && pacman '$PACMAN_OPT' --noconfirm -U *.pkg.tar.xz)
for i; do
(cd "$i" && pacman '$PACMAN_OPT' --noconfirm -U *.pkg.tar.xz)
done
' "$@"
fi