#!/bin/bash
# -*- mode: sh; fill-column: 75; tab-width: 8; coding: utf-8-unix -*-

. /usr/local/bin/usr_local_tput.bash || exit 2
PREFIX=$PREFIX
ROLE=testforge

# too early
[ -f /usr/local/etc/testforge/testforge.bash ] || exit 0

. /usr/local/etc/testforge/testforge.bash || exit 1

[ -n "$TESTF_VAR_LOCAL" ] && PREFIX=$TESTF_VAR_LOCAL

if uname -a | grep entoo ; then
    GENTOO=0
else
    GENTOO=1
fi
UBUNTU=$( [ -d /etc/apt ] )

for PYTHON_MINOR in $BASE_PYTHON2_MINOR $BASE_PYTHON3_MINOR ; do
    [ -z "$PYTHON_MINOR" ] && continue
    # [ "$PYTHON_MINOR" = "$BASE_PYTHON2_MINOR" ] && PYMAJOR=2 || PYMAJOR=3
    PYVER="${PYTHON_MINOR:0:1}"

    cd $BASE_USR_LOCAL/$LIB/python${PYTHON_MINOR}/site-packages/ || exit $PYVER
    [ -f __init__.py ] || touch __init__.py
    INFO $PYVER GENTOO=$GENTOO $BASE_USR_LOCAL/$LIB/python${PYTHON_MINOR}/site-packages/
    ls -1d * | \
      grep -v '__init__.py\|~$\|egg-info\|__pycache__\|egg-link\|dist-info\|pyc$\|pyo$\|pth$\|.sh$$\|.so$\|.egg$\|.tar$\|.log$\|.lis$\|.err$' | \
      sed -e 's/\.py$//' | \
    while read elt ; do
        [ -z "$elt" ] && continue
        [ $elt = cachecontrol ] && mod=CacheControl || mod=$elt

	#exceptions
        [ $elt = pip ] && echo "INFO: $PYTHON_MINOR Skipped OK - $elt" && continue
        [ $elt = ansible ] && echo "INFO: $PYTHON_MINOR Skipped OK - $elt" && continue
	#?FixMe: - we are now allowing site.py
        [ $elt = site ] && echo "INFO: $PYTHON_MINOR Skipped OK - $elt" && continue
	# broken for MarkupSafe-1.1.1-py2.7.egg-info
        [ $elt = markupsafe ] && echo "INFO: $PYTHON_MINOR Skipped OK - $elt" && continue
	# broken for PyYAML-5.3.1-py2.7.egg-info
        [ $elt = yaml ] && echo "INFO: $PYTHON_MINOR Skipped OK - $elt" && continue
	# Pygments-2.5.2-py2.7.egg-info
	[ $elt = pygments ] && echo "INFO: $PYTHON_MINOR Skipped OK - $elt" && continue

	# FixMe: what about the selenium patches
	[ $elt = selenium ] && continue

	# FixMe:
        if [ $GENTOO -eq 0 ] && eix -r ^dev-python/${elt}$ | grep "Installed.*[\" ]$PYTHON_MINOR" ; then
	    DBUG $PYVER $elt is Installed
        elif [ $GENTOO -eq 0 ] && eix ^dev-python/py${elt}$ | grep "Installed.*[\" ]$PYTHON_MINOR" ; then
    	    DBUG $PYVER py$elt is Installed
        elif [ -e /usr/$LIB/python$PYTHON_MINOR/$elt ] ; then
	    [ $elt = sitecustomize ] && echo WHY?: in /usr/$LIB/python$PYTHON_MINOR/$elt && continue
    	    echo "DEBUG: $PYVER $elt is in /usr/$LIB/python$PYTHON_MINOR/$elt"
	# The python$PYVER -s is crucial - otherwise
	# /root/.local/lib64/python2.7/site-packages precedes
	# /usr/lib64/python2.7/site-packageson sys.path
        elif python$PYVER -s -c "import $mod,os; print os.path.realpath($mod.__path__[0])" 2>/dev/null | grep /usr/$LIB/ ; then
    	    echo "DEBUG: $PYVER $elt is in /usr/$LIB/"
        else
            echo "INFO: $PYTHON_MINOR Checked OK - $elt"
    	    continue
          fi

        WARN "$PYTHON_MINOR deleting - $elt"

        if [ -d $elt ] ; then
    	    INFO rm -rf *${elt}* .*${elt}*
    	    rm -rf *${elt}* .*${elt}*
        elif file $elt | grep 'empty' ; then
    	    INFO rm -rf ${elt}
    	    rm -rf ${elt}
        elif [ -f "$elt.py" ] ; then
    	    INFO rm ${elt}.py*
    	    rm ${elt}.py*
        elif [ -f "$elt.pyo" ] || [ -f "$elt.pyc" ] ; then
    	    INFO rm -f ${elt}.pyc ${elt}.pyo
    	    rm -f ${elt}.pyc ${elt}.pyo
        else
    	    echo "ERROR: oddball not a dir or file $( file $elt ) - \"$elt\""
          fi
      done
    # FixMe:  these are missed and crucial
    [ -f /usr/local/lib64/python$PYTHON_MINOR7/site-packages/pkg_resources/__init__.py -a \
	 -f /usr/lib64/python$PYTHON_MINOR/site-packages/pkg_resources/__init__.py ] &&
	rm -rf /usr/local/lib64/python$PYTHON_MINOR/site-packages/pkg_resources/
    /usr/local/bin/python$PYVER.sh -c 'from pkg_resources import ensure_directory, ContextualZipFile' || \
    	    WARN "from pkg_resources import ensure_directory, ContextualZipFile "
done

cd /usr/local/bin
for file in * ; do
    [ -x $file ] || continue
    [ -e /usr/bin/$file -o -e /usr/sbin/$file -o -e /usr/bin/$file.py ] || continue
    # ls -l /usr/bin/$file $file
    root=$( basename $file .py )
    [[ $file =~ .*2.py$ ]] && DBUG $file && continue
    [[ $file =~ .*2$ ]] && DBUG $file && continue
    if file $file | grep -q 'Python script' && head -2 $file | grep -q '/python2' ; then
	[[ $file =~ .*.py$ ]] && INFO mv $file ${root}2.py && mv $file ${root}2.py && continue
	[ -e /usr/bin/$file.py ] && INFO mv $file ${file}2 && mv $file ${file}2 &&z \
	    INFO ln -s /usr/bin/$file.py $file && ln -s /usr/bin/$file.py $file && continue
	WARN $file not python ; continue
      fi
    INFO mv $file ${file}2; mv $file ${file}2
done

exit 0
