def build_docker(name) {
    def image = "${name}-libs"
    echo "Build image ${image}"
	def pref = ""
    if (env.DOCKER_PREFIX) {
        pref = "--build-arg DOCKER_PREFIX=${env.DOCKER_PREFIX}/"
	}
	def jobs = 4
    if (env.JOBS_COUNT) {
        jobs = "${env.JOBS_COUNT}"
	}
	def args = "${pref} --build-arg LIBS_BUILD_NUMBER=${env.BUILD_NUMBER} --build-arg JOBS_COUNT=${jobs}"
	dir ("docker/${image}") {
	    sh "docker build ${args} --no-cache -t ${image} ."
    }
	return "\n - ${image}"
}
node {
	stage("checkout") {
		checkout scm
	}
    def _envmap = [:]
    def _env = sh(script: "env", returnStdout: true).trim().split("\n")
    _env.each{ l -> 
        def _ind = l.indexOf('=');
        def _n = l.substring(0, _ind);
        def _v = l.substring(_ind + 1);
        _envmap["${_n}"] = "${_v}";
    }
	def _msg = "Built images:"
    def platforms = ['debian', 'osx', 'windows', 'android', 'pi']
	for (int i = 0; i < platforms.size(); ++i) {
	    if (_envmap["BUILD_${platforms[i]}"] == "1") {
			stage("${platforms[i]}-libs") {
				_msg += build_docker("${platforms[i]}")
			}
	    }
	}
	sh "docker system prune -f"
	echo "${_msg}"
}
pipeline {
	agent {
		label 'master'
	}
    stages {
		stage("PIP doc") {
			steps {
				checkout scm
				sh "rm -rf share"
				sh "rm -vf *.zip"
				sh "mkdir -p build"
				sh "cd build && cmake -DLIB=0 -DCMAKE_INSTALL_PREFIX=`pwd` ../"
				sh "cd build && make doc"
				sh "cd share/doc && zip -r ../../pip_doc.zip pip"
				sh "cp share/doc/pip/html/pip.qch ./"
				archiveArtifacts 'pip_doc.zip'
				archiveArtifacts 'pip.qch'
			}
		}
	}
}
