#!/bin/bash

set -e

if [ "${LINT_CHECK}" != "0" ]; then
    pip install -Uq flake8=="3.4.1" Click pylint-mccabe pbr

    # Install pylint plugin depends without lxml
    wget -q https://raw.githubusercontent.com/OCA/pylint-odoo/master/requirements.txt -O ${HOME}/maintainer-quality-tools/travis/pylint_odoo_requirements.txt
    pip install --upgrade -r ${HOME}/maintainer-quality-tools/travis/pylint_odoo_requirements.txt
    pip install --upgrade --pre --no-deps git+https://github.com/OCA/pylint-odoo.git   # To use last version ever
    npm install -g eslint  # Extra package for pylint-odoo plugin
    if [ -f "${HOME}/.nvm/nvm.sh" ]; then
        # Update nodejs v6.latest required by eslint
        # Using nvm of travis
        CURRENT_NODE=$(which node)
        source ${HOME}/.nvm/nvm.sh
        nvm install 6
        ln -sf $(nvm which 6) $CURRENT_NODE
    fi
fi

# We can exit here and do nothing if this is only a LINT check
if [ "${TESTS}" != "1" ] && [ "${LINT_CHECK}" == "1" ]; then
    exit 0
fi


# Install less
ln -s `which nodejs` $HOME/maintainer-quality-tools/travis/node
# Remove fixed version after fix https://github.com/odoo/odoo/issues/25632
npm install -g less@3.0.4 less-plugin-clean-css

if [ "${WEBSITE_REPO}" == "1" ]; then
    if [ -f ~/.rvm/scripts/rvm ]; then
        source ~/.rvm/scripts/rvm
    else
        source /usr/local/rvm/scripts/rvm
    fi
    rvm install ruby --latest
    rvm use ruby --latest
    # Uninstall current versions to be sure that
    # the correct version will be installed
    gem uninstall -aIx bootstrap-sass compass sass
    gem install compass bootstrap-sass
fi

# Update PhantomJS (v10 compat)
if [ "${PHANTOMJS_VERSION}" != "OS" ]; then
    npm install --prefix ${TRAVIS_BUILD_DIR} "phantomjs-prebuilt@${PHANTOMJS_VERSION:=latest}"
    ln -s "${TRAVIS_BUILD_DIR}/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs" "${HOME}/maintainer-quality-tools/travis/phantomjs"
fi

# For backward compatibility, take version from parameter if it's not globally set
if [ "x${VERSION}" == "x" ] ; then
    VERSION="${1}"
    echo "WARNING: no env variable set for VERSION. Using '${1}'."
fi

if [ "x${ODOO_BRANCH}" == "x"  ] ; then
    ODOO_BRANCH=${VERSION}
fi

