#! /bin/bash
MYNAME=$(basename "$0")
MYDIR=$(dirname "$0")
MYDIR=$(readlink -m "$MYDIR")
VERSION="0.0.9"
VERBOSE=""
LOGFILE="$HOME/$MYNAME.log"
EXITCODE=0

COMMAND_LINE_OPTIONS="$0 $*"
OPTION_MAIL_LIST=""
OPTION_TESTS=""

#
# The name of the configuration.
#
#OPTION_CONFIG_NAME="s9s-test-release"
OPTION_CONFIG_NAME="s9s-tests"

#
# This is where the XML and HTML files are placed before we upload them to the
# Jenkins server.
#
OPTION_OUTPUT_DIR="${PROJECT_CC_WORKER_DIR}"
OPTION_OUTPUT_DIR+="tests/s9s_test"

MAIL_LIST_FILE=""

REPORT_FILE=""

today=$(date "+%Y-%m-%d" 2>/dev/null)
YESTERDAY=$(date "+%Y-%m-%d" -d "yesterday" 2>/dev/null)

ymd=$(echo "$today" | tr '-' '_')
gzfile="${ymd}.tar.gz"

N_TESTS_PASSED=0
N_TESTS_FAILED=0
N_MAILS_SENT=0

REPORT_FILE="${ymd}.text"
OPTION_NO_HTML=""
OPTION_UPLOAD=""

source $MYDIR/utilityfunctions.sh

# Some information:
# https://stackoverflow.com/questions/4922867/what-is-the-junit-xml-format-specification-that-hudson-supports

function printHelpAndExit()
{
cat <<EOF

Usage:
  $MYNAME [OPTION]... [TEST_NAME]...

  $MYNAME - Downloads test results from my server. :)

  -h, --help              Print this help and exit.
  -v, --version           Print version information and exit.
  --verbose               Print more messages.
  --log-file=FILE         Store all the messages in the given file too.

  --mail-list=NAME        The name of the mail list.

  --no-html               Do not include html test report.
  --upload                Upload the files to Jenkins.

EXAMPLES:
  PASSWORD="SSSSSSS" pip-download --verbose --upload

EOF

  exit 0
}

ARGS=$(\
    getopt \
        -o hvj: \
        -l "help,verbose,version,log-file:,no-html,upload,mail-list:" \
        -- "$@")

if [ $? -ne 0 ]; then
    exit 6
fi

eval set -- "$ARGS"
while true; do
    case "$1" in
        -h|--help)
            shift
            printHelpAndExit
            ;;

        --verbose)
            shift
            VERBOSE="true"
            ;;

        -v|--version)
            shift
            printVersionAndExit
            ;;

        --log-file)
            shift
            LOGFILE=$(readlink -m "$1")
            shift
            ;;

        --mail-list)
            OPTION_MAIL_LIST="$2"
            shift 2
            ;;

        --no-html)
            OPTION_NO_HTML="true"
            shift
            ;;

        --upload)
            OPTION_UPLOAD="true"
            shift
            ;;

        --)
            shift
            break
            ;;

        *)
            printError "Unhandled option $1"
            exit 1
            ;;
    esac
done

OPTION_TESTS="$*"


function print_header()
{
    cat <<EOF
                       TEST_NAME STATUS     COMMITTED                 ELAPSED  GIT HASH
=======================================================================================================================
EOF
}

function print_footer()
{
    cat <<EOF
=======================================================================================================================

Summary:
     passed: $N_TESTS_PASSED test(s)
     failed: $N_TESTS_FAILED test(s)

EOF
}

function get_field_from_html()
{
    local html_file=""
    local field_name=""
    local value=""
    local fields=""

    while [ -n "$1" ]; do
        case "$1" in 
            --html-file)
                html_file=$2
                shift 2
                ;;

            *)
                break
                ;;
        esac
    done

    field_name="$1"
    fields=$(cat $html_file | grep "$field_name")
    fields=$(echo "$fields" | sed -e 's/<!--//g' -e 's/-->//g');
   
    # The value is the third column. Some older code used 3 or 4 columns, here
    # is a support for both.
    value=$(echo "$fields" | awk -F' ' '{print $4}')
    if [ -z "$value" ]; then
        value=$(echo "$fields" | awk -F' ' '{print $3}')
    fi

    echo "$value"
}

