#!/bin/bash

#Check if we're running as root

if [ $EUID -ne 0 ]; then
  echo "ERROR: The packaging must be run as the root user; sudoing is not"
  echo "       enough (if just sudoing, the package signing key will not"
  echo "       be found by reprepro and the packaging will fail)."
  echo " "
  echo "Do a 'sudo su' and run the packaging again as root user."
  echo " "
  exit 1
fi

#Update schemata
cd /var/www/packages.swarmops.com/schemata
echo "Checking for updates to schema..."
svn update

cd /home/builder/swarmops/packaging

#Update all files, including this one. Runpos will remain same in possible new version
echo "Checking for updates to packaging process..."
svn update

# Check parameters

if [ "$1" == "release" ]; then
  DROPFOLDER=release
  echo "Packaging FINISHED RELEASE for super-stable deployments."
elif [ "$1" == "sprint" ]; then
  DROPFOLDER=sprint
  echo "Packaging END-OF-SPRINT for normal deployments."
elif [ "$1" == "candidate" ]; then
  DROPFOLDER=sprint
  echo "Packaging RELEASE CANDIDATE to test a specific build before releasing it."
elif [ "$1" == "" ]; then
  DROPFOLDER=internal
  echo "internal" > ../builds/internal/frontend/SprintName.txt
  echo "Packaging internal test build."
else
  echo "Syntax: repackage-build [candidate|sprint|release]"
  echo "(No parameter will trigger a build of the internal branch.)"
  exit 1;
fi

cp ../builds/$DROPFOLDER/BuildIdentity.txt $DROPFOLDER/BuildIdentity.txt
cp ../builds/$DROPFOLDER/CommitIdentity.txt $DROPFOLDER/CommitIdentity.txt

BUILDID=$(cat $DROPFOLDER/BuildIdentity.txt | tr -d ' \r')
COMMITID=$(cat $DROPFOLDER/CommitIdentity.txt | tr -d ' \r')

BUILDID=$(echo $BUILDID)-$(echo $COMMITID | cut -c 1-7)

echo ""
echo "Packaging Swarmops, branch '$DROPFOLDER', build $BUILDID."
echo "----------------------------------------------------------------------"
echo ""

if [ "$DROPFOLDER" != "stable" ]; then
  scripts/repackage-build-package extscripts $DROPFOLDER
fi

scripts/repackage-build-package common $1
scripts/repackage-build-package backend $1
scripts/repackage-build-package frontend $1

echo "Notifying developers that new build has packaged for test/deployment..."

if [ "$DROPFOLDER" == "internal" ]; then
  PACKAGELABEL="internal"
  echo "$(cat ../builds/internal/CommitMessages.txt)" > deploymsg.txt
  echo "" >> deploymsg.txt
  echo "The packages (swarmops-frontend-internal, swarmops-backend-internal) are available from repositories now, and can be tested at the Dev Sandbox as usual. Last 3 commits listed above." >> deploymsg.txt
  
  echo "Installing unattended on Sandbox..."
  cat scripts/remote-update-sandbox | ssh swarmops-sandbox /bin/bash

  # Do not mail for internal releases, for now

#else
#  This didn't work anyway
#  -----------------------
#  PACKAGELABEL="$DROPFOLDER $(cat ../builds/$DROPFOLDER/frontend/SprintName.txt | tr -d '\n\r')"
#  echo "NOTE: this is a $DROPFOLDER package bundle. It will deploy automatically to live systems via apt upgrade. Yay, we made another sprint!" > deploymsg.txt
#  mail -a "From: Swarmops Builder <builds@swarmops.com>" -s "Swarmops $BUILDID ($PACKAGELABEL) packaged." "swarmops.developers@groups.facebook.com" < deploymsg.txt

#   scripts/notify-sprint-ready.sh
fi

# TODO: Add build messages to deploy message, at least for internal build

rm deploymsg.txt
rm $DROPFOLDER/BuildIdentity.txt
rm $DROPFOLDER/CommitIdentity.txt


