# Get the current non-root user
CURRENT_USER=$(who | awk 'NR==1{print $1}')

# Executed after package installation
post_install() {
    # Check if the Docker/Adguard folder exists, otherwise create it
    if [ ! -d /home/$CURRENT_USER/Docker/Adguard ]; then
        echo "Creating Docker/Adguard folder..."
        mkdir -p /home/$CURRENT_USER/Docker/Adguard
    fi
    
    # Check if Docker is installed
    if ! command -v docker >/dev/null 2>&1; then
        echo "The Docker package is not installed. Please install it before continuing."
        exit 1
    fi

    # Function to check if a port is available
    is_port_available() {
    local port=$1
    if command -v netstat >/dev/null 2>&1; then
        if netstat -tuln | grep ":$port " >/dev/null; then
        return 1 # Port is in use
        else
        return 0 # Port is available
        fi
    else
        return 2 # Unable to check port availability
    fi
    }

    # Function to find an available port starting from a given port
    find_available_port() {
    local start_port=$1
    local port=$start_port
    local max_port=$((start_port + 100))
    while [ $port -lt $max_port ]; do
        if is_port_available "$port"; then
        echo "$port"
        return
        fi
        port=$((port + 1))
    done
    echo "No available ports found in the range $start_port-$max_port" >&2
    exit 1
    }

    # Check if port 53 is available
    if ! is_port_available 53; then
    # Use port 5353 as an alternative if available
    if is_port_available 5353; then
        echo "Port 53 is in use. Using port 5353 as an alternative for the DNS server."
        sed -i 's/53:53\/tcp/5353:53\/tcp/' /etc/docker-biglinux/adguard/docker-compose.yml
        sed -i 's/53:53\/udp/5353:53\/udp/' /etc/docker-biglinux/adguard/docker-compose.yml

    else
        # Use port 6000 as a fallback
        suggested_port=6000
        echo "Both port 53 and 5353 are in use. Using port $suggested_port as an alternative for the DNS server."
        sed -i 's/53:53\/tcp/6000:53\/tcp/' /etc/docker-biglinux/adguard/docker-compose.yml
        sed -i 's/53:53\/udp/6000:53\/udp/' /etc/docker-biglinux/adguard/docker-compose.yml
    fi
    fi

    # Start the Adguard container using docker-compose
    echo "Starting the Adguard container..."

    # Expand $CURRENT_USER and execute docker-compose with eval
    eval "HOME=/home/$CURRENT_USER docker-compose -f /etc/docker-biglinux/adguard/docker-compose.yml up -d"
    
    # Symlink docker-compose.yml to Docker/Adguard folder
    echo "Creating symlink to docker-compose.yml..."
    ln -s /etc/docker-biglinux/adguard/docker-compose.yml /home/$CURRENT_USER/Docker/Adguard/docker-compose.yml
    
    # Set the correct owner and group for the Docker folder and its contents
    echo "Setting correct permissions for Docker folder..."
    chown -R $CURRENT_USER:$CURRENT_USER /home/$CURRENT_USER/Docker/Adguard
}

# Upgrade package
post_upgrade() {
	post_install
}

# Executed before package removal
pre_remove() {
	# Stop and remove the Adguard container
	echo "Stopping and removing Adguard container..."
	docker-compose -f /etc/docker-biglinux/adguard/docker-compose.yml down
	
	# Remove symlink to docker-compose.yml in Docker/Adguard folder
    echo "Removing symbolic link to docker-compose.yml..."
    rm -f /home/$CURRENT_USER/Docker/Adguard/docker-compose.yml
}

# Executed after package removal
post_remove() {
	# Remove the Docker image
    echo "Removing the Adguard Docker image..."
    docker rmi adguard/adguardhome:latest
}
