dotfiles/.bin/pkg-depends
2024-12-31 10:47:38 +01:00

129 lines
3.3 KiB
Bash
Executable File

#!/bin/sh
# lazy config
_sqlports="/usr/local/share/sqlports"
_pkgpath="$(head -1 /etc/installurl)/$(uname -r)/packages/$(uname -m)"
_usage() {
echo "usage: pkg-depends [options] <pkgpath>"
echo "options: -r - include run dependencies (default)"
echo " -l - include lib dependencies (default)"
echo " -b - include build dependencies"
echo " -t - include test dependencies"
echo " -p - show packges names (default)"
echo " -P - show port paths"
echo " -d <dir> - download packages"
echo
echo "Only one flavor / subpackage in pkgpath(7) is supported."
exit 2
}
while getopts rlbtpPd: arg
do
case $arg in
l) _deps="$_deps 0" ;;
r) _deps="$_deps 1" ;;
b) _deps="$_deps 2" ;;
t) _deps="$_deps 3" ;;
p) _showmode=$(( _showmode + 1 )) ;;
P) _showmode=$(( _showmode + 2 )) ;;
d) _dldir=$OPTARG ;;
h) _usage ;;
?) _usage ;;
esac
done
shift $(($OPTIND - 1))
[ -z "$1" ] \
&& _usage
if [ ! -f $_sqlports ]
then
echo "$_sqlports not found. Install \"sqlports\" to use this script."
exit 1
fi
### FUNCTIONS
_do_pkg() {
echo "pkg-depends needs pkgpath(7) as argument, pkg_info -P can help."
echo "Here, I'm calling it for you:"
echo
echo "$ pkg_info -P $1"
pkg_info -P "$1"
exit 0
}
_do_sql() {
_query="
WITH RECURSIVE pkg(x) AS (
SELECT \"$1\"
UNION
SELECT distinct fulldepends FROM depends
JOIN pkg ON fullpkgpath=x WHERE type in ($2)
) SELECT x, fullpkgname FROM pkg
JOIN ports ON fullpkgpath=x;
"
sqlite3 "$_sqlports" "$_query" \
| cut -d: -f2
}
_handle_list() {
if [ -z "$_deps" ]
then
_deps="0,1"
else
_deps="$(echo "$_deps" \
| tr -s " " \
| sed 's/^ //g' \
| tr ' ' ',')"
fi
_list=$(_do_sql "$1" "$_deps")
_firstname=$(echo "$_list" | head -1 | cut -d"|" -f2)
echo "$_list" | sort -u | while read line
do
_portpath=$(echo "$line" | cut -d"|" -f1)
_pkgname=$(echo "$line" | cut -d"|" -f2)
if [ -n "$_dldir" ]
then
_extra="(downloading)"
fi
# default || both on
if [ -z "$_showmode" ] || [ "$_showmode" == "3" ]
then
echo "$_portpath ($_pkgname) $_extra"
fi
# pkgname only
if [ "$_showmode" == "1" ]
then
echo "$_pkgname $_extra"
fi
# portpath only
if [ "$_showmode" == "2" ]
then
echo "$_portpath $_extra"
fi
if [ -n "$_dldir" ]
then
mkdir -p "$_dldir"
ftp -MV -C -o $_dldir/$_pkgname.tgz $_pkgpath/$_pkgname.tgz
fi
done
if [ -n "$_dldir" ]
then
echo "Set the following environmane variables to enable offline installation:"
echo "export TRUSTED_PKG_PATH=\"$(readlink -f "$_dldir")\""
echo "export PKG_PATH=\"$(readlink -f "$_dldir")\""
echo "pkg_add $_firstname"
fi
}
### MAIN PROGRAM
case "$1" in
*/*) _handle_list "$1" ;;
*) _do_pkg "$1" ;;
esac