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

[ ! -f /usr/local/etc/testforge/testforge.bash ] || \
  . /usr/local/etc/testforge/testforge.bash || exit 1
. /usr/local/bin/usr_local_tput.bash || exit 2

PYVER=3
PYTHON_MINOR=3.11 # $( python$PYVER --version 2>&1| sed -e 's@^.* @@' -e 's@\.[0-9]*$@@' )
#sudo chmod 1777 /tmp/.ansible /tmp/.ansible/tmp

PYTHON_EXE=/usr/local/bin/python${PYVER}.sh
# old 2 jinja2
export PYTHONPATH=/usr/local/lib/python3.11/site-packages

export TESTFORGE_ANSIBLE_DIR=$PWD
export TMPDIR=/mnt/tmp/Pentoo
export TMP=/mnt/tmp/Pentoo
# subtle problem with MFLAGS in a calling make getting picked up by an inferior make
export MFLAGS=""
WD=$PWD

skip=
tags='untagged'
check=0
connection=
verbose=1
debug=
diff=0
become=-1
limit=''
inventory=hosts.yml
step=0
timeout=60
gather_timeout=120
start=""
list_tags=0
list_tasks=0
# unused
user=""
group=""
home=""
also=""
#
verbose=2
limit=$1
roles="proxy ansible-gentoo_install toxcore"
[ -z "$limit" ] && BOX_HOST=localhost || BOX_HOST=$limit
limit=$BOX_HOST

. $PWD/ansible_local.args

# --flush-cache
if [ $verbose -eq 1 ] ; then
    LARGS="-v"
elif [ $verbose -eq 2 ] ; then
    LARGS="-vv"
elif [ $verbose -eq 3 ] ; then
    LARGS="-vvv"
elif [ $verbose -eq 4 ] ; then
    LARGS="-vvvv"
else
    LARGS=""    
fi
# https://git-scm.com/book/en/v2/Git-Internals-Environment-Variables
# https://github.blog/2018-02-01-crypto-removal-notice/
# https://stackoverflow.com/questions/48938019/git-pull-push-unable-to-access-https-ssl-routines-seem-to-be-down
if [ $verbose -ge 3 ] ; then
    export GIT_TRACE=1
  fi
if [ $verbose -ge 4 ] ; then
    export GIT_CURL_VERBOSE=1
  fi

export ANSIBLE_CONFIG=/o/var/local/src/play_tox/ansible.cfg

#? ANSIBLE_TRANSPORT=chroot PYTHONUNBUFFERED=1
if [ -z "$debug" ] ; then
    EXE="$PYTHON_EXE /usr/local/bin/ansible-playbook"    
  elif [ $debug = debug -o $debug = ansible ] ; then
    export ANSIBLE_KEEP_REMOTE_FILES=1
    export ANSIBLE_STRATEGY=debug
    EXE="$PYTHON_EXE /usr/local/bin/ansible-playbook"
  elif [ $debug = trepan ] ; then
    export ANSIBLE_KEEP_REMOTE_FILES=1
    EXE="/var/local/bin/trepan${PYVER}2.bash /usr/local/bin/ansible-playbook"
  elif [ $debug = trepan2c ] ; then
    export ANSIBLE_KEEP_REMOTE_FILES=1
    EXE="/var/local/bin/trepan${PYVER}c2.bash /usr/local/bin/ansible-playbook"
  # coverage
  elif [ $debug != 0 ] ; then
    export ANSIBLE_KEEP_REMOTE_FILES=1
    export _ANSIBLE_TREPAN_PORT=$debug
    EXE="$PYTHON_EXE /usr/local/bin/ansible-playbook"
  fi

LARGS="$LARGS -c $connection "
if [ $become -eq -1 ] ; then
    [ $connection = chroot ] && become=0 || become=1