: ${ODOO_REPO:="odoo/odoo"}  # default value, if not set
IFS="/" read -a REPO <<< "${ODOO_REPO}"
export REMOTE="${REPO[0],,}"
export REPO_NAME="${REPO[1]}"
export BRANCH="${ODOO_BRANCH}"
export PULL_REQUEST=''
# If the branch is a pull request
if [[ $BRANCH == *"/"*  ]]; then
    export PULL_REQUEST=$BRANCH
    export BRANCH=${BRANCH/\//-}
fi
export ODOO_PATH=${HOME}/$REPO_NAME-$ODOO_BRANCH
if [ -d "${REPO_CACHED}/odoo" ]; then
    echo "Using Odoo from cache ${REPO_CACHED}"
    ln -sf ${REPO_CACHED}/odoo ${ODOO_PATH}
else
    echo "Downloading Odoo"
    git clone --depth=50 https://github.com/$REMOTE/$REPO_NAME.git -b $BRANCH ${ODOO_PATH}
    git --work-tree=${ODOO_PATH} --git-dir=${ODOO_PATH}/.git remote rename origin $REMOTE
fi
if [ "x${PULL_REQUEST}" == "x" ] ; then
    git --work-tree=${ODOO_PATH} --git-dir=${ODOO_PATH}/.git fetch --depth=1 ${REMOTE} ${BRANCH} \
        && git --work-tree=${ODOO_PATH} --git-dir=${ODOO_PATH}/.git config --local --bool core.bare false \
        && git --work-tree=${ODOO_PATH} --git-dir=${ODOO_PATH}/.git checkout -b ${BRANCH}-${REMOTE} -qf ${REMOTE}/${BRANCH}
else
    # Is a pull request
    git --work-tree=${ODOO_PATH} --git-dir=${ODOO_PATH}/.git reset --hard
    git --work-tree=${ODOO_PATH} --git-dir=${ODOO_PATH}/.git fetch --depth=1 ${REMOTE} $PULL_REQUEST/head:${BRANCH}-${REMOTE} \
        && git --work-tree=${ODOO_PATH} --git-dir=${ODOO_PATH}/.git config --local --bool core.bare false \
        && git --work-tree=${ODOO_PATH} --git-dir=${ODOO_PATH}/.git checkout ${BRANCH}-${REMOTE}
fi

# Workaround to force using system site packages (see https://github.com/Shippable/support/issues/241#issuecomment-57947925)
rm -f $VIRTUAL_ENV/lib/python2.7/no-global-site-packages.txt
pip install --upgrade pip
pip install -q --no-binary pycparser -r ${HOME}/maintainer-quality-tools/requirements.txt

# Odoo <= 7.0 doesn't have requirements.txt file, then the 8.0 file is used by default
if [ ! -f ${ODOO_PATH}/requirements.txt ]; then
    wget https://raw.githubusercontent.com/odoo/odoo/8.0/requirements.txt -O ${ODOO_PATH}/requirements.txt
fi
# Remove python-ldap from odoo requirements because is not a common module used
sed -i '/^python-ldap\=\=/d' ${ODOO_PATH}/requirements.txt
# Use requests with [security] suffix to fix [Errno 111] Connection refused for old python2.7 versions.
sed -i 's/^requests\=\=/requests[security]\=\=/g' ${ODOO_PATH}/requirements.txt

pip install -q --no-binary pycparser -r ${ODOO_PATH}/requirements.txt
pip install -q QUnitSuite coveralls codecov

# Use reference .coveragerc
cp ${HOME}/maintainer-quality-tools/cfg/.coveragerc .

echo "Getting addons dependencies"
clone_oca_dependencies
clone_result=$?
if [ "$clone_result" != "0"  ]; then
    echo "Error cloning dependencies"
    exit $clone_result
fi;

if [[ "${WKHTMLTOPDF_VERSION}" == "" && $(which wkhtmltopdf) != "" ]]; then
    echo "You have installed wkhtmltopdf but is not patched (probably) then we will to overwrite it"
    export WKHTMLTOPDF_VERSION="0.12.4"
fi;

if [ "${WKHTMLTOPDF_VERSION}" != "" ]; then
    echo "Install webkit (wkhtmltopdf) patched version ${WKHTMLTOPDF_VERSION}"
    (cd ${HOME}/maintainer-quality-tools/travis/ && wget -qO- -t 1 --timeout=240 https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/${WKHTMLTOPDF_VERSION}/wkhtmltox-${WKHTMLTOPDF_VERSION}_linux-generic-amd64.tar.xz | tar -xJ --strip-components=2 wkhtmltox/bin/wkhtmltopdf)
fi;

# Expected directory structure:
#
#    HOME/
#     |___ <OdooRepo>-<Branch>/         <-- Odoo Server
#     |___ maintainer-quality-tools/
#     |___ build/<Owner>/<TestedRepo>/
#     |___ dependencies/<DependencyRepo1>/
#     |___ dependencies/<DependencyRepo2>/
#     |...
echo "Content of ${HOME}:"
ls -l ${HOME}

echo "Content of ${HOME}/dependencies:"
mkdir -p ${HOME}/dependencies && ls -l ${HOME}/dependencies

set +e
