#!/bin/bash

test_server_stdout=tests/server.stdout

function install_deps {
    pip install -r requirements-dev.txt
}

function start_server {
    rm -f ${test_server_stdout}
    python tests/m3u8server.py >${test_server_stdout} 2>&1 &
}

function stop_server {
    ps ax | grep m3u8server.py | grep -v grep | cut -d ' ' -f 1 | xargs kill
    echo "Test server stdout on ${test_server_stdout}"
}

function run {
    PYTHONPATH=. py.test -vv --cov-report term-missing --cov m3u8 tests/
}

function main {
    install_deps
    start_server
    run
    retval=$?
    stop_server
    return "$retval"
}

if [ -z "$1" ]; then
    main
else
    $@
fi
