#!/bin/bash

# Set the server information
SERVER_IP="62.171.152.24"

# Set the path to the PHP executable on the server
PHP_PATH="/opt/cpanel/ea-php80/root/usr/bin/php"
# Set the composer path
COMPOSER_PATH="/opt/cpanel/ea-php80/root/usr/bin/php /opt/cpanel/composer/bin/composer"

# Set the path to the Laravel project on the server
PROJECT_PATH="/home/starttech/test.health.start-tech.ae"
#Passphrase
SSH_PASSPHRASE="h8=BaIPu~@oE"

#install open open-ssh client
apt-get update -y
apt-get install openssh-client -y

apt-get update -y
apt-get install -y rsync
'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )'
eval $(ssh-agent -s)
chmod 400 "$SSH_PRIVATE_KEY"
echo "$SSH_PASSPHRASE" | ssh-add -k "$SSH_PRIVATE_KEY"
mkdir -p ~/.ssh
chmod 700 ~/.ssh


# Run the migration
ssh -o StricthostKeyChecking=no -i $SSH_PRIVATE_KEY starttech@62.171.152.24 "cd $PROJECT_PATH && $COMPOSER_PATH  update"
ssh -o StricthostKeyChecking=no -i $SSH_PRIVATE_KEY starttech@62.171.152.24 "cd $PROJECT_PATH && $PHP_PATH artisan cache:clear"
ssh -o StricthostKeyChecking=no -i $SSH_PRIVATE_KEY starttech@62.171.152.24 "cd $PROJECT_PATH && $PHP_PATH artisan config:clear"
ssh -o StricthostKeyChecking=no -i $SSH_PRIVATE_KEY starttech@62.171.152.24 "cd $PROJECT_PATH && $PHP_PATH artisan migrate"
ssh -o StricthostKeyChecking=no -i $SSH_PRIVATE_KEY starttech@62.171.152.24 "cd $PROJECT_PATH && $PHP_PATH artisan storage:link"


# Remove the SSH key file
rm -f ssh_key
