#!/bin/bash

set -u

echo "================================"
echo "        spotDL Downloader"
echo "================================"
echo

read -rp "Spotify link: " SPOTIFY_URL

if [[ -z "$SPOTIFY_URL" ]]; then
    echo "Error: Spotify link cannot be empty."
    exit 1
fi

echo
read -rp "YouTube link (optional): " YOUTUBE_URL

echo
echo "Starting download..."
echo

if [[ -n "$YOUTUBE_URL" ]]; then
    # Manual YouTube + Spotify matching
    spotdl download "${YOUTUBE_URL}|${SPOTIFY_URL}"
else
    # Normal Spotify download
    spotdl download "$SPOTIFY_URL"
fi

STATUS=$?

echo

if [[ $STATUS -eq 0 ]]; then
    echo "Download finished!"
    echo "Fixing permissions..."

    sudo chmod 775 -R ./*

    echo "Done!"
else
    echo "spotDL failed with exit code $STATUS."
    exit "$STATUS"
fi
