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

prog=`basename $0 .bash`
PREFIX=/usr/local
ROLE=toxcore

#? broken in ansible

PYVER=3
P="BASE_PYTHON${PYVER}_MINOR"
PYTHON_MINOR="$(eval echo \$$P)"
[ -z "$PYTHON_MINOR" ] || PYTHON_MINOR=3.9
PYTHON_EXE_MSYS=$PREFIX/bin/python$PYVER.sh
PYTHON_EXE=$PYTHON_EXE_MSYS
DESC=""

PKG="ansible"
MOD="$PKG"

VER="2.9.22"
AVER="2.9.22"
DIR="${PKG}-$VER"
EXT="tar.gz"
URL="files.pythonhosted.org/packages/03/4f/cccab1ec2e0ecb05120184088e00404b38854809cf35aa76889406fbcbad/ansible-2.9.10.tar.gz"
TODIR=/o/data/TestForge/src/ansible

if [ -f /var/local/src/var_local_src.bash ] ; then
    . /var/local/src/var_local_src.bash
  else
     ols_are_we_connected () { route | grep -q ^default ; return $? ; }
fi

cd $PREFIX/src || exit 2
WD=$PWD

if [ "$#" -eq 0 ] ; then
    if [ !  -d "$DIR" ] ; then
	if [ ! -f "$HTTP_DIR/$URL" ] ; then
	    ols_are_we_connected || { DEBUG not connected ; exit 0 ; }
	    wget -xc -P "$HTTP_DIR" "https://$URL" || exit 2
	fi
	if [ "$EXT" = "zip" ] ; then
	    unzip "$HTTP_DIR/$URL" || exit 3
	else
	    tar xfvz "$HTTP_DIR/$URL" || exit 3
	fi
    fi

    cd "$DIR" || exit 4

    [ -f lib/ansible/parsing/utils/yaml.py.dst ] || \
      bash /usr/local/sbin/base_patch_from_diff.bash $ROLE \
        $TODIR/roles/$ROLE/overlay/Linux/$PREFIX/patches/$ROLE/$PWD || exit 6$?
    
    [ -d $PREFIX/$LIB/python$PYTHON_MINOR/site-packages/$DIR-py$PYTHON_MINOR.egg ] || \
	pip3.sh install . >> install.log 2>&1\
	|| { echo "ERROR: code $?" ; tail install.log ; exit  5 ; }

    "$PYTHON_EXE" -c "import $MOD" || exit 10


    grep -l '_tput\|_src' *sh ../bin*sh | \
	xargs grep -l 'echo \(INFO\|DEBUG\|ERROR\|DEBUG\):' | \
        xargs sed -e 's@echo \(INFO\|DEBUG\|ERROR\|DEBUG\):@\1 @'

    if [ -d  $PREFIX/src/ansible-$AVER/docs/docsite ] ; then
	cd $PREFIX/src/ansible-$AVER/docs/docsite
	[ -f htmldocs.log ] || make -n -f Makefile htmldocs > htmldocs.log 2>&1 || exit 2$?
	[ -f info.log ] || make -n -f Makefile.sphinx info > info.log 2>&1 || exit 3$?

    exit 0

elif [ "$1" = 'check' ] ; then
    "$PYTHON_EXE" -c "import $MOD" || exit 10
#    msys_run_checks_requirements
    
elif [ $1 = 'test' ] ; then
    cd $PREFIX/src/$DIR || exit 50
    $PYTHON_EXE_MSYS -m tox >> test.log 2>&1 || \
	{ echo "ERROR: $MOD code $?" ; cat test.log  ; exit 51 ; }
    
elif [ "$1" = 'refresh' ] ; then
    cd $PREFIX/src/$DIR || exit 60
    env PWD=$PREFIX/src/$DIR \
	/usr/local/sbin/base_diff_from_dst.bash $ROLE || exit 6$?
fi

exit 0
