#!/bin/bash

declare -r name=${1:-docker.seblu.net/docker/dokuwiki}
# Dokuwiki upstream consider the last reliable version to be rc when available.
# The update_check in dokuwiki is always looking for RC too.
# https://forum.dokuwiki.org/d/8939-update-check-should-not-be-used-for-release-candidates
# so we tag latest rc when available, either stable or oldstable.
declare latest=''

# declare well known versions. order matter!
declare -a versions=(dev oldstable stable rc)
declare -A url=(
  ["dev"]="http://github.com/splitbrain/dokuwiki/tarball/master"
  ["oldstable"]="https://download.dokuwiki.org/src/dokuwiki/dokuwiki-oldstable.tgz"
  ["stable"]="https://download.dokuwiki.org/src/dokuwiki/dokuwiki-stable.tgz"
  ["rc"]="https://download.dokuwiki.org/src/dokuwiki/dokuwiki-rc.tgz"
)

for version in "${versions[@]}"; do
  # do not build unavailable url (e.g: rc is only avaible before a new release)
  echo "Checking $version url ${url[$version]}"
  curl -Isf "${url[$version]}" -o /dev/null || continue
  echo "Building $version"
  docker build --build-arg=DOKUWIKI_URL="${url[$version]}" --tag "$name:$version" $BUILD_EXTRA .
  latest=$version
done

echo "Tag latest: $latest"
docker tag $name:$latest $name:latest

echo =========================================================================
echo ::: IMAGE NOT PUSHED
echo ::: to push image:
for version in "${!versions[@]}"; do
  echo       docker push "$name:$version"
done
echo       docker push "${name%:*}:latest"
echo =========================================================================
