#!/bin/sh -e

##########################################################################
#   Script description:
#       Print the path of the pkgsrc source tree for the currently active
#       pkgsrc installation.
#       
#   History:
#   Date        Name        Modification
#   2018?       Jason Bacon Begin
##########################################################################

prefix=$(auto-pkgsrc-prefix)
if [ -n "$PKGSRCDIR" ]; then
    # User-controlled
    dir="$PKGSRCDIR"
elif [ -e "${prefix}src" ]; then
    # Common practice to use /parent/pkgsrc with /parent/pkg
    dir="${prefix}src"
elif [ -e ${prefix%/pkg}/dreckly ]; then
    dir="${prefix%/pkg}/dreckly"
else
    # Default, replaced by $PKGSRCDIR in pkgsrc package
    # Binary packages built elsewhere may have a different source
    # tree location than the target system, so this is unreliable
    # when using pkg_add or pkgin
    dir="/usr/pkgsrc"
fi

if [ -e $dir ]; then
    printf "$dir\n"
fi
