#!/bin/bash

current_directory=`pwd -P`
cd `dirname $0`
naith_directory=`pwd -P`
cd $current_directory

raw_code_coverage_file=$current_directory/coverage_raw.txt
raw_tests_report_file=$current_directory/tests_report_raw.txt
junit_report_file=$current_directory/junit.xml

action_name=$1
tests_folder=$2

if [ "$tests_folder" == "" ]
then
    tests_folder="$current_directory/tests"
fi

if [ "$action_name" == "help" ]
then
    echo "naith 1.1.0"
    exit 0
fi

if [ "$action_name" == "run" ]
then
    cd $tests_folder
    if [ "${?}" -ne "0" ]
    then
        echo ""
        echo "Cannot find tests folder: $tests_folder"
        echo ""
        exit 1
    fi
    
    default_options="--tests_report_path \"$raw_tests_report_file\" --coverage_file_path \"$raw_code_coverage_file\" --base_directory \"$current_directory\""
    
    if [ -f "$current_directory/_before_test.php" ]
    then
        default_options="$default_options --prepend_file \"$current_directory/_before_test.php\""
    fi

    echo ""
    echo " Running Tests "
    echo "==============="
    echo ""
    echo "" > $raw_code_coverage_file
    echo "" > $raw_tests_report_file

    test_success=0
    test_errors=0
    # We cannot use while read line here
    # @see http://fvue.nl/wiki/Bash:_Piped_%60while-read'_loop_starts_subshell
    # for further information 
    for file in `ls *.php`
    do
        php $naith_directory/naith.php run-test $default_options \
            --test_file "$tests_folder/$file"
            
        current_exit_code="${?}"
        if [ "${current_exit_code}" -ne "0" ]
        then
            echo "  [  ] $file"
            echo "   -> broken! (Exit code: $current_exit_code)"
            let test_errors=test_errors+1
        else
            echo "  [OK] $file"
            let test_success=test_success+1
        fi
    done
    

    php $naith_directory/naith.php generate-junit-xml $default_options \
        --junit_xml_path "$junit_report_file"
    
    rm "$raw_tests_report_file"
    
    if [ ! $test_errors -eq 0 ]
    then
        rm "$raw_code_coverage_file"
        exit 1
    fi
    
    php $naith_directory/naith.php make-coverage-overview $default_options \
        --minimum_code_coverage 100 \
        --excluded_path "$tests_folder" \
        --excluded_path "$naith_directory/" \
        --excluded_path "$current_directory/vendor/" \
        --excluded_path "$current_directory/_before_naith.php"
        
    current_exit_code="${?}"
    if [ "${current_exit_code}" -ne "0" ]
    then
        php $naith_directory/naith.php make-untested-code-overview $default_options \
            --minimum_code_coverage 100 \
            --excluded_path "$tests_folder" \
            --excluded_path "$naith_directory/" \
            --excluded_path "$current_directory/vendor/" \
            --excluded_path "$current_directory/_before_naith.php"
            
        echo ""
        rm "$raw_code_coverage_file"
        exit 1
    else
        echo ""
        echo "Everything is tested. Awesome!"
    fi
    
        
    echo ""
    rm "$raw_code_coverage_file"
    exit 0
fi


if [ "$action_name" == "run-constant" ]
then
    wait_for_command=""

    # disabled, because it does not work reliable and recursive :(   
    #    if [ `which node` ]
    #    then
    #        wait_for_command="node -e \"require('fs').watch('.', function (event, filename) { console.log(arguments); process.exit(0); });\""
    #    fi

    # macosx (get with: port install wait_on)
    if [ `which wait_on` ]
    then
        wait_for_command="wait_on -w ."
    fi
    
    # linux (get e.g. with: apt-get install inotify-tools)
    if [ `which inotifywait` ]
    then
        wait_for_command="inotifywait --quiet -r -e close_write ."
    fi
    
    # ok, we can't get anything .. let's just make it every 30 seconds
    if [ "$wait_for_command" == "" ]
    then
        wait_for_command="sleep 30"
    fi
    
    echo " "
    echo " Using $wait_for_command to poll for file changes ..."
    echo " "
    
    while [ 1 ]
    do
        cd $current_directory
        $naith_directory/naith run
        sleep 1
        echo "Waiting ...."
        bash -c "$wait_for_command"
    done
    
    exit 0
fi

echo "Unsupported action: '$action_name', use 'naith run' or 'naith run-constant'!"
exit 1