function download_html_file()
{
    local url
    local server="www"

    [ ! -d "$ymd" ] && mkdir "$ymd"

    test_name="$1"
    html_file="$ymd/$(basename $test_name .sh).html"

    url="https://$server/ft_install/"
    url+="?page=test&test_name=$test_name&full_screen=true"

    #
    # We download the test results from the web, this way the reports are nicely
    # formatted for easy use.
    #
    wget \
        --user=$WWW_USER \
        --password="$WWW_PASSWORD" \
        --no-check-certificate \
        "$url" \
        --output-document=$html_file 2>/dev/null

    if [ $? -ne 0 ]; then 
        printError "Failed to download $url..."
        return 1
    fi

    echo "$html_file"
}

function cut_test_case_html()
{
    local html_file
    local output_file
    local test_name

    while [ -n "$1" ]; do
        case "$1" in 
            --html-file)
                html_file="$2"
                shift 2
                ;;

            --output-file)
                output_file="$2"
                shift 2
                ;;

            --test-name)
                test_name="$2"
                shift 2
                ;;

            *)
                printError "process_html_file: Invalid option '$1'."
                return 1
                ;;
        esac
    done

    [ -z "$html_file" ] && return 1
    [ -z "$output_file" ] && return 1
    [ -z "$test_name" ] && return 1

    # These mark the begin and the end of the output for one particular test
    # case. We can now split this part out of the html file that holds the
    # output of the whole test script.
    if grep -q "^p42dc 200" "$html_file"; then
        awk_command="/p42dc 200 $test_name\$/,/p42dc 201 $test_name\$/"
    else
        awk_command="/p42dc 200 $test_name /,/p42dc 201 $test_name /"
    fi

    awk "$awk_command" "$html_file" >"$output_file"
}

function get_test_class()
{
    local html_file
    local git_branch
    
    while [ -n "$1" ]; do
        case "$1" in 
            --html-file)
                html_file=$2
                shift 2
                ;;

            *)
                printError "get_test_class: Invalid option '$1'."
                return 1
                ;;
        esac
    done

    git_branch=$(get_field_from_html --html-file $html_file "p42di 131")
    #git_branch=$(basename $git_branch)
    if [ -z "$git_branch" ]; then
        echo "s9s"
    else
        echo "s9s_$git_branch"
    fi
}

function get_test_names_html()
{
    local html_file
    local oldifs

    while [ -n "$1" ]; do
        case "$1" in 
            --html-file)
                html_file=$2
                shift 2
                ;;

            *)
                printError "process_html_file: Invalid option '$1'."
                return 1
                ;;
        esac
    done

    [ -z "$html_file" ] && return 1
    [ ! -f "$html_file" ] && return 1
    
    oldifs="$IFS"
    IFS=$'\n'
    for caseline in $(cat "$html_file" | grep "p42dc 200 "); do
        my_name=$(echo "$caseline" | sed -e 's/<!--//g' -e 's/-->//g' | awk -F' ' '{print $3}')
        printf "$my_name\n"
    done
    IFS="$oldifs"
}

function get_test_case_time()
{
    local html_file

    while [ -n "$1" ]; do
        case "$1" in 
            --html-file)
                html_file="$2"
                shift 2
                ;;

            *)
                printError "process_html_file: Invalid option '$1'."
                return 1
                ;;
        esac
    done
        
    my_time=$(get_field_from_html --html-file "$html_file" "p42dc 300")

    echo "$my_time"
}

function get_test_status()
{
    local html_file
    local oldifs

    while [ -n "$1" ]; do
        case "$1" in 
            --html-file)
                html_file="$2"
                shift 2
                ;;

            *)
                printError "process_html_file: Invalid option '$1'."
                return 1
                ;;
        esac
    done

    echo $(get_field_from_html --html-file "$html_file" "p42di 110")
}

function get_test_case_title()
{
    local html_file
    local my_title

    while [ -n "$1" ]; do
        case "$1" in 
            --html-file)
                html_file="$2"
                shift 2
                ;;

            *)
                printError "process_html_file: Invalid option '$1'."
                return 1
                ;;
        esac
    done
        
    my_title=$(cat "$html_file" | \
        grep '^<h3>' | head -n 1 | \
        sed -e 's#<h3>##g' -e 's#</h3>##g')

    echo "$my_title"
}


