#!/bin/bash

# for testing we can use this to actually stop/start without redeploy
if grep -q "development" .env; then
    printenv > /tmp/ffenv
fi

if [[ -z "$RECO_TYPE" ]]; then
    echo "RECO_TYPE environment variable is not set. Exiting..."
    exit 1
fi
if [[ -z "$ENV_UPDATE" ]]; then
    echo "RECO_TYPE environment variable is not set. Exiting..."
    exit 1
fi
if [[ -z "$DOPPLER_TOKEN" ]]; then
    echo "RECO_TYPE environment variable is not set. Exiting..."
    exit 1
fi
# Create variables
RECO_TYPE_CLOUD="reco-${RECO_TYPE}"
# RECO_TYPE_CLOUD_NO_DASH="reco-${RECO_TYPE//-/}cloud"
RECO_TYPE_CLOUD_NO_DASH="recomedcloud"


#first delete the old files; 
rm -rf /home/ubuntu/slack-bot-server
rm -rf /home/ubuntu/commit-review

cd /home/ubuntu 
wget https://github.com/tluyben/slack-bot-server/raw/refs/heads/main/slack-bot-server-linux-amd64 -O slack-bot-server
chmod +x slack-bot-server
wget https://github.com/tluyben/commit-review/raw/refs/heads/main/commit-review-linux-amd64 -O commit-review
chmod +x commit-review

cd /home/ubuntu/reco-medcloud

git stash 
git pull 

# TODO: fix, but ok for now
if [ -z "$(ls -A ./uploads)" ]; then
    sudo rsync -r ./web/uploads/* ./guploads/
fi
sudo chmod -R 777 ./uploads

# if /home/backups does not exist, create it; 
if [ ! -d "/home/backups" ]; then
    sudo mkdir -p /home/backups/database
fi
sudo chmod -R 777 /home/backups

if [ ! -d "./logs" ]; then
    sudo mkdir -p ./logs
fi

sudo chmod -R 777 ./logs

# reload doppler env
# if env variable ENV_UPDATE is set to true, then update the env variables
if [[ "$ENV_UPDATE" = "true" ]]; then
    /usr/bin/doppler secrets download --no-file --format env > .env
fi 

LOCAL_IP="127.0.0.1"
# check if if the binary /usr/bin/tailscale exists , if it does, set LOCAL_IP=/usr/bin/tailscale ip -4 , other wise do nothing: 
if [ -f "/usr/bin/tailscale" ]; then
    LOCAL_IP=$(/usr/bin/tailscale ip -4)
fi

# test if LOCAL_IP is in .env , if it is, replace it with this one, if not, add it;
if grep -q "LOCAL_IP=" .env; then
    sed -i "s/LOCAL_IP=.*/LOCAL_IP=$LOCAL_IP/g" .env
else
    echo "LOCAL_IP=$LOCAL_IP" >> .env
fi

sed -i 's|^BASE_URL="https://production.reco-logocloud.com"$|BASE_URL="https://reco-logocloud.com"|' .env

#chmod -R 777 ./web/uploads


# /usr/bin/doppler secrets download --no-file --format env > .env
if ! grep -q "RECO_TYPE=$RECO_TYPE" .env; then
    echo "RECO_TYPE=$RECO_TYPE" >> .env
fi

if [[ "$RECO_TYPE" = "med" ]]; then
    if ! grep -q "RECO_DEVOPS_PORT=81" .env; then
        echo "RECO_DEVOPS_PORT=81" >> .env
    fi
    if ! grep -q "RECO_DEVOPS_PORT_SSL=444" .env; then
        echo "RECO_DEVOPS_PORT_SSL=444" >> .env
    fi
else
    if ! grep -q "RECO_DEVOPS_PORT=82" .env; then
        echo "RECO_DEVOPS_PORT=82" >> .env
    fi
    if ! grep -q "RECO_DEVOPS_PORT_SSL=445" .env; then
        echo "RECO_DEVOPS_PORT_SSL=445" >> .env
    fi
