#!/bin/bash

# Directory structure is supposed to be /home/builder/swarmops/packaging/...
# Builds in /home/builder/swarmops/{buildtype}
# Home folder for script is /home/builder/swarmops/packaging

DEPLOYDIR=usr/share/swarmops
DISTROS=xenial,bionic,stretch

# Check parameters

if [ "$2" == "release" ]; then
  CONTROLSUFFIX=-release
  DROPFOLDER=release
  if [ "$1" == "extscripts" ]; then
    echo "Error: Cannot package external scripts for stable build. Use 'sprint' or 'internal'."
    exit 1;
  fi
  echo "Packaging FINISHED RELEASE for super-stable deployments."

elif [ "$2" == "sprint" ]; then
  CONTROLSUFFIX=-sprint
  DROPFOLDER=sprint
  echo "Packaging END-OF-SPRINT for normal deployments."
elif [ "$2" == "candidate" ]; then
  CONTROLSUFFIX=-sprint
  DROPFOLDER=sprint
  echo "Packaging RELEASE CANDIDATE."
else
  CONTROLSUFFIX=-internal
  DROPFOLDER=internal
fi

if [ "$1" == "frontend" ]; then
  echo "Packaging frontend...";
  SOURCEDIR=/home/builder/swarmops/builds/$DROPFOLDER/frontend
  TARGETDIR=frontend
  PACKAGE=frontend
  INCLUDEDB=no
  COPYFILES=yes
elif [ "$1" == "backend" ]; then
  echo "Packaging backend..."
  SOURCEDIR=/home/builder/swarmops/builds/$DROPFOLDER/backend
  TARGETDIR=backend
  PACKAGE=backend
  INCLUDEDB=no
  COPYFILES=yes
elif [ "$1" == "extscripts" ]; then
  echo "Packaging external scripts..."
  SOURCEDIR=/home/builder/swarmops/sources/external-scripts
  TARGETDIR=frontend/Scripts/ExternalScripts
  PACKAGE=external-scripts
  COPYFILES=yes
elif [ "$1" == "common" ]; then
  echo "Packaging common (the config file)..."
  COPYFILES=no
  INCLUDEDB=yes
  PACKAGE=common
else
  echo "Syntax:"
  echo "repackage {frontend|backend|extscripts|common} [candidate|sprint|release]"
  echo
  echo "If 'candidate', 'sprint', or 'release' is not given, -internal will be added to the package name."
  exit 1;
fi

PACKAGENAME=$PACKAGE$CONTROLSUFFIX

mkdir -p $DROPFOLDER

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

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

echo ""
echo "Building package $PACKAGENAME $BUILDID."
echo "----------------------------------------------------------------------"
echo ""

# Delete flag that indicates a new drop is available
#rm /home/packaging/lastbuild/BuildDropped.txt

# Remove previous package files, if any
if [ -d $DROPFOLDER/payload ]; then
  rm -rf $DROPFOLDER/payload
fi

# Init build
mkdir -p $DROPFOLDER/payload
mkdir -p $DROPFOLDER/payload/DEBIAN
cd $DROPFOLDER/payload


if [ "$1" == "extscripts" ]; then
  echo "Copying from $SOURCEDIR to $DEPLOYDIR/$TARGETDIR."
#  read -n1 -r -p "About to copy files, press any key..."
fi


if [ "$COPYFILES" == "yes" ]; then
  echo "Copying payload files into repo..."
  mkdir -p $DEPLOYDIR/$TARGETDIR
  cp -R $SOURCEDIR/* $DEPLOYDIR/$TARGETDIR/.

  #Special case: remove staging sandbox from scripts before packaging
  if [ "$1" == "extscripts" ]; then
    rm -rf $DEPLOYDIR/$TARGETDIR/staging
  fi

  if [ -d $SOURCEDIR/libsystemd ]; then
    echo "Adding /lib/systemd files..."
    mkdir -p lib/systemd/system
    cp $SOURCEDIR/libsystemd/* lib/systemd/system/.
    ls -la lib/systemd/system
  fi

  if [ "$1" == "frontend" ]; then
    echo "Moving user-configurable files to /etc/swarmops/..."
    mkdir -p etc/swarmops
    mv $DEPLOYDIR/$TARGETDIR/machineKey.config etc/swarmops/.
    sed -i 's/\"machineKey.config\"/\"..\/..\/..\/..\/etc\/swarmops\/machineKey.config\"/g' $DEPLOYDIR/$TARGETDIR/web.config
    echo "UNINITIALIZED" > etc/swarmops/symmetricKey.config
  fi
fi

#if [ "$1" == "extscripts" ]; then
#  read -n1 -r -p "About to copy files, press any key..." key
#fi

echo "Copying template files into repo..."
if [ "$INCLUDEDB" == "yes" ]; then
  mkdir -p etc/swarmops
  cp ../../templates/database.config.template etc/swarmops/database.config
fi
cp ../../templates/control-$PACKAGENAME.template DEBIAN/control
cp ../../templates/conffiles-$PACKAGE.template DEBIAN/conffiles


if [ -e "../../templates/prerm-$PACKAGE.template" ]; then
  echo "Adding $PACKAGE prerm script..."
  cp ../../templates/prerm-$PACKAGE.template DEBIAN/prerm
  chmod +x DEBIAN/prerm
else
  if [ -e "DEBIAN/prerm" ]; then
    echo "Deleting old prerm script..."
    rm DEBIAN/prerm
  fi
fi

if [ -e "../../templates/postinst-$PACKAGE.template" ]; then
  echo "Adding $PACKAGE postinst script..."
  cp ../../templates/postinst-$PACKAGE.template DEBIAN/postinst
  chmod +x DEBIAN/postinst
else
  if [ -e "DEBIAN/postinst" ]; then
    echo "Deleting old postinst script..."
    rm DEBIAN/postinst
  fi
fi

if [ -e "../../templates/preinst-$PACKAGE.template" ]; then
  echo "Adding $PACKAGE preinst script..."
  cp ../../templates/preinst-$PACKAGE.template DEBIAN/preinst
  chmod +x DEBIAN/preinst
else
  if [ -e "DEBIAN/preinst" ]; then
    echo "Deleting old preinst script..."
    rm DEBIAN/preinst
  fi
fi

echo "Converting CRLF line endings in scripts to LF-only..."
dos2unix -q DEBIAN/*
dos2unix -q etc/init.d/*

if [ -e $DEPLOYDIR/$TARGETDIR ]; then
  echo "Timestamping build..."
  echo $BUILDID > $DEPLOYDIR/$TARGETDIR/BuildIdentity.txt
  date +"%Y-%m-%d %H:%M" > $DEPLOYDIR/$TARGETDIR/BuildDateTime.txt
fi

cd ../..


#if [ "$1" == "extscripts" ]; then
#  read -n1 -r -p "About to package the copied files, press any key..." key
#fi



scripts/repackage-build-package-distro $PACKAGENAME $DROPFOLDER $DISTROS $PACKAGE $BUILDID $2

#read -n1 -r -p "Press any key to continue..."


#echo "Cleaning up..."

#rm -rf $DROPFOLDER/payload