fi
[ $become -eq 0 ] || LARGS="$LARGS -b"
[ $check -eq 0 ] || LARGS="$LARGS --check"
[ $diff -eq 0 ] || LARGS="$LARGS -D"
[ $step -eq 0 ] || LARGS="$LARGS --step"
[ $list_tags -eq 0 ] || LARGS="$LARGS --list-tags"
[ $list_tasks -eq 0 ] || LARGS="$LARGS --list-tasks"
[ -n "$skip" ] && LARGS="$LARGS --skip-tags=$skip"
[ -n "$tags" ] && LARGS="$LARGS --tags=$tags"
[ -n "$start" ] && LARGS="$LARGS -start-at-task=\"$start\""
[ -n "$inventory" ] && LARGS="$LARGS --inventory=$inventory"
[ -n "$limit" ] && LARGS="$LARGS --limit=$limit"
#  --flush-cache
df | grep -q /home$ && [ -n "$RUN_QERC_USERFILE" ] && [ -f "$RUN_QERC_USERFILE" ] && \
LARGS="$LARGS -e $RUN_QERC_USERFILE" && ls -l "$RUN_QERC_USERFILE"
#    LARGS="$LARGS --ask-vault-pass" && ls -l "$RUN_QERC_USERFILE"

log_name=`echo $roles|sed -e 's/ /_/g'`
[ -z "$log_name" ] && log_name=ansible
[ "$tags" = untagged ]  | log_name=${log_name}-${tags}
date=`date +%Y/%m/%d`
log_relpath=var/tmp/$date/$BOX_HOST/$log_name.log
grep -q "log_path = ${log_relpath}@"  ansible.cfg || \
    sed -e "s@^log_path = .*@log_path = ${log_relpath}@" -i ansible.cfg
ANSIBLE_RUN_LOG=$PWD/$log_relpath

EXTRA_VARS="{\"ANSIBLE_RUN_LOG\": $ANSIBLE_RUN_LOG"

if [ -d "$PLAY_PIP_CACHE" ] ; then
    EXTRA_VARS="$EXTRA_VARS, \"PLAY_PIP_CACHE\": \"$PLAY_PIP_CACHE\""    
fi
if [ -f "$PLAY_CA_CERT" ] ; then
    EXTRA_VARS="$EXTRA_VARS, \"PLAY_CA_CERT\": \"$PLAY_CA_CERT\""    
fi
EXTRA_VARS="$EXTRA_VARS, \"TESTF_ANSIBLE_SRC\": \"$PWD\""

# maybe dynamically determine this - but some of these conflict - e.g. qemu and lxc
#HOSTVMS_FEATURES="['vagrant', 'virtualbox', 'lxc']" # 'packer', 'qemu', 'libvirt', 'docker'
#EXTRA_VARS="$EXTRA_VARS, \"HOSTVMS_FEATURES\": \"$HOSTVMS_FEATURES\""

EXTRA_VARS="$EXTRA_VARS, \"BOX_HOST\": \"$BOX_HOST\""
# TESTF_QERC_USERFILE - NOT $HOME in case this is run as root - let it default
[ -z "$RUN_QERC_USERFILE" ] && [ -f "$RUN_QERC_USERFILE" ] && \
    EXTRA_VARS="$EXTRA_VARS, \"RUN_QERC_USERFILE\": \"$RUN_QERC_USERFILE\"" # may not exist

# EXTRA_VARS="$EXTRA_VARS, \"INCD_SKEL_USERS_LIST\": [\"$BOX_USER_NAME\", \"mike\"]"

EXTRA_VARS="$EXTRA_VARS, \"ROLES\": [\"base\""
for role in $roles ; do
    [ "$role" = "base" ] && continue
    EXTRA_VARS="$EXTRA_VARS, \"$role\""
  done
EXTRA_VARS="$EXTRA_VARS]}"


if false && [ $check -eq 0 ] && [ $list_tags -eq 0 ] && [ $list_tasks -eq 0 ] && [ "$tags" = "untagged"  ] && \
       ! wget -O /dev/null --no-check-certificate http://google.com ; then
    WARN "We cant reach the Internet - reauthenticate with the proxy?"
  fi

