#!/bin/bash

set -e

puser="${PIPEDREAM_USER:-pipedream}"

echo "Starting scheduling service"
uname -a
echo "Scheduling service will use ${puser} as the user to trigger retries with."
echo ""

puser_dir="/root/.pipedream"
puser_ssh_key_path="${puser_dir}/${puser}.key"
mkdir -p "${puser_dir}"
chmod 400 "${puser_dir}"
echo "${puser}" > /root/.pipedream/default_user

if [[ "${PIPEDREAM_USER_SSH_KEY}" ]]; then
    echo "${PIPEDREAM_USER_SSH_KEY}" > "${puser_ssh_key_path}"
    chmod 400 "${puser_ssh_key_path}"
fi

if [ ! -f /root/.pipedream/${puser}.key ]; then
    echo "WARNING: Missing ssh key for ${puser} at /root/.pipedream/${puser}.key"
    echo "         Scheduling service will be unable to trigger retries."
    echo ""
    echo "         Key should be mounted to that path or passed in via PIPEDREAM_USER_SSH_KEY."
    echo ""
fi

/pipedream/scheduling/uwsgi
