#!/bin/bash
# -*- coding: utf-8; mode: sh; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=sh:et:sw=4:ts=4:sts=4

# Note:
# This script is sourced by the mpbb wrapper script.
# Do not execute this directly!

list-subports-usage() {
    # "prog" is defined in mpbb-help.
    # shellcheck disable=SC2154
    cat <<EOF
usage: $prog [<global opts>] list-subports <port> [<port2> [...]]

Print the name and subports of each given port to standard output.

Run \`$prog help' for global options and a list of other subcommands.
EOF
}

print-subports() {
    local portname=$1
    local port
    local portgroup
    local ports
    local exclude
    local exclude_reasons
    local reason
    # $option_log_dir is set in mpbb
    # shellcheck disable=SC2154

    os_version="$(sw_vers -productVersion | cut -d . -f 1-2)"
    is_64bit_capable="$(sysctl -n hw.cpu64bit_capable)"
    # $optoin_prefix is set in mpbb
    # shellcheck disable=SC2154
    ports=$("${option_prefix}/bin/port" -q info --index --line --name "${portname}" "subportof:${portname}" 2>/dev/null) || return
    for port in $ports; do
        exclude=0
        exclude_reasons=()

        if [[ "$port" =~ graveyard ]]; then
            exclude=1
            exclude_reasons+=("its name contains 'graveyard'")
        else
            # $thisdir is set in mpbb
            # shellcheck disable=SC2154
            for portgroup in $("${option_prefix}/bin/port-tclsh" "${thisdir}/tools/portgroups.tcl" "$port"); do
                if [ "$portgroup" = "obsolete-1.0" ]; then
                    exclude=1
                    exclude_reasons+=("it includes the obsolete 1.0 PortGroup")
                fi
            done
        fi

        if [[ $exclude -eq 0 && "${os_version}" = "10.6" && "${is_64bit_capable}" = "0" ]]; then
            supported_archs=$("${option_prefix}/bin/port-tclsh" "${thisdir}/tools/supported-archs.tcl" "${port}")
            if [[ -n "${supported_archs}" && ! ("${supported_archs}" == *"x86_64"* && "${supported_archs}" == *"i386"*) ]]; then
                exclude=1
                exclude_reasons+=("the ${os_version}_x86_64 builder will build it")
            fi
        fi

        if [ $exclude -eq 0 ]; then
            echo "$port"
        else
            if [ ${#exclude_reasons[@]} -eq 1 ]; then
                echo >&2 "Excluding '${port}' because ${exclude_reasons[0]}."
            else
                echo >&2 "Excluding '${port}' for the following reasons:"
                for reason in "${exclude_reasons[@]}"; do
                    echo >&2 " - ${reason}"
                done
            fi
        fi
    done
}

list-subports() {
    # $option_log_dir is set in mpbb
    # shellcheck disable=SC2154
    local log_subports_progress="${option_log_dir}/ports-progress.txt"

    if [ $# -le 0 ]; then
        err "Must specify at least one port"
        return 1
    fi

    success=0

    # prepare the log file and make sure to start with an empty one
    mkdir -p "$option_log_dir"
    > "$log_subports_progress"

    for p in "$@"; do
        print-subports "$p" && success=1
    done

    if [ $success -eq 0 ]; then
        err "None of the specified ports were found in the port index."
        return 1
    fi
}