#!/bin/bash

# A hackaround for test-module and pdb and ansible 2.x
#
# Usage:
#   test-module-v2 -m <modulepath> -a @<argsfile>
#
# Notes:
#   * space delimited args are not functional, use an args file with @ instead
#   * pdb can be used in module code with this script

CWD=$(pwd)

SCRATCH=/tmp/test_module_scratch
if [ -d $SCRATCH ]; then
    rm -rf $SCRATCH
fi
mkdir -p $SCRATCH

# Where is test-module ?
which test-module
RC=$?
if [[ $RC != 0 ]]; then
    TEST_MODULE=$(which ansible | xargs dirname | xargs dirname)
    TEST_MODULE="$TEST_MODULE/hacking/test-module"
else
    TEST_MODULE=$(which test-module)    
fi


ARGS=( "$@" )
MODULE_FILE=$2

if [ ! -f $MODULE_FILE ]; then

    # check the ansible modules dir for the file
    ANSIBLE_DIR=$(which ansible | xargs dirname | xargs dirname)
    MODULE_DIR="$ANSIBLE_DIR/lib/ansible/modules"    
    echo "MODULE_DIR: $MODULE_DIR"
    FOUND_FILE=$(find -L $MODULE_DIR -type f -name "$MODULE_FILE")
    echo "FOUND FILE: $FOUND_FILE"
    ARGS[1]=$FOUND_FILE
fi

echo "MODULE_FILE: ${ARGS[2]}"
echo "ARGS: ${ARGS[@]}"

# Render/Join/Zip the module
echo "#####################################"
echo "#  Calling test-module ..." 
echo "#####################################"
$TEST_MODULE -o $SCRATCH/zipped_module.py -n ${ARGS[@]}
echo "Done."

cd $SCRATCH

# Unzip the module
echo "#####################################"
echo "#  Exploding zipped module ..." 
echo "#####################################"
python zipped_module.py explode
echo "Done."

# Get the args
echo "#####################################"
echo "#  Extracting parameter args ..." 
echo "#####################################"
JSONARGS=$(fgrep -m1 ANSIBALLZ_PARAMS zipped_module.py | sed -e 's/^[[:space:]]*//')
if [ ! -z "$JSONARGS" ]; then 
    #echo $JSONARGS
    python -c "import os; import json; $JSONARGS; ARGS=json.loads(ANSIBALLZ_PARAMS); print ARGS; open('args.json', 'wb').write(json.dumps(ARGS, indent=2))"
    echo "Done."

    # Overwrite _load_params to not use stdin (thereby allowing pdb to function)
    echo "#####################################"
    echo "# Patching _load_params ..." 
    echo "#####################################"
    if [ ! -d debug_dir ]; then
        sed -i.bak 's/sys.stdin.read()/open("args\.json",\ "rb")\.read()/' ansible/module_utils/basic.py
        sed -i.bak 's/sys.stdin.buffer.read()/open("args\.json",\ "rb")\.read()/' ansible/module_utils/basic.py
    else
        sed -i.bak 's/sys.stdin.read()/open("args\.json",\ "rb")\.read()/' debug_dir/ansible/module_utils/basic.py
        sed -i.bak 's/sys.stdin.buffer.read()/open("args\.json",\ "rb")\.read()/' debug_dir/ansible/module_utils/basic.py
    fi    
    echo "Done."
fi

# Run the extracted module with args
echo "#####################################"
echo "# Running the module code now ..."
echo "#####################################"
mkdir strace.out
#strace -fftttvo strace.out/test python ansible_module_*.py
if [ ! -d debug_dir ]; then
    echo "# $(which python) ansible_module_*.py"
    python ansible_module_*.py
else
    echo "# cd debug_dir ; ln -s ../args.json args.json; python ansible_module_*.py"
    cd debug_dir
    ln -s ../args.json args.json
    echo "# $(which python) ansible_module_*.py"
    python ansible_module_*.py
fi
echo "Done."