function emit_test_failures()
{
    local html_file
    local oldifs

    while [ -n "$1" ]; do
        case "$1" in 
            --html-file)
                html_file="$2"
                shift 2
                ;;

            *)
                printError "process_html_file: Invalid option '$1'."
                return 1
                ;;
        esac
    done

    oldifs="$IFS"
    IFS=$'\n'
    for caseline in $(cat "$html_file" | grep "p42err"); do
        error_message=$(\
            echo "$caseline" | \
                sed -e 's/p42err //g' \
                    -e 's/&/\&amp;/g' \
                    -e 's/</\&lt;/g' \
                    -e 's/>/\&gt;/g' \
                    | tr -d '"')

        if [ -z "$error_message" ]; then
            continue
        fi

        printf "        <failure message=\"$error_message\"></failure>\n"
    done
    IFS="$oldifs"

}

function emit_test_output()
{
    local html_file
    local report_file
    local oldifs

    while [ -n "$1" ]; do
        case "$1" in 
            --report-file)
                # The original file with the entire report.
                report_file="$2"
                shift 2
                ;;

            --html-file)
                html_file="$2"
                shift 2
                ;;

            *)
                printError "process_html_file: Invalid option '$1'."
                return 1
                ;;
        esac
    done

    if [ -z "$OPTION_NO_HTML" ]; then
        echo "        <system-out>"
        echo "<![CDATA["
        cat "$html_file" | grep -v "p42dc"
        echo "]]>"
        echo "        </system-out>"
    else
        echo "        <system-out></system-out>"
    fi

    #
    # The extended data about the test.
    #
    test_status=$(get_test_status --html-file "$report_file")
    git_branch=$(get_field_from_html --html-file "$report_file" "p42di 131")
    git_hash=$(get_field_from_html --html-file "$report_file" "p42di 130")
    git_date=$(get_field_from_html --html-file $report_file "p42di 120")
    git_time=$(get_field_from_html --html-file $report_file "p42di 121")
    test_hostname=$(get_field_from_html --html-file $report_file "p42di 150")
  
    return 0
    cat <<EOF
            <system-err>
    <![CDATA[
    <h3>Debug information</h3>
    <p>Here is some debug information about this test run created by the 
    pip-download script</p>
    <pre>
     test_status: $test_status
      git_branch: $git_branch
        git_hash: $git_hash
        git_time: $git_date $git_time
     test_server: $test_hostname
    </pre>
EOF
    
    awk_command="/p42lg 200/,/p42lg 201/"
    awk "$awk_command" "$report_file" 
    
    cat <<EOF
    ]]>
            </system-err>
EOF
}

function emit_test_case_xml()
{
    local html_file
    local oldifs
    local test_name
    local test_time
    local test_title
    local test_suite
    local report_file

    while [ -n "$1" ]; do
        case "$1" in 
            --report-file)
                # The original file with the entire report.
                report_file="$2"
                shift 2
                ;;

            --html-file)
                # The html file with the slice of the html document.
                html_file="$2"
                shift 2
                ;;

            --test-name)
                test_name="$2"
                shift 2
                ;;

            --test-suite)
                test_suite="$2"
                shift 2
                ;;

            *)
                printError "process_html_file: Invalid option '$1'."
                return 1
                ;;
        esac
    done

    test_time=$(get_test_case_time --html-file "$html_file")
    test_title=$(get_test_case_title --html-file "$html_file")

    class_name=$(get_test_class --html-file "$report_file")
    class_name="${class_name}.${test_suite}"
   
    is_skipped=""
    if grep -q 'p42dc 101' "$html_file"; then
        is_skipped="true"
    fi

    if [ -z "$test_title" ]; then
        printWarning "No title for $test_name in $test_suite."
        printWarning "Original report file is $report_file"
        test_title="Untitled Test"
    fi
    
    if [ -z "$test_time" ]; then
        printWarning "No time for $test_name in $test_suite."
        printWarning "Original report file is $report_file"
        test_time=0
    fi

    #
    # Emitting the test case.
    #
    cat <<EOF

    <!-- 
         slice_file: $html_file 
          test_name: $test_name
    -->
    <testcase 
            classname="$class_name" 
            name="$test_title"
            time="$test_time">
EOF
    [ -n "$is_skipped" ] && echo "        <skipped/>"

    emit_test_failures --html-file "$html_file"
    emit_test_output --html-file "$html_file" --report-file "$report_file"

    cat <<EOF
    </testcase>
