#!/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.
#

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

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

    if [[ ! "${exists}" ]]; then
      echo "
        FROM python:${v}
        RUN apt-get update; apt-get install -y ffmpeg libimage-exiftool-perl
        RUN pip install --upgrade pip
        RUN pip install pytest pytest-sugar pytest-cov pytest-xdist
        RUN pip install cryptography file-magic requests termcolor dnspython \
          && rm -rf /root/.cache
      " | sed -E 's/^ *//' | docker build -t danielquinn/aletheia:debian-${v} --file - .
    fi

    docker run \
      --rm \
      -v ${PROJECT_ROOT}:/app \
      -it danielquinn/aletheia:debian-${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

  exists=$(docker images | grep -E "^danielquinn/aletheia *arch ")

  if [[ ! "${exists}" ]]; then
    echo "
      FROM archlinux/base
      RUN pacman -Syu --noconfirm
      RUN pacman -S --noconfirm base-devel python python-pip ffmpeg perl-image-exiftool
      RUN ln -s /usr/bin/vendor_perl/exiftool /usr/bin/exiftool
      RUN pip install pytest pytest-sugar pytest-cov pytest-xdist
      RUN pip install cryptography file-magic requests termcolor dnspython
    " | sed -E 's/^ *//' | docker build -t danielquinn/aletheia:arch --file - .
  fi

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

}


function fedora {

  for v in 26 27 28 29; do

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

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

    if [[ ! "${exists}" ]]; then
      echo "
        FROM fedora:${v}
        RUN dnf install --assumeyes https://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-\$(rpm -E %fedora).noarch.rpm https://download1.rpmfusion.org/nonfree/fedora/rpmfusion-nonfree-release-\$(rpm -E %fedora).noarch.rpm
        RUN dnf install --assumeyes findutils ffmpeg perl-Image-ExifTool
        RUN pip3 install pytest pytest-sugar pytest-cov pytest-xdist
        RUN pip3 install cryptography file-magic requests termcolor dnspython
      " | sed -E 's/^ *//' | docker build -t danielquinn/aletheia:fedora-${v} --file - .
    fi

    # 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 danielquinn/aletheia:fedora-${v} \
        /app/tests/cross-platform inside -m 'not ffmpeg_min_version34'

    else

      docker run \
        --rm \
        -v ${PROJECT_ROOT}:/app \
        -it danielquinn/aletheia: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