fi

# if SLACK_CHANNEL AND SLACK_TOKEN are set, then a) check .env if it they  are there and if not , add them ; they are optional 
# so simply ignore if they are not there ; 
if [[ -n "$SLACK_CHANNEL" && -n "$SLACK_TOKEN" ]]; then
    if ! grep -q "SLACK_CHANNEL=$SLACK_CHANNEL" .env; then
        echo "SLACK_CHANNEL=$SLACK_CHANNEL" >> .env
    fi
    if ! grep -q "SLACK_TOKEN=$SLACK_TOKEN" .env; then
        echo "SLACK_TOKEN=$SLACK_TOKEN" >> .env
    fi
fi

ENV_FILE=".env"

# Remove all RECO_MED_SMTP_* variables
sed -i '/^RECO_MED_SMTP_/d' "$ENV_FILE"

# Add new variables
echo "RECO_MED_SMTP_HOST=smtp-forwarder" >> "$ENV_FILE"
echo "RECO_MED_SMTP_PASSWORD=hello" >> "$ENV_FILE"
echo "RECO_MED_SMTP_PORT=25" >> "$ENV_FILE"
echo "RECO_MED_SMTP_SSL=465" >> "$ENV_FILE"
echo "RECO_MED_SMTP_TLS=587" >> "$ENV_FILE"
echo "RECO_MED_SMTP_USER=john" >> "$ENV_FILE"

# Remove all RECO_LOGO_SMTP_* variables
sed -i '/^RECO_LOGO_SMTP_/d' "$ENV_FILE"

# Add new variables
echo "RECO_LOGO_SMTP_HOST=smtp-forwarder" >> "$ENV_FILE"
echo "RECO_LOGO_SMTP_PASSWORD=hello" >> "$ENV_FILE"
echo "RECO_LOGO_SMTP_PORT=25" >> "$ENV_FILE"
echo "RECO_LOGO_SMTP_SSL=465" >> "$ENV_FILE"
echo "RECO_LOGO_SMTP_TLS=587" >> "$ENV_FILE"
echo "RECO_LOGO_SMTP_USER=john" >> "$ENV_FILE"

sed -i '/^RECO_MED_SES_/d' "$ENV_FILE"
sed -i '/^RECO_LOGO_SES_/d' "$ENV_FILE"

PHPINI="php.ini"

# if .env contains 'development then PHPINI=php-test.ini; 
if grep -q "development" .env; then
    PHPINI="php-test.ini"
fi

export PHPINI

echo "PHPINI is set to: $PHPINI"  # Debugging check


# docker kill ${RECO_TYPE_CLOUD}-php1
# docker kill ${RECO_TYPE_CLOUD}-web1
# docker-compose down cron 
# docker-compose down slack-bot
# docker-compose down smtp-forwarder
# docker system prune -f -a
docker-compose down php1 web1
sleep 2
# this only if you want to total rebuild
# docker system prune -f -a
# docker-compose up php1 -d --build
PHPINI=$PHPINI docker-compose up -d

# if SLACK_CHANNEL OR SLACK_TOKEN are not set, down slack-bot
if [[ -z "$SLACK_CHANNEL" || -z "$SLACK_TOKEN" ]]; then
    docker-compose down slack-bot
fi
sleep 5

# run migrations
# docker exec ${RECO_TYPE_CLOUD}-php1 bash -c "cd /var/www/html/${RECO_TYPE_CLOUD_NO_DASH} && php index.php partner/cli_migrate" > /tmp/migrations.log 2>&1
# echo "Exit status: $?" >> /tmp/migrations.log
./devops/db_migrate $RECO_TYPE
./devops/db_migrate $RECO_TYPE
./devops/db_migrate $RECO_TYPE

# if grep -q "development" .env; then
#     # nothing for now 
# fi

exit 0