EOF
}

function process_html_file_xml()
{
    local test_name
    local html_file
    local git_date
    local git_time
    local git_timezone
    local elapsed
    local html_file
    local n_skipped
    local n_failures
    local n_cases

    while [ -n "$1" ]; do
        case "$1" in 
            --html-file)
                html_file=$2
                shift 2
                ;;

            *)
                printError "process_html_file: Invalid option '$1'."
                return 1
                ;;
        esac
    done

    if [ -z "$html_file" ]; then
        printError "process_html_file: No HTML file."
        return 1
    elif [ ! -f "$html_file" ]; then
        printError "process_html_file: File '$html_file' was not found."
        return 1
    elif grep --quiet "p42di 152" "$html_file"; then
        printVerbose "Test was not executed in $html_file."
        return 1
    fi

    #
    # The downloaded file has these fields embedded in a HTML comment so that we
    # can have everything in one single HTML file.
    #
    git_date=$(get_field_from_html --html-file $html_file "p42di 120")
    git_time=$(get_field_from_html --html-file $html_file "p42di 121")
    gittimezone=$(get_field_from_html --html-file $html_file "p42di 122")
    git_hash=$(get_field_from_html --html-file $html_file "p42di 130")
    
    git_branch=$(get_field_from_html --html-file $html_file "p42di 131")
    #git_branch=$(basename $git_branch)

    #test_class=$(get_test_class --html-file "$html_file")
    
    test_hostname=$(get_field_from_html --html-file $html_file "p42di 150")
    elapsed=$(get_field_from_html --html-file $html_file "p42di 140")
    test_status=$(get_test_status --html-file $html_file)
    
    test_name=$(basename "$html_file" .html)
    test_name=$(echo "$test_name" | tr '.' '_')

    n_skipped=$(cat $html_file | grep 'p42dc 101' | wc -l)
    n_failures=$(cat $html_file | grep 'p42dc 102' | wc -l)
    n_cases=$(cat $html_file | grep 'p42dc 10' | wc -l)

    if [ "$test_status" == "PREPARING" -o "$test_status" == "RUNNING" ]; then
        printWarning "Status of $test_name is $test_status, skipping..."
        return 1
    fi

    if [ "$n_cases" -eq 0 ]; then
        printWarning "8273 No test cases in $test_name..."
        echo "*** html_file: $html_file" >&2
        #ls -lha $html_file  >&2
        #cat $html_file >&2
        #echo ""   >&2
        #echo ""   >&2
        #exit 1
    fi

    #printError "status: $test_status for $html_file"
    case "$test_status" in 
        SUCCESS)
            let N_TESTS_PASSED+=1
            ;;

        FAILURE)
            let N_TESTS_FAILED+=1
            ;;
    esac

    cat <<EOF
<?xml version="1.0" ?>
<testsuite 
    tests="$n_cases"
    failures="$n_failures" 
    skipped="$n_skipped"
    package="s9s">
        <properties>
            <property name="pip_download_version" value="$VERSION"/>
            <property name="source_html" value="$PWD/$html_file"/>            
            <property name="test_name" value="$test_name"/>
            <property name="elapsed_time" value="$elapsed"/>
            <property name="git_branch" value="$git_branch"/>
            <property name="git_date" value="$git_date"/>
            <property name="git_time" value="$git_time"/>
            <property name="git_hash" value="$git_hash"/>
            <property name="status" value="$test_status"/>
            <property name="hostname" value="$test_hostname"/>
        </properties>    
EOF
    slice_file="/tmp/pip_download.html"

    test_case_names=$(get_test_names_html --html-file "$html_file")
    for test_case_name in $test_case_names; do
        printVerbose "  Processing test $test_case_name..."
        cut_test_case_html \
            --html-file "$html_file" \
            --output-file "$slice_file" \
            --test-name "$test_case_name"
        
        if [ $? -ne 0 ]; then
            continue
        fi

        emit_test_case_xml \
            --report-file "$PWD/$html_file" \
            --html-file   "$slice_file" \
            --test-name   "$test_case_name" \
            --test-suite  "$test_name" 

        if [ $? -ne 0 ]; then
            return 1
        fi
    done

cat <<EOF
</testsuite>
EOF
    return 0
}

