#!/bin/bash
su - !{userName} << 'EndOfScript'
(
set -e
set -x

mkdir -p $HOME/.nextflow
cat <<EOF >> $HOME/.nextflow/config
!{nextflowConfig}
EOF

mkdir -p $HOME/bin
profile=$(mktemp)
cat <<EOF > $profile
!{bashProfile}
EOF

source $profile
cat $profile >> $HOME/.bash_profile
rm $profile

#
# set instance name
#
instance="$(curl -s http://169.254.169.254/latest/meta-data/instance-id)"
zone="$(curl -s 169.254.169.254/latest/meta-data/placement/availability-zone)"
region="${zone::-1}"

#
# Launch docker and pull the container when DOCKER variable is defined
#
[[ '!{dockerPull}' ]] && for x in '!{dockerPull}'; do docker pull $x || true; done

#
# Install NEXTFLOW and launch it
#
version="v!{nextflow.version}"
curl -fsSL http://www.nextflow.io/releases/${version}/nextflow  > $HOME/nextflow
chmod +x $HOME/nextflow
$HOME/nextflow -download

# pull the nextflow pipeline repo
[[ '!{nextflow.pull}' ]] && $HOME/nextflow pull '!{nextflow.pull}'

# launch the nextflow daemon
if [[ '!{role}' == worker ]]; then
  $HOME/nextflow node -cluster.join "$NXF_CLUSTER_JOIN" -cluster.interface eth0 -bg
fi

# save the environment for debugging
env | sort > boot.env

# just a marker file
touch READY

) &> ~!{userName}/boot.log
EndOfScript
