#!/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 helix-p4d
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-swarm
#Configure Swarm
sudo /opt/perforce/swarm/sbin/configure-swarm.sh -p ssl:1666 --super-user super --super-passwd REinforce --swarm-user swarm --swarm-passwd Reinforce -e localhost -c
echo "REinforce" | p4 -p ssl:1666 -u super login -ap > passwd.txt
sed -i '1d' passwd.txt
cat passwd.txt | awk '{system("curl -usuper:" $0 " -s http://localhost/about > /dev/null")}'
echo "======================================================================="
echo "=============================== CURL DONE ============================="
echo "======================================================================="
p4 -u super -p ssl:1666 triggers -i <<EOF
Triggers:
swarm.job form-commit job "%quote%/opt/perforce/swarm/p4-bin/scripts/swarm-trigger.pl%quote% -c %quote%/opt/perforce/swarm/p4-bin/scripts/swarm-trigger.conf%quote% -t job -v %formname%"
swarm.user form-commit user "%quote%/opt/perforce/swarm/p4-bin/scripts/swarm-trigger.pl%quote% -c %quote%/opt/perforce/swarm/p4-bin/scripts/swarm-trigger.conf%quote% -t user -v %formname%"
swarm.userdel form-delete user "%quote%/opt/perforce/swarm/p4-bin/scripts/swarm-trigger.pl%quote% -c %quote%/opt/perforce/swarm/p4-bin/scripts/swarm-trigger.conf%quote% -t userdel -v %formname%"
swarm.group form-commit group "%quote%/opt/perforce/swarm/p4-bin/scripts/swarm-trigger.pl%quote% -c %quote%/opt/perforce/swarm/p4-bin/scripts/swarm-trigger.conf%quote% -t group -v %formname%"
swarm.groupdel form-delete group "%quote%/opt/perforce/swarm/p4-bin/scripts/swarm-trigger.pl%quote% -c %quote%/opt/perforce/swarm/p4-bin/scripts/swarm-trigger.conf%quote% -t groupdel -v %formname%"
swarm.changesave form-save change "%quote%/opt/perforce/swarm/p4-bin/scripts/swarm-trigger.pl%quote% -c %quote%/opt/perforce/swarm/p4-bin/scripts/swarm-trigger.conf%quote% -t changesave -v %formname%"
swarm.shelve shelve-commit //... "%quote%/opt/perforce/swarm/p4-bin/scripts/swarm-trigger.pl%quote% -c %quote%/opt/perforce/swarm/p4-bin/scripts/swarm-trigger.conf%quote% -t shelve -v %change%"
swarm.commit change-commit //... "%quote%/opt/perforce/swarm/p4-bin/scripts/swarm-trigger.pl%quote% -c %quote%/opt/perforce/swarm/p4-bin/scripts/swarm-trigger.conf%quote% -t commit -v %change%"
EOF
echo "# SWARM_HOST (required)
# Hostname of your Swarm instance, with leading "http://" or "https://".
SWARM_HOST="http://$HOSTNAME"
# SWARM_TOKEN (required)
# The token used when talking to Swarm to offer some security. To obtain the
# value, log in to Swarm as a super user and select 'About Swarm' to see the
# token value.
SWARM_TOKEN="MY-UUID-STYLE-TOKEN"
# ADMIN_USER (optional)
# For enforcing reviewed changes, optionally specify the normal Perforce user
# with admin privileges (to read keys); if not set, will use whatever Perforce
# user is set in environment.
ADMIN_USER=
# ADMIN_TICKET_FILE (optional)
# For enforcing reviewed changes, optionally specify the location of the
# p4tickets file if different from the default ($HOME/.p4tickets).
# Ensure this user is a member of a group with an 'unlimited' or very long
# timeout; then, manually login as this user from the Perforce server machine to
# set the ticket.
" > /opt/perforce/swarm/p4-bin/scripts/swarm-trigger.conf
swarmtoken=$(ls /opt/perforce/swarm/data/queue/tokens)
cat /opt/perforce/swarm/p4-bin/scripts/swarm-trigger.conf | sed -e 's/MY-UUID-STYLE-TOKEN/'${swarmtoken}'/g' > swarm-trigger.conf
cat swarm-trigger.conf > /opt/perforce/swarm/p4-bin/scripts/swarm-trigger.conf
cat swarm-trigger.conf | sed -e 's/$HOSTNAME/localhost/g' > /opt/perforce/swarm/p4-bin/scripts/swarm-trigger.conf
echo "======================================================================="
echo "=========================== Swarm Config =============================="
echo "======================================================================="
cat /opt/perforce/swarm/p4-bin/scripts/swarm-trigger.conf
echo "======================================================================="
echo "======================================================================="
echo "======================================================================="
# Add some data
cd /opt/perforce/
mkdir ws
cd ws
echo P4PORT=ssl:1666>.p4config
p4 client -o | p4 client -i
echo test > f1
p4 add f1
p4 submit -d "Added my first changelist"
#Disable the default apache2 site.
sudo a2dissite 000-default
sudo service apache2 reload