# This scripts copy the start service script into the server.
# You need a ssh key with root permissions.

ENV=$1
CUSTOM_ENV=$2

# Checking valid environment
if [ "$ENV" != "prod" ] && [ "$ENV" != "dev" ] && [ "$ENV" != "demo" ] && [ "$ENV" != "open-demo" ]; then
    if [ "$CUSTOM_ENV" != "true" ]; then
        echo "Invalid environment $ENV";
        exit 0;
    fi
    echo "WARNING: you are using a custom env $CUSTOM_ENV"
fi

echo "\033[0;31m"
echo "    *************************************************************************"
echo "    |                                                                       |"
echo "    |   >>> Make sure you already created an user \033[0;45m quantex \033[0m\033[0;31m                 |"
echo "    |       on the $ENV server!!!                                           |"
echo "    |                                                                       |"
echo "    |       Use: adduser --shell /bin/rbash --home /home/quantex quantex    |"
echo "    |                                                                       |"
echo "    |   >>> and add \033[0;45m QUANTEX_PASS \033[0m\033[0;31m variable on GitLab                       |"
echo "    |                                                                       |"
echo "    *************************************************************************"
echo "\033[0m"

read -p "Press enter to continue"

echo "Pushing scripts to $ENV.quantex.com.ar"

ssh root@"$ENV".quantex.com.ar "mkdir -p /home/quantex/qfixdpl; chown quantex:quantex /home/quantex/qfixdpl"
ssh root@"$ENV".quantex.com.ar "mkdir -p /home/quantex/qfixdpl/logs; chown quantex:quantex /home/quantex/qfixdpl/logs"

scp tools/unzip-qfixdpl.sh root@"$ENV".quantex.com.ar:/usr/bin/
ssh root@"$ENV".quantex.com.ar "chmod +x /usr/bin/unzip-qfixdpl.sh"

scp tools/start root@"$ENV".quantex.com.ar:/home/quantex/qfixdpl/
ssh root@"$ENV".quantex.com.ar "chmod +x /home/quantex/qfixdpl/start; chown quantex:quantex /home/quantex/qfixdpl/start"

scp tools/stop root@"$ENV".quantex.com.ar:/home/quantex/qfixdpl/
ssh root@"$ENV".quantex.com.ar "chmod +x /home/quantex/qfixdpl/stop; chown quantex:quantex /home/quantex/qfixdpl/stop"

scp tools/backup.sh root@"$ENV".quantex.com.ar:/home/quantex/qfixdpl/
ssh root@"$ENV".quantex.com.ar "chmod +x /home/quantex/qfixdpl/backup.sh; chown quantex:quantex /home/quantex/qfixdpl/backup.sh"

scp tools/rollback.sh root@"$ENV".quantex.com.ar:/home/quantex/qfixdpl/
ssh root@"$ENV".quantex.com.ar "chmod +x /home/quantex/qfixdpl/rollback.sh; chown quantex:quantex /home/quantex/qfixdpl/rollback.sh"

scp tools/qfixdpl.service root@"$ENV".quantex.com.ar:/etc/systemd/system/
scp tools/qfixdpl-watcher.path root@"$ENV".quantex.com.ar:/etc/systemd/system/
scp tools/qfixdpl-watcher.service root@"$ENV".quantex.com.ar:/etc/systemd/system/

ssh root@"$ENV".quantex.com.ar "systemctl enable qfixdpl.service; systemctl start qfixdpl.service"
ssh root@"$ENV".quantex.com.ar "systemctl enable qfixdpl-watcher.path; systemctl start qfixdpl-watcher.path"

echo "\033[0;31m"
echo "    ************************************************"
echo "    |                                              |"
echo "    |   >>> Remember you need a \033[0;45m conf.toml \033[0m\033[0;31m file   |"
echo "    |       on the qfixdpl folder!!!              |"
echo "    |                                              |"
echo "    ************************************************\033[0m"

