#!/bin/bash

# Ask for sudo password up front
if sudo -v; then
  echo "Sudo privileges granted."
else
  echo "This script requires sudo access."
  exit 1
fi

# Set working directory
WORKDIR="$HOME/ab"

# Create folder and navigate to it
mkdir -p "$WORKDIR"
cd "$WORKDIR" || { echo "Failed to navigate to $WORKDIR"; exit 1; }

# Download the zip file
ZIP_URL="hhttps://nuvon.io/content/files/2025/08/Synology-Active-Backup-for-Business-Agent-3.0.0-4638-x64-deb.zip"
ZIP_FILE="agent.zip"

echo "Downloading ABB Agent..."
curl -L -o "$ZIP_FILE" "$ZIP_URL"

# Check if unzip is installed
if ! command -v unzip &> /dev/null; then
  echo "unzip not found. Installing unzip..."
  sudo apt update && sudo apt install -y unzip
fi

# Unzip the file
echo "Unzipping agent..."
unzip -o "$ZIP_FILE"

# Execute the installer
echo "Running installer..."
sudo ./install.run

# Run the CLI command
echo "Running abb-cli registration..."
echo "y" | sudo abb-cli -c -a backup.nuvon.io -u felipe.baez -p giGdax-5zusne-firwyn

echo "Done!"