function process_html_file()
{
    local test_name
    local html_file
    local git_date
    local git_time
    local git_timezone
    local elapsed
    local html_file

    while [ -n "$1" ]; do
        case "$1" in 
            --html-file)
                html_file=$2
                shift 2
                ;;

            *)
                printError "process_html_file: Invalid option '$1'."
                return 1
                ;;
        esac
    done

    if [ -z "$html_file" ]; then
        printError "process_html_file: No HTML file."
        return 1
    elif [ ! -f "$html_file" ]; then
        printError "process_html_file: File '$html_file' was not found."
        return 1
    fi

    #
    # The downloaded file has these fields embedded in a HTML comment so that we
    # can have everything in one single HTML file.
    #
    git_date=$(get_field_from_html --html-file $html_file "p42di 120")
    git_time=$(get_field_from_html --html-file $html_file "p42di 121")
    gittimezone=$(get_field_from_html --html-file $html_file "p42di 122")
    git_hash=$(get_field_from_html --html-file $html_file "p42di 130")
    elapsed=$(get_field_from_html --html-file $html_file "p42di 140")
    status=$(get_field_from_html --html-file "p42di 110")
    
    test_name=$(basename "$html_file" .html)

    cat <<EOF
    -------------------------------------------------------------
      html_file: $PWD/$html_file
      test_name: $test_name
       git_date: $git_date
       git_time: $git_time
    gittimezone: $gittimezone
       git_hash: $git_hash
        elapsed: $elapsed
         status: $status
EOF
  
    oldifs="$IFS"
    IFS=$'\n'
    for caseline in $(cat "$html_file" | grep "p42dc 10"); do
        my_status=$(echo "$caseline" | awk -F' ' '{print $2}')
        my_name=$(echo "$caseline" | awk -F' ' '{print $3}')
        cat <<EOF
             my_status: $my_status
               my_name: $my_name
EOF
    done
    IFS="$oldifs"
}

function old_email_stuff()
{
    if [ "$git_date" != "$today" -a "$git_date" != "$YESTERDAY" ]; then
        printVerbose "Git date for '$test_name' is '$git_date', not recent."
        rm -f $html_file
        return 0
    fi

    if [ "$status" == "SUCCESS" ]; then
        let N_TESTS_PASSED+=1
    elif [ "$status" == "FAILURE" ]; then
        let N_TESTS_FAILED+=1
    fi

    printf "%32s "  "$test_name"   | tee --append $REPORT_FILE
    printf "%-10s " "$status"      | tee --append $REPORT_FILE
    printf "%10s "  "$git_date"    | tee --append $REPORT_FILE
    printf "%8s "   "$git_time"    | tee --append $REPORT_FILE
    printf "%5s "   "$gittimezone" | tee --append $REPORT_FILE
    printf "%8s "   "$elapsed"     | tee --append $REPORT_FILE
    printf "%12s "  "$git_hash"    | tee --append $REPORT_FILE

    if [ "$git_date" != "$today" -a "$git_date" != "$YESTERDAY" ]; then
        rm -f $html_file
        printf "OLD\n" | tee --append $REPORT_FILE
        return 0
    fi

    printf "\n" | tee --append $REPORT_FILE
}

#
# This function prints the names of the tests that we process.
#
function test_names()
{
    local test_dir
    local test_dirs
    local script_file
    local retval

    if [ -n "$OPTION_TESTS" ]; then
        echo $OPTION_TESTS
        return 0
    fi
    
    #
    # The cmon tests.
    #
    test_dirs+=" ${PROJECT_CC_WORKER_DIR}/tests"


    #
    # The s9s tests.
    #
    test_dir="$HOME/Desktop/stuff/work/s9s-tools/tests"
    if [ ! -d "$test_dir" ]; then
        test_dir="$HOME/s9s-tools/tests"
    fi

    test_dirs+=" $test_dir"

    retval=1
    for test_dir in $test_dirs; do
        if [ -d "$test_dir" ]; then
            for script_file in $test_dir/ft_cmonha*; do
                basename=$(basename $script_file)

                if [ ! -f "$script_file/$basename" ]; then
                    continue
                fi

                echo $basename
                retval=0
            done
            
            for script_file in $test_dir/ft_*.sh; do
                if [ ! -f "$script_file" ]; then
                    continue
                fi

                echo $(basename $script_file)
                retval=0
            done
        fi
    done

    return $retval
}

