#!/usr/bin/env bash
set -e
#check to make sure this is a 64-bit machine
getconf LONG_BIT | grep 64 > /dev/null || ( echo "Git Fusion requires a 64-bit machine. Exiting." && exit 1)
wget -q http://package.perforce.com/perforce.pubkey -O - | sudo apt-key add -
#Update sources list with the Perforce Package repo.
sudo echo "deb http://package.perforce.com/apt/ubuntu precise release" >> /etc/apt/sources.list
sudo aptitude update
sudo aptitude -y install perforce-cli perforce-server
sudo /opt/perforce/sbin/configure-helix-p4d.sh -n perforce1 -p ssl:1666 -r /opt/perforce/servers/perforce1 -u super -P REinforce
sudo aptitude -y install helix-git-fusion
sudo /opt/perforce/git-fusion/libexec/configure-git-fusion.sh -n --super super --superpassword REinforce --gfp4password REinforce --gfsysuser git --gfdir /opt/perforce/git-fusion/libexec --server local --p4port ssl:1666 --timezone Europe/London --unicode y --unknownuser pusher --https
#Set some system wide variables for the VM.
sudo echo "export P4EDITOR=nano" >> /etc/bash.bashrc
sudo echo "export P4PORT=ssl:localhost:1666" >> /etc/bash.bashrc
sudo echo "export P4CONFIG=.p4config" >> /etc/bash.bashrc
#Create a directory structure, that will be used for the GF admin workspace.
mkdir -p /home/vagrant/workspaces/gf
cd /home/vagrant/workspaces/gf
echo "P4USER=super" > .p4config
echo "P4PORT=ssl:localhost:1666" >> .p4config
echo "P4CLIENT=gf_admin" >> .p4config
#Add the public key used for testing to the workspace.
mkdir -p .git-fusion/users/jen/keys
cp /home/vagrant/hws/keys/vagrant_key.pub /home/vagrant/workspaces/gf/.git-fusion/users/jen/keys
#Add the config files to the correct locations.
cd /home/vagrant/workspaces/gf/.git-fusion
cp /home/vagrant/hws/gf_configs/p4gf_config .
#Put the config file for orca repo in the correct place
mkdir -p repos/orca
cd repos/orca
cp /home/vagrant/hws/gf_configs/orca_config .
mv orca_config p4gf_config
sudo chown -R vagrant /home/vagrant/workspaces/*
echo "Now login to the VM and submit the GF configs and key"