[ -f hosts.yml ] || cat > hosts.yml << EOF
# -*- mode: yaml; tab-width: 0; coding: utf-8 -*-

---

all:
  children:    
    localgroup:
      hosts:
        localhost:
      vars:
        ansible_connection: "local"
EOF
yamllint -c .yamllint.rc -f standard hosts.yml || {
    ERROR yamllint -c .yamllint.rc -f standard hosts.yml
    exit 5
}
[ -f ansible.cfg ] || cat > ansible.cfg << EOF
[defaults]
stdout_callback: yaml
log_path = ./var/tmp/2019/10/03/testforge_pydev_.pydev.log
# callback_plugins = $WD/lib/plugins/
# /i/data/DevOps/net/Http/docs.ansible.com/ansible/intro_configuration.html
# http://docs.ansible.com/ansible/intro_configuration.html#command-warnings
callback_whitelist = timer
command_warnings = False
retry_files_enabled = False
deprecation_warnings = False
display_args_to_stdout = False
error_on_undefined_vars = True
force_color = False
forks = 5
#   Ansible by default will override variables in specific precedence orders, as described in Variables.
#   When a variable of higher precedence wins, it will replace the other value.
#?! hash_behaviour = merged
#! fatal: [localhost]: FAILED! => {"changed": false, "cmd": "/bin/lsblk --list --noheadings --paths --output NAME,UUID --exclude 2", "msg": "Timer expired after 30 seconds", "rc": 257}
gather_timeout = 120
internal_poll_interval=0.1
#   This sets the interval (in seconds) of Ansible internal processes polling each other. Lower values
#   improve performance with large playbooks at the expense of extra CPU load. Higher values are more
#   suitable for Ansible usage in automation scenarios, when UI responsiveness is not required but CPU usage
#   might be a concern. Default corresponds to the value hardcoded in  2.1:
# Fixme: should be per user
local_tmp = /var/tmp
# library = /usr/share/ansible
library = $WD/library

nocows = 0
roles_path = $WD/roles
handler_includes_static = True
timeout = 60
EOF

# Fixme: should be per user
# local_tmp = /var/tmp

log_dir="`dirname $log_relpath`"
if [ -d $log_dir ] ; then
    cp /dev/null $log_relpath
  else
    mkdir -p $log_dir
  fi
ls $log_dir >/dev/null || { ERROR ls $log_dir ; exit 3 ; }
touch $log_relpath || { ERROR $prog  $log_relpat not wrieaable; exit 4 ; }
ls -l $log_relpath || exit 5

#?[ -x /usr/local/bin/clean_path.bash ] && N="`/usr/local/bin/clean_path.bash`" || N=$PATH
# echo "INFO: PATH=$PATH"
# `grep log_path ansible.cfg`

if [ -z "$http_proxy" ] ; then
    DBUG make sure http_proxy and no_proxy are set if you need them
    . /usr/local/bin/proxy_export.bash
else
    DBUG "http_proxy=$http_proxy CORP_NTLM_PROXY=$CORP_NTLM_PROXY"
  fi

# [ -z "${DISPLAY}" ] && EXE="xvfb-run ${EXE}"

INFO "$EXE $LARGS --extra-vars \'$EXTRA_VARS\' ansible_local.yml"
DBUG PYTHON_MINOR=$PYTHON_MINOR PYTHONPATH=$PYTHONPATH

if ! ps ax | grep -v grep | grep -q ansible-playbook ; then
    # clean the /var/tmp directory
    rm -rf /var/tmp/ansible-*
  fi
#? bash --norc $PYTHON_EXE

yamllint -c .yamllint.rc -f standard ansible_local.yml || {
    ERROR yamllint -c .yamllint.rc -f ansible_local.yml
    exit 8
}
# strace -o /tmp/check.str
exec env $ANSIBLE_ENV $EXE $LARGS \
     --extra-vars "$EXTRA_VARS" \
     ansible_local.yml
