#!/bin/bash

echo "Crear el directorio de destino en el servidor"
SSH_PORT=19247
SERVER_IP4="172.105.103.215"

if [ $USER = "jorgeescallon" ]; then
  REMOTE_USER="deeeijppnwkr"
  REMOTE_PWD="jcapuQfNM6gYrZyX7Q991UWU"
else
  REMOTE_USER="sceovddutywx"
  REMOTE_PWD="3tAcbDCqhV1a7n9fa6QZJXoO"
fi

SITE_NAME="www.calacloud.net"
SITE_DOMAINS="calacloud.net www.calacloud.net"
PROJECT_NAME="calacloud"

DEPLOYED_DATA_FOLDER="/var/www/static/${SITE_NAME}"

VHOST_FILE="${SITE_NAME}"
TMP_VHOST_FILE="/tmp/${VHOST_FILE}"
DEPLOYED_VHOST_FILE="/etc/nginx/sites-available/${VHOST_FILE}"
ENABLED_VHOST_FILE="/etc/nginx/sites-enabled/${VHOST_FILE}"

echo "retirando virtualhost"
ssh -q -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -p ${SSH_PORT} ${REMOTE_USER}@${SERVER_IP4} "echo ${REMOTE_PWD} | sudo -S -- sh -c 'rm ${TMP_VHOST_FILE} ${DEPLOYED_VHOST_FILE} ${ENABLED_VHOST_FILE};systemctl reload nginx'"

echo "retirando logs"
LOGS_FOLDER="/var/www/log/${SITE_NAME}"
ssh -q -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -p ${SSH_PORT} ${REMOTE_USER}@${SERVER_IP4} "echo ${REMOTE_PWD} | sudo -S -- sh -c 'rm -r ${LOGS_FOLDER}'"

echo "Elimiando deployed data"
ssh -q -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -p ${SSH_PORT} ${REMOTE_USER}@${SERVER_IP4} "echo ${REMOTE_PWD} | sudo -S -- sh -c 'rm -r ${DEPLOYED_DATA_FOLDER}'"

echo "Cambios aplicados en produccion. Se eliminó: ${SITE_NAME}"
