#!/usr/bin/env bash
## Shared script part for CircleCI
##
## Copyright (c) 2012-2014 Sylvain Benner
## Copyright (c) 2014-2018 Sylvain Benner & Contributors
##
## Author: Eugene Yaremenko
## URL: https://github.com/syl20bnr/spacemacs
##
## This file is not part of GNU Emacs.
##
## License: GPLv3

# Vars
dev_b="develop"
mst_b="master"
prj_owner="syl20bnr"
prj_repo="spacemacs"
bot_login="emacspace"
api_URL_root="https://api.github.com"

upstream_data_URL_root="${api_URL_root}/"
upstream_data_URL_root+="repos/"
upstream_data_URL_root+="${prj_owner}/"
upstream_data_URL_root+="${prj_repo}"

CIRCLE_PR_NUMBER="${CIRCLE_PR_NUMBER:-${CIRCLE_PULL_REQUEST##*/}}"
pr_data_URL="${upstream_data_URL_root}/pulls/${CIRCLE_PR_NUMBER}"
upstream_data_URL="${upstream_data_URL_root}/branches/develop"

pr_file_list_URL="${api_URL_root}/"
pr_file_list_URL+="repos/"
pr_file_list_URL+="${prj_owner}/"
pr_file_list_URL+="${prj_repo}/"
pr_file_list_URL+="pulls/"
pr_file_list_URL+="${CIRCLE_PR_NUMBER}/"
pr_file_list_URL+="files"

# Helpers
echo_headline () {
	printf '=%.0s' {1..80}
	printf "\n$1\n"
	printf '=%.0s' {1..80}
	echo
}

select_changed_orgs () {
    changed_f_as_args=()
    while read p
    do
        if [ -f "$p" ]; then
            if [ ${p: -4} == ".org" ]; then
                changed_f_as_args+=("${p}")
            fi
        fi
    done </tmp/changed_files
}

fail_when_undefined_pr_number () {
    if [[ -z "${CIRCLE_PR_NUMBER// }" ]]; then
        echo "CircleCI didn't set CIRCLE_PR_NUMBER"
        echo "nor CIRCLE_PULL_REQUEST."
        echo "Job restart sometimes helps."
        exit 1
    fi
}

skip_when_branch_update () {
    if [[ "${IS_BRANCH_UPDATE}" == "yes" ]]; then
        echo "Skipping for branch update."
        exit 0
    fi
}

cleanup () {
	rm -rf ~/.emacs.d/elpa ~/.emacs.d/.cache ~/.spacemacs
}
