#!/bin/bash -xe

if [ "$#" == 0 ]; then
   DUMP_BRANCH=${BRANCH}
else
   DUMP_BRANCH=$1
fi

cd "$5"

export GITLAB_TOKEN=Pej7sZt-kvyD9xo94tTC

DUMP_BRANCH=$(php -r "echo urlencode(\"$DUMP_BRANCH\");")

curl -# -L -f --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" "https://gitlab.wpj.cz/api/v4/projects/2/jobs/artifacts/${DUMP_BRANCH}/download?job=dump-database" > data.zip

unzip -o data.zip

# Import database
bunzip2 -c database.sql.bz2 | mysql.php

rm data.zip database.sql.bz2
