#!/bin/bash

EXT_NAME=$1
EXT_VERSION=$2

cd "${WIKI_SOURCE}/extensions"

if [[ -e "${EXT_NAME}" ]]; then
    cp -Rv "${EXT_NAME}" .
    cd "$(basename ${EXT_NAME})"
else
    git clone "https://gerrit.wikimedia.org/r/mediawiki/extensions/${EXT_NAME}"
    cd "${EXT_NAME}"
fi

if [[ "${EXT_VERSION}" ]]; then
    git checkout -b "${EXT_VERSION}" "origin/${EXT_VERSION}"
fi

if [[ -e ".git" ]]; then
    git submodule update --init
fi

if [[ -e "composer.json" ]]; then
    composer install --no-dev
fi

chown -R www-data:www-data .