function plural_test()
{
    local n="$1"

    if [ "$n" == "0" ]; then
        echo "no tests"
    elif [ "$n" == "1" ]; then
        echo "one test"
    else
        echo "$n tests"
    fi
}

function email_body()
{
    local subject
    local ref_url="https://80.98.196.73/ft_install/"

    if [ "$N_TESTS_FAILED" == "0" ]; then
        subject="Daily s9s test results (all $N_TESTS_PASSED green)."
    elif [ "$N_TESTS_FAILED" == "1" ]; then
        subject="Daily s9s test results (1 failure)."
    else
        subject="Daily s9s test results ($N_TESTS_FAILED failures)."
    fi

    echo -en "From: pipas@borgcollective.eu\r\n"
    echo -en "Subject: ${subject}.\r\n"
    echo -en "Mime-Version: 1.0\r\n"
    echo -en "Subject: Subject_heading\r\n"
    echo -en "Content-Type: multipart/mixed; boundary=\"-\"\r\n"
    echo -en "\r\n"
    
    echo -en "---\r\n"
    echo -en "Content-Type: text/html; charset=utf-8\r\n"
    echo -en "\r\n"
    echo "<html>"
    echo "  <body>"
    echo "    <h1>Daily s9s test results</h1>"
    echo "    <p>Please find the attached test results of the s9s functional "
    echo "tests that are selected for daily testing. Only tests that contain "
    echo "git commit date in the last day or today are presented here.</p>"
    echo "    <p>These test scrips can be found in the s9s-tools source "
    echo "under the tests/ subdirectory.</p>"
    echo "    <p>You may want to check the up to date test reports at "
    echo "      <a href=\"$ref_url\">$ref_url</a> (login name and password "
    echo "      required).</p>"
    echo "    <pre>"
    cat $REPORT_FILE
    echo "    </pre>"
    echo "  </body>"
    echo "</html>"
    echo ""

    echo -en "---\r\n"
    echo -en "Content-Type: application/octet-stream; name=\"$gzfile\"\r\n"
    echo -en "Content-Transfer-Encoding: base64\r\n"
    echo -en "Content-Disposition: inline; filename=\"$gzfile\"\r\n"
    echo -en "\r\n"
    
    uuencode --base64 $gzfile /dev/stdout
    echo -en "\r\n"
    echo -en "---\r\n"
}

#
# Checking the command line option.
#
if [ ! -d "$HOME/tests" ]; then
    mkdir $HOME/tests
fi

cd "$HOME/tests"
if [ "$PWD" != "$HOME/tests" ]; then
    printVerbose "Downloading test results failed, target directory was not found."
    printError "Could not create '$HOME/tests'..."
    exit 1
fi

#
#
#
#printVerbose "Downloading daily test results."
export WWW_PASSWORD="$PASSWORD"
export WWW_USER="$USER"

if [ ! -d "$OPTION_OUTPUT_DIR" ]; then
    printError "The output directory '$OPTION_OUTPUT_DIR' was not found."
    exit 1
else
    printVerbose "***  output_dir: $OPTION_OUTPUT_DIR"
fi

#
# Loading and checking the Jenkins configuration.
#
if [ -n "$OPTION_UPLOAD" ]; then
    CONFIG_FILE_NAME="${OPTION_OUTPUT_DIR}/${OPTION_CONFIG_NAME}.conf"
    if [ ! -f "$CONFIG_FILE_NAME" ]; then
        printError "File '$CONFIG_FILE_NAME' was not found."
        exit 6
    else
        printVerbose "*** config_file: $OPTION_OUTPUT_DIR"
        source "$CONFIG_FILE_NAME"
    fi

    printVerbose "*** JENKINS_DESTFILE: $JENKINS_DESTFILE"
    printVerbose "***    JENKINS_TOKEN: $JENKINS_TOKEN"
    printVerbose "***  JENKINS_APIUSER: $JENKINS_APIUSER"
    printVerbose "***  JENKINS_JOB_URL: $JENKINS_JOB_URL"
    printVerbose "*** JENKINS_SSH_PORT: $JENKINS_SSH_PORT"

    if [ -z "$JENKINS_DESTFILE" ]; then
        printError "Jenkins config is invalid, aborting..."
        exit 5
    fi
fi

