#!/bin/bash

# Update and upgrade packages
sudo apt update && sudo apt upgrade -y

# Install necessary packages
sudo apt install docker-compose -y

# Create directory and set permissions
sudo mkdir /NPM


# Create and write the script to /etc/rc.local
cat <<EOL | sudo tee /NPM/docker-compose.yml > /dev/null
version: '3.6'
services:
  app:
    image: 'jc21/nginx-proxy-manager:latest'
    restart: unless-stopped
    ports:
      - '80:80'
      - '81:81'
      - '443:443'
    volumes:
      - ./data:/data
      - ./letsencrypt:/etc/letsencrypt
EOL


cd /NPM

server_ip=$(ip addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}')

# Run the docker-compose command
sudo docker-compose up -d

echo " "
echo " "
echo "Nginx Proxy Manager installed successfully"
echo " "
echo "To access Nginx Proxy Manager, enter the IP address: http://$server_ip:81"
echo "Email:    admin@example.com"
echo "Password: changeme"