#!/bin/bash
set -e  # Exit if any command fails

# Define variables
URL="https://darkflows.com/downloads/darkflows-current.tgz"
TMP_DIR=$(mktemp -d)
TARBALL="$TMP_DIR/package.tgz"

# Download the package
echo "Downloading package..."
curl -sSL "$URL" -o "$TARBALL"

# Extract the package
echo "Extracting package..."
tar -xzf "$TARBALL" -C "$TMP_DIR"

# Find and run the install script inside the extracted folder
INSTALL_SCRIPT=$(find "$TMP_DIR" -name "install_darkflows.sh" -type f | head -n 1)

if [ -n "$INSTALL_SCRIPT" ]; then
    echo "Running install script..."
    chmod +x "$INSTALL_SCRIPT"
    "$INSTALL_SCRIPT"
else
    echo "No install_darkflows.sh found in extracted files!"
    exit 1
fi

# Clean up (optional)
rm -rf "$TMP_DIR"

echo "Installation complete."


