#!/bin/bash
#UserSettings
ThisScriptConfigured=no
GitRepo=https://git.bprieshof.nl/brammp/HS-Testing.git
JenkinsUsername=<Username>
JenkinsPassword=<Password>
LXCBuilderIP=<ip.of.the.BuilderCT>

#StaticVars
AdminPassword=$(cat /var/lib/jenkins/secrets/initialAdminPassword)
JeninsPlugins="dark-theme ssh-slaves workflow-aggregator locale golang git dark-theme copyartifact ws-cleanup"

#Check if script is configured
if [ $ThisScriptConfigured = no ]; then
    echo  "Script is not configured"
    exit
fi

#Download Jenkins-CLI tool
curl localhost:8080/jnlpJars/jenkins-cli.jar -o /opt/jenkins-cli.jar
#Install Plugins 
java -jar /opt/jenkins-cli.jar -s http://localhost:8080/ -webSocket -auth admin:$AdminPassword install-plugin $JeninsPlugins
echo "jenkins.model.Jenkins.instance.securityRealm.createAccount(\"$JenkinsUsername\", \"$JenkinsPassword\")" | java -jar /opt/jenkins-cli.jar -s "http://localhost:8080" -webSocket -auth admin:$AdminPassword groovy = –

#Installed plugin versions
InstalledPlugins=$(java -jar /opt/jenkins-cli.jar -s http://localhost:8080/ -webSocket -auth admin:$AdminPassword list-plugins)
#Update configuratiopn before putting them in place
sed -i -e 's/LocaleVersion/'$(printf "%s" "${InstalledPlugins%x}" | grep '^locale ' | awk '{ print $NF}')'/g' /opt/Setup/Configs/locale.xml
sed -i -e 's/DarkThemeVersion/'$(printf "%s" "${InstalledPlugins%x}" | grep '^dark-theme ' | awk '{ print $NF}')'/g' -e 's/ThemeManagerVersion/'$(printf "%s" "${InstalledPlugins%x}" | grep '^theme-manager ' | awk '{ print $NF}')'/g' /opt/Setup/Configs/io.jenkins.plugins.thememanager.ThemeManagerPageDecorator.xml
sed -i -e 's/GoPluginVersion/'$(printf "%s" "${InstalledPlugins%x}" | grep '^golang ' | awk '{ print $NF}')'/g' -e 's/GoVersion/'$(curl -s https://golang.org/VERSION?m=text |sed 's/go//g')'/g' /opt/Setup/Configs/org.jenkinsci.plugins.golang.GolangBuildWrapper.xml
mv -t /var/lib/jenkins /opt/Setup/Configs/locale.xml /opt/Setup/Configs/io.jenkins.plugins.thememanager.ThemeManagerPageDecorator.xml /opt/Setup/Configs/org.jenkinsci.plugins.golang.GolangBuildWrapper.xml 

#Restart
java -jar /opt/jenkins-cli.jar -s http://localhost:8080/ -webSocket -auth admin:$AdminPassword safe-shutdown
echo "Waiting 30 sec for jenkins to shutdown"
sleep 30
systemctl restart jenkins
echo "Waiting 1 min for Systemctl to restart jenkins"
sleep 60

#Add Jobs
cat /opt/Setup/Configs/FolderJob.xml | sed -e 's/FoldersVersions/'$(printf "%s" "${InstalledPlugins%x}" | grep '^cloudbees-folder ' | awk '{ print $NF}')'/g' | java -jar /opt/jenkins-cli.jar -s http://localhost:8080/ -webSocket -auth admin:$AdminPassword create-job LXC-ImgBuilder
cat /opt/Setup/Configs/BuildJobTemplate.xml | sed -e 's#BuildJenkinsFile#Other-Builds/DistroBuilder.Jenkinsfile#g' -e 's#GitRepo#'$GitRepo'#g' -e 's/GITVersion/'$(printf "%s" "${InstalledPlugins%x}" | grep '^git ' | awk '{ print $NF}')'/g' -e 's/WorkFlowCPSVersion/'$(printf "%s" "${InstalledPlugins%x}" | grep '^workflow-cps ' | awk '{ print $NF}')'/g' -e 's/WorkFlowJobVersion/'$(printf "%s" "${InstalledPlugins%x}" | grep '^workflow-job ' | awk '{ print $NF}')'/g' | java -jar /opt/jenkins-cli.jar -s http://localhost:8080/ -webSocket -auth admin:$AdminPassword create-job LXC-ImgBuilder/DistroBuilder
cat /opt/Setup/Configs/BuildJobTemplate.xml | sed -e 's#BuildJenkinsFile#CT-Build/Debian.Jenkinsfile#g' -e 's#GitRepo#'$GitRepo'#g' -e 's/GITVersion/'$(printf "%s" "${InstalledPlugins%x}" | grep '^git ' | awk '{ print $NF}')'/g' -e 's/WorkFlowCPSVersion/'$(printf "%s" "${InstalledPlugins%x}" | grep '^workflow-cps ' | awk '{ print $NF}')'/g' -e 's/WorkFlowJobVersion/'$(printf "%s" "${InstalledPlugins%x}" | grep '^workflow-job ' | awk '{ print $NF}')'/g' | java -jar /opt/jenkins-cli.jar -s http://localhost:8080/ -webSocket -auth admin:$AdminPassword create-job LXC-ImgBuilder/Debian-LXC
cat /opt/Setup/Configs/BuildJobTemplate.xml | sed -e 's#BuildJenkinsFile#CT-Build/Alpine.Jenkinsfile#g' -e 's#GitRepo#'$GitRepo'#g' -e 's/GITVersion/'$(printf "%s" "${InstalledPlugins%x}" | grep '^git ' | awk '{ print $NF}')'/g' -e 's/WorkFlowCPSVersion/'$(printf "%s" "${InstalledPlugins%x}" | grep '^workflow-cps ' | awk '{ print $NF}')'/g' -e 's/WorkFlowJobVersion/'$(printf "%s" "${InstalledPlugins%x}" | grep '^workflow-job ' | awk '{ print $NF}')'/g' | java -jar /opt/jenkins-cli.jar -s http://localhost:8080/ -webSocket -auth admin:$AdminPassword create-job LXC-ImgBuilder/Alpine-LXC
#Add Node
cat /opt/Setup/Configs/LXCBuilder1.xml | sed -e 's/SSHAgentVersion/'$(printf "%s" "${InstalledPlugins%x}" | grep '^golang ' | awk '{ print $NF}')'/g' -e 's/LXCBuilderIP/'$LXCBuilderIP'/g' | java -jar /opt/jenkins-cli.jar -s http://localhost:8080/ -webSocket -auth admin:$AdminPassword create-node LXCBuilder1

# BuildDistroBuilder (add -f to wait for job completion)
java -jar /opt/jenkins-cli.jar -s http://localhost:8080/ -webSocket -auth admin:$AdminPassword  build LXC-ImgBuilder/DistroBuilder -f

#Run both LXC builds, to fetch the jenkins file and set update the available parameters (The builds WILL fail)
java -jar /opt/jenkins-cli.jar -s http://localhost:8080/ -webSocket -auth admin:$AdminPassword  build LXC-ImgBuilder/Debian-LXC
java -jar /opt/jenkins-cli.jar -s http://localhost:8080/ -webSocket -auth admin:$AdminPassword  build LXC-ImgBuilder/Alpine-LXC
echo "Debian-LXC and Alpine-LXC build will fail, this is expected behaviour"
echo "Next step is to login to the jenins instance, update the credentials for the Node and delete the admin user"