#!/bin/bash

#
# Attempts to run the test suite on a variety of operating systems and software
# versions.  To run this thing, you'll need Docker installed locally and a few
# gigabytes of disk space for the various containers it pulls down and/or
# creates.
#
# To run one of these containers directly (to do tests in a specific
# environment), you can just run something like:
#
#   docker run --rm -v $(pwd):/app -it danielquinn/aletheia:debian-3.5 /bin/bash
#

PROJECT_ROOT=$(readlink -f $(dirname $0)/..)


function debian {

  for v in 3.5 3.6 3.7; do

    echo
    echo
    echo
    echo "Debian ${v}"
    echo "===================================================================="
    echo

    docker run \
      --rm \
      -v ${PROJECT_ROOT}:/app \
      -it registry.gitlab.com/danielquinn/aletheia-python:debian9-python${v} \
      /app/tests/cross-platform inside -m 'not ffmpeg_min_version34'

  done

}


function alpine {

  # This doesn't work yet

  for v in 3.6 3.7; do

    echo
    echo
    echo
    echo "Alpine ${v}"
    echo "===================================================================="
    echo

    exists=$(docker images | grep -E "^danielquinn/aletheia *alpine-${v} ")

    if [[ ! "${exists}" ]]; then
      echo "
        FROM python:${v}-alpine
        RUN apk update; apk add ffmpeg perl-image-exiftool libffi-dev libmagic
        RUN wget https://patch-diff.githubusercontent.com/raw/python/cpython/pull/10461.patch -O - \
          | patch /usr/local/lib/python${v}/ctypes/util.py
        RUN pip install --upgrade pip
        RUN pip install pytest pytest-sugar pytest-cov pytest-xdist
        RUN apk add --update --no-cache --virtual .build-deps gcc g++ make tzdata ca-certificates python3-dev musl-dev openssl-dev \
          && pip install cryptography file-magic requests termcolor dnspython \
          && apk del .build-deps \
          && rm -rf /root/.cache
      " | sed -E 's/^ *//' | docker build -t danielquinn/aletheia:alpine-${v} --file - .
    fi

    docker run \
      --rm \
      -v ${PROJECT_ROOT}:/app \
      -it danielquinn/aletheia:alpine-${v} \
      /app/tests/cross-platform inside

  done

}


function arch {

  echo
  echo
  echo
  echo "Arch"
  echo "===================================================================="
  echo

  docker run \
    --rm \
    -v ${PROJECT_ROOT}:/app \
    -it registry.gitlab.com/danielquinn/aletheia-python:arch \
    /app/tests/cross-platform inside

}


function fedora {

  for v in 26 27 28 29; do

    echo
    echo
    echo
    echo "Fedora ${v}"
    echo "===================================================================="
    echo

    # I can't figure out how to get the arguments to pass properly, so we have
    # a little copypasta here.

    if [[ ${v} = 26 || ${v} = 27 ]]; then

      docker run \
        --rm \
        -v ${PROJECT_ROOT}:/app \
        -it registry.gitlab.com/danielquinn/aletheia-python:fedora${v} \
        /app/tests/cross-platform inside -m 'not ffmpeg_min_version34'

    else

      docker run \
        --rm \
        -v ${PROJECT_ROOT}:/app \
        -it registry.gitlab.com/danielquinn/aletheia-python:fedora${v} \
        /app/tests/cross-platform inside

    fi

  done

}


if [[ "${1}" = "inside" ]]; then

  cd /app
  pip3 install --quiet .
  pytest -n auto "${@:2}"
  rm -rf $(find . -name '__pycache__')

else

  debian
  arch
  fedora

fi
