#!/bin/bash

# Finds ports that are using the unified python portgroup incorrectly.

# Ports using the unified python portgroup.
NAMES=$(port info --subports --index --line name:^py- | sed -E -n -e 's/^py[[:digit:]]{2}-//' -e 's/,.*$//p')

# Count them.
TOTALPYPORTS=$(echo $NAMES | wc -w)

PROBLEMPORTS=""
DEPENDENCYPROBLEMS=0
DEFAULTVERSIONPROBLEMS=0
DOCDIRPROBLEMS=0
LIVECHECKPROBLEMS=0
for NAME in $NAMES; do
    STUBPORT=py-$NAME
    STUBFILE="$(port file $STUBPORT)"
    
    DEPS=$(port info --depends_lib --depends_build --depends_run --index --line $STUBPORT | sed 's/[^[:space:]]*://g')
    
    # If the port declares dependencies correctly, get the default python version.
    # If the port declares dependencies incorrectly, this will be empty.
    DEFAULTVERSION=$(echo $DEPS | sed -E -n "s/^py([[:digit:]]{2})-$NAME$/\1/p")
    
    if [ -z "$DEFAULTVERSION" ]; then
        printf "%s declares dependencies even in the stub port\n" $STUBPORT
        DEPENDENCYPROBLEMS=$(($DEPENDENCYPROBLEMS + 1))
        PROBLEMPORTS="$PROBLEMPORTS $STUBPORT"
    else
        # Get the file of the default subport. If the default is set incorrectly, this will be empty.
        FILE="$(port file py$DEFAULTVERSION-$NAME 2>/dev/null)"
        if [ -z "$FILE" ]; then
            printf "%s declares default python version %s but does not offer python version %s\n" $STUBPORT $DEFAULTVERSION $DEFAULTVERSION
            DEFAULTVERSIONPROBLEMS=$(($DEFAULTVERSIONPROBLEMS + 1))
            PROBLEMPORTS="$PROBLEMPORTS $STUBPORT"
        fi
    fi
    if [ -n "$(grep -E '\$\{?prefix\}?/share/doc/\$\{?name\}?' "$STUBFILE")" ]; then
        printf "%s installs documentation that will conflict between subports\n" $STUBPORT
        DOCDIRPROBLEMS=$(($DOCDIRPROBLEMS + 1))
        PROBLEMPORTS="$PROBLEMPORTS $STUBPORT"
    fi
    if [ -z "$(grep -E 'livecheck\.type[[:space:]]+none' "$STUBFILE")" ]; then
        printf "%s enables livecheck even in the subports\n" $STUBPORT
        LIVECHECKPROBLEMS=$(($LIVECHECKPROBLEMS + 1))
        PROBLEMPORTS="$PROBLEMPORTS $STUBPORT"
    fi
done

TOTALPROBLEMS=$(($DEPENDENCYPROBLEMS + $DEFAULTVERSIONPROBLEMS + $DOCDIRPROBLEMS + $LIVECHECKPROBLEMS))

echo
printf "Out of %d ports using the unified python portgroup, there are %d problems.\n" $TOTALPYPORTS $TOTALPROBLEMS

if [ $DEPENDENCYPROBLEMS -gt 0 ]; then
    echo
    printf "%d ports have dependency problems. Dependencies should only be declared in subports.\n" $DEPENDENCYPROBLEMS
    echo "Enclose dependencies and all other \"real\" port actions in an \"if {\${name} != \${subport}}\" block."
fi

if [ $DEFAULTVERSIONPROBLEMS -gt 0 ]; then
    echo
    printf "%d ports have default python version problems. python.default_version needs to be\n" $DEFAULTVERSIONPROBLEMS
    echo "set to one of the values you set in python.versions."
fi

if [ $DOCDIRPROBLEMS -gt 0 ]; then
    echo
    printf "%d ports have documentation directory problems. In ports with subports, documentation\n" $DOCDIRPROBLEMS
    echo "should be installed into \${prefix}/share/doc/\${subport} rather than \${prefix}/share/doc/\${name}."
fi

if [ $LIVECHECKPROBLEMS -gt 0 ]; then
    echo
    printf "%d ports have livecheck problems. Livecheck should only be enabled in the stub port.\n" $LIVECHECKPROBLEMS
    echo "Livecheck directives should be enclosed in an \"if {\${name} == \${subport}}\" block;"
    echo "in the \"else\" part, set livecheck.type to none."
fi

if [ $TOTALPROBLEMS -gt 0 ]; then
    echo
    echo "The maintainers of these ports are:"
    echo
    port info --maintainers --index --line $PROBLEMPORTS | tr ',' '\n' | sort -u
fi
