#!/bin/bash


# Script Variables
REPO_URL="https://github.com/frbarbre/next-integrate"
APP_DIR=~/next-integrate/myapp
SWAP_SIZE="1G"
DOMAIN_NAME="next-integrate.frederikbarbre.dk"
EMAIL="fr.barbre@gmail.com"
NGINX_CONFIG_NAME="next-integrate"



# Update and install dependencies
sudo apt update && sudo apt upgrade -y

# Add Swap Space
echo "Adding swap space..."
sudo fallocate -l $SWAP_SIZE /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

# Install Docker and Docker Compose
sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" -y
sudo apt update
sudo apt install docker-ce -y

sudo curl -L "https://github.com/docker/compose/releases/download/v2.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -sf /usr/local/bin/docker-compose /usr/bin/docker-compose

sudo systemctl enable docker
sudo systemctl start docker
# Clone the Git repository
if [ -d "$APP_DIR" ]; then
  echo "Directory $APP_DIR already exists. Pulling latest changes..."
  cd $APP_DIR && git pull
else
  echo "Cloning repository from $REPO_URL..."
  git clone $REPO_URL $APP_DIR
  cd $APP_DIR
fi


# Set up next environment variables
cat > "$APP_DIR/./docs/.env" << EOL
BASE_URL=https://next-integrate.frederikbarbre.dk
AUTH_GOOGLE_SECRET=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
AUTH_GOOGLE_ID=952266044449-7lakcgiamn0o3jk724ttkpra0e7ien5h.apps.googleusercontent.com
EOL


# Install and configure Nginx
sudo apt install nginx -y

sudo rm -f /etc/nginx/sites-available/$NGINX_CONFIG_NAME
sudo rm -f /etc/nginx/sites-enabled/$NGINX_CONFIG_NAME

sudo systemctl stop nginx

sudo apt install certbot -y
sudo certbot certonly --standalone -d $DOMAIN_NAME --non-interactive --agree-tos -m $EMAIL

cat > /etc/letsencrypt/options-ssl-nginx.conf << EOL
# This file contains important security parameters. If you modify this file
# manually, Certbot will be unable to automatically provide future security
# updates. Instead, Certbot will print and log an error message with a path to
# the up-to-date file that you will need to refer to when manually updating
# this file. Contents are based on https://ssl-config.mozilla.org

ssl_session_cache shared:le_nginx_SSL:10m;
ssl_session_timeout 1440m;
ssl_session_tickets off;

ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;

ssl_ciphers "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384";
EOL

sudo openssl dhparam -out /etc/letsencrypt/ssl-dhparams.pem 2048

cat > /etc/nginx/sites-available/$NGINX_CONFIG_NAME << EOL
server {
    listen 80;
    server_name ${DOMAIN_NAME}; 

    # Redirect all HTTP requests to HTTPS
    return 301 https://\$host\$request_uri;
}

server {
    listen 443 ssl;
    server_name ${DOMAIN_NAME};

    ssl_certificate /etc/letsencrypt/live/${DOMAIN_NAME}/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/${DOMAIN_NAME}/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;


    location / {
        proxy_pass http://localhost:3001;
        proxy_http_version 1.1;
        proxy_set_header Upgrade \$http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host \$host;
        proxy_cache_bypass \$http_upgrade;
        
        proxy_buffering off;
        proxy_set_header X-Accel-Buffering no;
    }
}
EOL

sudo ln -s /etc/nginx/sites-available/$NGINX_CONFIG_NAME /etc/nginx/sites-enabled/$NGINX_CONFIG_NAME
sudo systemctl restart nginx

# Build and run Docker Compose services
cd $APP_DIR
sudo docker-compose up --build -d

echo "Deployment complete. Your application is available at https://$DOMAIN_NAME"
