ambevar-dotfiles/.scripts/ubuilder

167 lines
3.2 KiB
Plaintext
Raw Normal View History

#!/bin/sh
if [ $# -ne 1 ] || [ "$1" = "-h" ]; then
cat<<EOF
Usage: ${0##*/} PKGBUILD
Convert an Arch Linux PKGBUILD to a univesral standalone bash script. (The
infamous 'bash' is assumed because PKGBUILD are bash scripts.)
EOF
exit
fi
if [ -z "$(command -v realpath)" ]; then
echo "You need 'realpath'"
exit
fi
if [ ! -f "$1" ]; then
echo "$1 must be a PKGBUILD file."
exit
fi
OUTPATH="$(realpath "$1")"
OUTPATH="${OUTPATH%/*}"
UBUILD="${OUTPATH}/UBUILD.sh"
if [ -f "$UBUILD" ]; then
echo "$UBUILD already exists."
exit
fi
echo '#!/bin/bash
if [ -z "$(command -v realpath)" ];then
echo "You need 'realpath'."
exit
fi
if [ -z "$(command -v wget)" ];then
echo "You need 'wget'."
exit
fi
################################################################################
' > "$UBUILD"
cat "$1" | sed -e 's|--prefix=/usr|--prefix=/usr/local|g' \
-e 's|PREFIX=/usr|PREFIX=/usr/local|g' >> "$UBUILD"
echo '
################################################################################
_ROOT="$(realpath "$0")"
_ROOT="${_ROOT%/*}"
srcdir=src
pkgdir=pkg
init()
{
mkdir -p "$_ROOT/$srcdir"
mkdir -p "$_ROOT/$pkgdir"
srcdir="$(realpath "$srcdir")"
pkgdir="$(realpath "$pkgdir")"
}
fetch()
{
for i in $source; do
echo "[$i]"
if [ $(echo $i | grep -c "/") -ne 0 ]; then
FILE="${i##*/}"
## TODO: add support for DLAGENT
wget "$OPT_FORCE" "$i" -O "$FILE"
case "$FILE" in
*.tar.bz2|*.tbz2) CODEC="j";;
*.tar.gz|*.tgz) CODEC="z";;
*.tar.xz|*.txz) CODEC="J";;
*) echo "[$1]: unrecognized file compression"
return 1;;
esac
## TODO: checksum support.
tar xvzf "$FILE" -C "$srcdir"
fi
done
}
cleanse ()
{
(cd "$pkgdir" && \
find . -type f -executable -exec strip -s {} \; \
)
}
clean ()
{
rm -r "$srcdir" "$pkgdir"
}
compress()
{
tar cvJf "$pkgname-$pkgver.tar.xz" -C "$pkgdir" usr/ --owner=root --group=root
}
_printhelp ()
{
cat <<EOE
Usage: ${1##*/} [OPTIONS]
This script can perform multiple operations:
1. Fetch source code.
2. Build.
3. Create package.
4. Clean folder from build data.
Default operation is to fetch source code if required.
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.
-p: Create package.
EOE
}
OPT_FORCE="nc"
OPT_BUILD=false
OPT_PACKAGE=false
OPT_CLEAN=false
while getopts ":bcfhp" opt; do
case $opt in
b)
OPT_BUILD=true ;;
c)
OPT_CLEAN=true ;;
f)
OPT_FORCE="" ;;
h)
_printhelp "$0"
exit 1 ;;
p)
OPT_PACKAGE="" ;;
?)
_printhelp "$0"
exit 1 ;;
:)
echo "Missing argument."
_printhelp "$0"
exit 1 ;;
esac
done
shift $(($OPTIND - 1))
init
fetch
if $OPT_BUILD; then (cd "$srcdir" && build); fi
if $OPT_PACKAGE; then (cd "$srcdir" && package) && cleanse && compress; fi
if $OPT_CLEAN; then clean; fi
' >> "$UBUILD"
chmod +x "$UBUILD"