ambevar-dotfiles/.scripts/ubuilder

105 lines
2.1 KiB
Bash
Executable File

#!/bin/sh
## TODO: add support for DLAGENT.
## TODO: add support for curl.
_printhelp()
{
cat<<EOF
Usage: ${1##*/} [OPTIONS] PKGBUILDs
Convert an Arch Linux PKGBUILD to a universal standalone bash script. (The
infamous 'bash' is assumed because PKGBUILD are bash scripts, they strongly rely
on arrays.) The resulting script tries to resemble makepkg build process where
it makes sense. There is no dependency resolution because we cannot guess the
package names of the target system.
Differences with makepkg
* Parameters may differ.
* The progress output may differ.
* Some features are not yet implemented (e.g. explicit source filename)
Options:
-h: Show this help.
-f: Overwrite existing builder.
-l: Install in usr/local/ instead of usr/.
EOF
}
OPT_LOCAL=false
OPT_OVERWRITE=false
while getopts ":flh" opt; do
case $opt in
h)
_printhelp "$0"
exit 1 ;;
l)
OPT_OVERWRITE=true ;;
l)
OPT_LOCAL=true ;;
?)
_printhelp "$0"
exit 1 ;;
:)
_printhelp "$0"
exit 1 ;;
esac
done
shift $(($OPTIND - 1))
if [ $# -eq 0 ]; then
_printhelp "$0"
exit 1
fi
if ! command -v realpath >/dev/null; then
echo "You need 'realpath'"
exit
fi
for i ; do
if [ ! -f "$i" ]; then
echo "$i must be a PKGBUILD file."
continue
fi
OUTPATH="$(realpath "$i")"
OUTPATH="${OUTPATH%/*}"
PKGNAME="$(awk -F= '/^pkgname/{print $2}' "$i")"
UBUILD="${OUTPATH}/$PKGNAME.sh"
if [ -f "$UBUILD" ] && ! $OPT_OVERWRITE; then
echo "$UBUILD already exists."
continue
fi
cat<<"EOF" > "$UBUILD"
#!/bin/bash
if ! command -v realpath >/dev/null; then
echo "You need 'realpath'."
exit
fi
if ! command -v wget >/dev/null; then
echo "You need 'wget'."
exit
fi
################################################################################
EOF
if $OPT_LOCAL; then
sed -e 's|/usr|/usr/local|g' "$i" >> "$UBUILD"
else
cat "$i" >> "$UBUILD"
fi
cat "${0%/*}/.${0##*/}.in" >> "$UBUILD"
chmod +x "$UBUILD"
done