if [ -z "$WWW_PASSWORD" ]; then
    printVerbose "The 'PASSWORD' environment variable should be present."
    exit 1
else
    printVerbose "***  www_passwd: 'xxxxxxxxx'" 
fi

if [ -z "$WWW_USER" ]; then
    printVerbose "The 'WWW_USER' environment variable should be present."
    exit 1
else
    printVerbose "***    www_user: '$WWW_USER'" 
fi

#
# Checking the test names.
#
mytests=$(test_names)
if [ -z "$mytests" ]; then
    printError "No tests found."
    exit 6
fi


[ -f "$REPORT_FILE" ] && rm -f $REPORT_FILE

print_header | tee --append $REPORT_FILE

NUMBER_OF_TEST_SCRIPTS=0
for testscript in $mytests; do
    #echo "1 testscript: $testscript"
    let NUMBER_OF_TEST_SCRIPTS+=1
done

#
# Removing old files from the output directory.
#
printVerbose "Removing old files...."
n_files_removed=0

for old_file in \
    $OPTION_OUTPUT_DIR/*.xml \
    $OPTION_OUTPUT_DIR/*.html \
    results.tar.bz2;
do
    if [ ! -f "$old_file" ]; then
        continue
    fi

    #printWarning "Removing $old_file..."
    rm -f $old_file
    let n_files_removed+=1
done

printVerbose "Removed $n_files_removed old files (XML, HTML)."

#
# Generating the reports...
#
for testscript in $mytests; do
    HTML_FILE=$(download_html_file "$testscript")
    if [ -z "$HTML_FILE" ]; then
        printWarning "HTML file for $testscript was not found..."
        continue
    fi

    if [ ! -f "$HTML_FILE" ]; then
        printWarning "File $HTML_FILE was not found..."
        continue
    fi

    XML_FILE="/tmp/pip_download.xml"

    XML_FILE="$OPTION_OUTPUT_DIR/"
    XML_FILE+="$(basename $HTML_FILE .html)"
    XML_FILE+=".xml"

    #
    # Here we print out some progress messages
    #
    printf "Processing $PWD/$HTML_FILE...               \r"

    printVerbose "Processing $HTML_FILE"
    process_html_file_xml \
        --html-file $HTML_FILE >$XML_FILE

    xml_file_processed=$?
    printVerbose "Processed $HTML_FILE"

    if [ $xml_file_processed -ne 0 ]; then
        printWarning "Removing $XML_FILE..."
        rm -f "$XML_FILE"
    fi

    cp $HTML_FILE $OPTION_OUTPUT_DIR
    echo "I am trying to figure this out." >"$OPTION_OUTPUT_DIR/readme.txt"
done

print_footer | tee --append $REPORT_FILE

#
# Uploading
#
if [ -n "$OPTION_UPLOAD" ]; then
    pushd $OPTION_OUTPUT_DIR
    SSH_KEY=""

    echo "Uploading to Jenkins..."
    printVerbose "Creating tarball..."
    tar -cjpf results.tar.bz2 *.txt *.xml *.html

    printVerbose "Uploading tarball to Jenkins..."

    #
    # Upload the file and trigger Jenkins to process it.
    #
cat <<EOF
    scp $SSH_KEY \\
        -oPort="$JENKINS_SSH_PORT" \\
        results.tar.bz2 \\
        jenkins@${JENKINS_IP}:${JENKINS_DESTFILE}

    ssh $SSH_KEY -oPort="$JENKINS_SSH_PORT" \\
        jenkins@${JENKINS_IP} -- \\
    curl --user "${JENKINS_APIUSER}" "${JENKINS_JOB_URL}"
EOF

    scp $SSH_KEY \
        -oPort="$JENKINS_SSH_PORT" \
        results.tar.bz2 \
        jenkins@${JENKINS_IP}:${JENKINS_DESTFILE}
    
    if [ $? -eq 0 ]; then
        printVerbose "Uploaded"
    else
        printError "Failed to upload"
    fi

    ssh $SSH_KEY -oPort="$JENKINS_SSH_PORT" \
        jenkins@${JENKINS_IP} -- \
        curl --user "${JENKINS_APIUSER}" "${JENKINS_JOB_URL}"

    if [ $? -eq 0 ]; then
        printVerbose "Triggered"
    else
        printError "Failed to trigger"
    fi

    popd
else
    echo "Skipping the upload, it was not requested."
fi


