#!/bin/bash

# this test script runs "./manage.py test" for
# all supported python versions (2.4, 2.5, 2.6)
# and all supported Django versions (1.1, 1.2, 1.3)

# it needs symbolic links named "django1.1" and "django1.2" etc. in:
# libraries-local/django-versions
# which point to the respective django versions

cd libraries-local
rm -f django-orig
if test -e django ; then mv django django-orig ; fi
cd ..

function restore_django {
    echo "### restoring original libraries-local/django"
    cd libraries-local
    if test -e django-orig  ; then mv django-orig django ; fi
    cd .
}

function test_python_version {
    echo ; echo ; echo
    echo "#########################################################################"
    echo "### Testing Python $1, Django $2"
    echo "#########################################################################"
    echo

    if which python$1 ; then
        if ! python$1 manage.py test polymorphic; then
            echo ERROR
            restore_django
            exit 10
        fi
        if ! ./test_dumpdata $1 ; then
            echo ERROR
            restore_django
            exit 10
        fi
    else
        echo
        echo "### python $1 is not installed!"
        echo
    fi
}

function test_all_python_versions {
    test_python_version 2.4 $1
    test_python_version 2.5 $1
    test_python_version 2.6 $1
}

function test_django_version {
    if ! test -e libraries-local/django-versions/django$1 ; then
        echo
        echo "### django $1 is not installed!"
        echo
        return
    fi
    cd libraries-local
    rm -f django
    ln -s django-versions/django$1 django
    cd ..
    test_all_python_versions $1
}

test_django_version 1.1
test_django_version 1.2
test_django_version 1.3

restore_django

