#!/bin/bash

# Configuration variables - change this for your setup
gluetun_container_name="gluetun"
qbittorrent_container_name="qbittorrent"
gluetun_origin="http://100.120.152.94:8888"
qb_origin="http://100.120.152.94:8099"
####################################################################

# Arrays for URLs
declare -A gluetun_urls=(
  ["pub_ip"]="$gluetun_origin/v1/publicip/ip"
  ["portforwarded"]="$gluetun_origin/v1/openvpn/portforwarded"
)

declare -A qbittorrent_urls=(
  #used for getting and setting listen_port
  ["prefs"]="$qb_origin/api/v2/app/preferences"
  ["setPrefs"]="$qb_origin/api/v2/app/setPreferences"
)


# Function to check if a Docker container is running
is_container_running() {
  local container_name="$1"
  docker inspect -f '{{.State.Running}}' "$container_name" 2>/dev/null
 # echo "Container $container_name status: $status"
}

get_vpn_external_ip() {
  local url="$1"
  curl -s "$url" | -r .'public_ip'
}

# Function to send a GET request and extract the port from the response
get_port_from_url() {
  local url="$1"
  local port_key

  # Try 'port' key first
  port_key=$(curl -s "$url" | jq -r '.port')

  if [ "$port_key" == "null" ]; then
    # If 'port' key is null, try 'listen_port' key
    port_key=$(curl -s "$url" | jq -r '.listen_port')
  fi

  echo "$port_key"
}

# Function to send a POST request with JSON data
send_post_request() {
  local url="$1"
  local port="$2"
  curl -s -X POST -d json={\"listen_port\":$port} "$url"
}

# Outputs container names
echo "Gluetun container name: $gluetun_container_name - Gluetun Origin URL: $gluetun_origin"
echo "qBittorrent container name: $qbittorrent_container_name - qBittorrent Origin URL: $qb_origin"

# Check if both containers are running
if [[ $(is_container_running "$gluetun_container_name") == $(is_container_running "$qbittorrent_container_name") ]]; then
  echo "Both Gluetun and qBittorrent containers are running. Continuing."

  external_ip=$(get_vpn_external_ip "${gluetun_urls["pub_ip"]}")
  if [ -z "$external_ip" ]; then
    echo "External IP is empty. Exiting script due to potential VPN or internet connection issue."
    exit 1
  else
    echo "External IP is $external_ip therefore VPN is up"
  fi

  gluetun_port=$(get_port_from_url "${gluetun_urls["portforwarded"]}")
  qbittorrent_port=$(get_port_from_url "${qbittorrent_urls["prefs"]}")

  echo "Gluetun forwarded port is $gluetun_port"
  echo "qBittorrent listen port is $qbittorrent_port"
  if [ "$gluetun_port" -eq "$qbittorrent_port" ]; then
    echo "qBittorrent listen port is already set to $qbittorrent_port. No need to change. Exiting script."
  else
    echo "Updating qBittorrent listen port to Gluetun forwarded port $gluetun_port."
    send_post_request "${qbittorrent_urls["setPrefs"]}" "$gluetun_port"
    qbittorrent_port=$(get_port_from_url "${qbittorrent_urls["prefs"]}")
    echo "qBittorrent listen port updated to $qbittorrent_port. Exiting script."
  fi
else
  echo "Either Gluetun or qBittorrent container is not running. Exiting script."
fi
