pipeline {
	agent {
		docker {
			image 'windows-qtcreator'
			args '-u root:root --privileged'
		}
	}
	options {
		copyArtifactPermission('*')
		buildDiscarder(logRotator(numToKeepStr: "1000", artifactNumToKeepStr: "2"))
	}
	stages {
		stage('shstk') {
			steps {
				script {
					sh 'rm -rf /soft/shstk'
					sh 'git clone -b master --single-branch --depth 1 --recursive https://git.shstk.ru/SHS/shstk.git /soft/shstk'
					sh 'mkdir -p /soft/shstk_build_host && rm -rf /soft/shstk_build_host/*'
					sh 'cd /soft/shstk_build_host && cmake -G Ninja -DICU=0 -DCROSSTOOLS=1 -DBUILD_NUMBER=9999 ../shstk'
					sh 'cd /soft/shstk_build_host && cmake --build ./ --target install -j${JOBS_COUNT}'
					sh 'ldconfig'

					sh 'mkdir -p /soft/shstk_build_windows && rm -rf /soft/shstk_build_windows/*'
					sh 'cd /soft/shstk_build_windows && cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=/soft/toolchain-Windows.cmake -DCMAKE_INSTALL_PREFIX=/soft/windows -DCMAKE_PREFIX_PATH=/soft/qt/mingw_64 -DQT_HOST_PATH=/soft/qt/gcc_64 -DICU=0 -DLOCAL=1 -DQGLENGINE=1 -DQGLVIEW=0 -DPIP_BUILD_OPENCL=0 -DBUILD_NUMBER=9999 -DOpenCL_INCLUDE_DIRS=/soft/windows/include ../shstk/'
					sh 'cd /soft/shstk_build_windows && cmake --build ./ --target install -j${JOBS_COUNT}'
				}
			}
		}
		
		stage('SHS') {
			steps {
				script {
					sh 'rm -rf /soft/SHS'
					sh 'git clone -b v1_master --single-branch --depth 1 --recursive https://git.shstk.ru/SHS/SHS.git /soft/SHS'
					sh 'mkdir -p /soft/SHS/build && rm -rf /soft/SHS/build/*'
					sh 'cd /soft/SHS/build && cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=/soft/toolchain-Windows.cmake -DCMAKE_INSTALL_PREFIX=/soft/windows -DCMAKE_PREFIX_PATH=/soft/qt/mingw_64 -DQT_HOST_PATH=/soft/qt/gcc_64 -DSHS_ONLY_LIBS=1 -DBUILD_NUMBER=9999 -DSHS_QT_VERSION=6 ../src'
					sh 'cd /soft/SHS/build && cmake --build ./ --target install -j${JOBS_COUNT}'
					sh 'cp -vf /soft/SHS/builds/Windows_x86_64/*.dll /soft/windows/bin/'
					sh 'cp -vf /soft/SHS/builds/Windows_x86_64/lang/* /soft/windows/lang/'
					sh 'cp -rvf /soft/SHS/builds/Windows_x86_64/designer /soft/qt/mingw_64/plugins/'
				}
			}
		}

		stage('llama') {
			steps {
				script {
					sh 'rm -rf /soft/qt-creator-plugins'
					sh 'git clone --single-branch --depth 1 https://github.com/cristianadam/llama.qtcreator.git /soft/qt-creator-plugins'
					sh 'mkdir -p /soft/qt-creator-plugins/build && rm -rf /soft/qt-creator-plugins/build/*'
					sh 'cd /soft/qt-creator-plugins/build && cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=/soft/toolchain-Windows.cmake -DCMAKE_INSTALL_PREFIX=/soft/windows/qt-creator -DCMAKE_PREFIX_PATH=/soft/qt/mingw_64 -DQT_HOST_PATH=/soft/qt/gcc_64 -DQtCreator_DIR=/soft/qt-creator_build/cmake ../'
					sh 'cd /soft/qt-creator-plugins/build && cmake --build ./ --target install -j${JOBS_COUNT}'
				}
			}
		}
		
		stage('qt-creator-plugins') {
			steps {
				script {
					sh 'rm -rf /soft/qt-creator-plugins'
					sh 'git clone -b v18 --single-branch --depth 1 https://git.shstk.ru/SHS/qtcreator.git /soft/qt-creator-plugins'
					sh 'mkdir -p /soft/qt-creator-plugins/build && rm -rf /soft/qt-creator-plugins/build/*'
					sh 'cd /soft/qt-creator-plugins/build && cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=/soft/toolchain-Windows.cmake -DCMAKE_INSTALL_PREFIX=/soft/windows/qt-creator -DCMAKE_PREFIX_PATH=/soft/qt/mingw_64 -DQT_HOST_PATH=/soft/qt/gcc_64 -DQtCreator_DIR=/soft/qt-creator_build/cmake ../project_fs'
					sh 'cd /soft/qt-creator-plugins/build && cmake --build ./ --target install -j${JOBS_COUNT}'
				}
			}
		}
		
		stage('deploy') {
			steps {
				script {
					sh 'cd /soft/windows/qt-creator && cp -vfr /soft/qt/mingw_64/plugins ./'
					sh 'cd /soft/windows/qt-creator && cp -vfr /soft/qt/gcc_64/plugins/designer/lib*.dll ./plugins/designer/'
					sh 'cd /soft/windows/qt-creator && cp -vfr /soft/windows/lang/* ./translations/'
					sh 'cd /soft/windows/qt-creator && cp -vfr /soft/qt/mingw_64/bin/* ./bin/'
					sh 'cd /soft/windows/qt-creator && cp -vfrL /soft/windows/bin/* ./bin/ && rm -vf ./bin/llvm-*'
					sh 'cd /soft/windows/qt-creator && cp -vf /usr/lib/gcc/x86_64-w64-mingw32/14-posix/*.dll ./bin/'
					sh 'cd /soft/windows/qt-creator && for f in $(ls ./bin/*); do if [[ $(file -b $f | grep "ELF 64-bit") ]]; then rm -v $f; fi; done'
					sh 'cd /soft/windows/qt-creator && zip -qr /soft/qt-creator-19_win_x64.zip *'
					sh 'rm ./* || true'
					sh 'mv -v /soft/qt-creator-19_win_x64.zip ./'
					archiveArtifacts 'qt-creator-*.zip'
				}
			}
		}
	}
}
