#!/bin/sh
# Nico Schottelius, 2021-01-17

set -e

if [ $# -ne 1 ]; then
    echo "$0 target-host"
    exit 1
fi

target_host=$1; shift
user=app

dir=${0%/*}
uncloud_base=$(cd ${dir}/.. && pwd -P)
conf_name=local_settings-${target_host}.py
conf_file=${uncloud_base}/uncloud/${conf_name}

if [ ! -e ${conf_file} ]; then
    echo "No settings for ${target_host}."
    echo "Create ${conf_file} before using this script."
    exit 1
fi

# Deploy
rsync -av \
      --exclude venv/ \
      --exclude '*.pyc' \
      --exclude uncloud/local_settings.py \
      --delete \
      ${uncloud_base}/ ${user}@${target_host}:app/

ssh "${user}@${target_host}" ". ~/pyvenv/bin/activate; cd ~/app; pip install -r requirements.txt"

# Config
ssh "${user}@${target_host}" "cd ~/app/uncloud; ln -sf ${conf_name} local_settings.py"

# Restart / Apply
ssh "${user}@${target_host}" "sudo /etc/init.d/uwsgi restart"
