170 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			170 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
#!/bin/sh
 | 
						|
 | 
						|
# lazy config
 | 
						|
_sqlports="/usr/local/share/sqlports"
 | 
						|
_pkgpath="$(head -1 /etc/installurl)/$(uname -r)/packages/$(uname -m)"
 | 
						|
 | 
						|
# parameters can be freely combined
 | 
						|
_usage() {
 | 
						|
    echo "usage: pkg-depends [options] <pkgpath>"
 | 
						|
    echo "options: -r       - list run dependencies (default)"
 | 
						|
    echo "         -l       - list lib dependencies (default)"
 | 
						|
    echo "         -b       - list build dependencies"
 | 
						|
    echo "         -t       - list test dependencies"
 | 
						|
    echo "         -p       - show packges names (default)"
 | 
						|
    echo "         -P       - show port paths"
 | 
						|
    echo "         -d <dir> - download packages to <dir>"
 | 
						|
    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
 | 
						|
 | 
						|
# a little helper in case no pkgpath is provided
 | 
						|
_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() {
 | 
						|
 | 
						|
    # XXX: This lists all dependencies for the dependencies for the
 | 
						|
    # dependencies. I haven't encountert an endless loop here (yet?).
 | 
						|
    # I guess sqlite takes care or that?
 | 
						|
    # I'm sure the performance can be improved by not using the big
 | 
						|
    # ports view to grab the pkgname. But it's late and it works and
 | 
						|
    # this is a problem for another day.
 | 
						|
    _query="
 | 
						|
    WITH RECURSIVE pkg(x) AS (
 | 
						|
        SELECT \"$1\"
 | 
						|
        UNION
 | 
						|
        SELECT distinct dependspath FROM canonical_depends
 | 
						|
        JOIN pkg ON fullpkgpath=x WHERE type in ($2)
 | 
						|
    ) SELECT x, fullpkgname FROM pkg
 | 
						|
      JOIN ports ON fullpkgpath=x;
 | 
						|
    "
 | 
						|
    echo "SQL QUERY: $_query" > $HOME/pkg-depends.log
 | 
						|
    echo "RESULT:" >> $HOME/pkg-depends.log
 | 
						|
    sqlite3 "$_sqlports" "$_query" | tee -a $HOME/pkg-depends.log
 | 
						|
}
 | 
						|
 | 
						|
_handle_list() {
 | 
						|
    # build the comma sparated list that descibes the types
 | 
						|
    # to include in the list. Used in the sql query.
 | 
						|
    # _deps are set in getops, with a leading space when 0
 | 
						|
    # is not set. FIXME: I'm sure there's a better way to do this.
 | 
						|
    if [ -z "$_deps" ]
 | 
						|
    then
 | 
						|
        _deps="0,1"
 | 
						|
    else
 | 
						|
        _deps="$(echo "$_deps" \
 | 
						|
                    | tr -s " " \
 | 
						|
                    | sed 's/^ //g' \
 | 
						|
                    | tr ' ' ',')"
 | 
						|
    fi
 | 
						|
 | 
						|
    # download mode: create directory provided in -d <dir>
 | 
						|
    if [ -n "$_dldir" ]
 | 
						|
    then
 | 
						|
        mkdir -p "$_dldir"
 | 
						|
    fi
 | 
						|
 | 
						|
    echo "Figuring out recursive dependencies, this may take some time..."
 | 
						|
    # FIXME: even though the list ist sorted -u this is only true for 
 | 
						|
    # pkgpath, as textproc/libical and textproc/libical,-main are the
 | 
						|
    # going to be the same package. Simply stripping off all ,-main
 | 
						|
    # could fix it.. but it doesn't feel right.
 | 
						|
    _do_sql "$1" "$_deps" | sort -u | while read line
 | 
						|
    do
 | 
						|
        _portpath=$(echo "$line" | cut -d"|" -f1)
 | 
						|
        _pkgname=$(echo "$line" | cut -d"|" -f2)
 | 
						|
 | 
						|
        # download mode: -d <dir>
 | 
						|
        if [ -n "$_dldir" ]
 | 
						|
        then
 | 
						|
            _extra="(downloading)"
 | 
						|
        fi
 | 
						|
 | 
						|
        # showmode:
 | 
						|
        # unset: shows pkgpath and pkgname
 | 
						|
        # 1: shows pkgname only
 | 
						|
        # 2: shows pkgpath only
 | 
						|
        # 3: equals unset # FIXME: set 3 as default and don't handle unset.
 | 
						|
        # 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
 | 
						|
 | 
						|
        # download mode: download file from mirror
 | 
						|
        # note: this needs sqlports information and the mirror to be
 | 
						|
        # in sync, which only the case on -release.
 | 
						|
        # FIXME: figure out a way to do this in snaps.
 | 
						|
        # Naive idea: cut off the version, get index list from mirror with
 | 
						|
        # match agains the non-version name and download everything found.
 | 
						|
        if [ -n "$_dldir" ]
 | 
						|
        then
 | 
						|
            ftp -MV -C -o $_dldir/$_pkgname.tgz $_pkgpath/$_pkgname.tgz
 | 
						|
        fi
 | 
						|
    done
 | 
						|
 | 
						|
    if [ -n "$_dldir" ]
 | 
						|
    then
 | 
						|
        # XXX: Are both variables needed?
 | 
						|
        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")\""
 | 
						|
        # FIXME: name the package that has been downloaded. But to translate $1
 | 
						|
        # to the package name, another sqlports db call would be necessary.
 | 
						|
        # There is also pkg_info -e ..., but that only works for alread installed
 | 
						|
        # packages.
 | 
						|
        echo "$ pkg_add <package>"
 | 
						|
    fi
 | 
						|
}
 | 
						|
 | 
						|
### MAIN PROGRAM :)
 | 
						|
case "$1" in
 | 
						|
    */*) _handle_list "$1" ;;
 | 
						|
      *) _do_pkg "$1" ;;
 | 
						|
esac
 |