#Get the public key for Jenkins
wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -
sudo echo "deb http://pkg.jenkins-ci.org/debian-stable binary/" >> /etc/apt/sources.list
#Get the public key for Perforce package repo
sudo wget -qO - https://package.perforce.com/perforce.pubkey | sudo apt-key add -
#Update sources list with the Perforce Package repo.
sudo echo "deb http://package.perforce.com/apt/ubuntu xenial release" >> /etc/apt/sources.list
sudo apt-get update
#Make aptitude be fully non-interactive, so we don't get the issues with Grub trying to configure itself.
sudo DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
sudo apt-get -y install default-jdk helix-cli helix-p4d
sudo apt-get -y install jenkins
#Configure P4D
sudo /opt/perforce/sbin/configure-helix-p4d.sh -n perforce1 -p ssl:1666 -r /opt/perforce/servers/perforce1 -u super -P REinforce --unicode
#Replace the SSL fingerprint of P4D with a pre-existing one from credentials.xml
#This should not be done in production
p4 trust -f -i B9:CB:78:22:66:EB:CF:CF:02:18:BC:61:11:8D:D6:1B:9E:EB:23:B5
#Setup Jenkins
sudo cp hws/config/config.xml /var/lib/jenkins/
sudo mkdir /var/lib/jenkins/users/admin/
sudo cp /home/vagrant/hws/config/users/admin/config.xml /var/lib/jenkins/users/admin/config.xml
sudo cp /home/vagrant/hws/config/credentials.xml /var/lib/jenkins/credentials.xml
sudo cp -r /home/vagrant/hws/config/jobs/ /var/lib/jenkins/
sudo chown -R jenkins:jenkins /var/lib/jenkins/
#Setup the Jenkins cli, so we can install the plugins without needing the GUI
wget http://localhost:8080/jnlpJars/jenkins-cli.jar
java -jar jenkins-cli.jar -s http://localhost:8080/ -auth admin:admin install-plugin p4
#Restart Jenkins
java -jar jenkins-cli.jar -s http://localhost:8080/ -auth admin:admin restart