# .linux-env
VIDEO_PATH_LOCAL="/home/valere/Downloads"
IMAGE_PATH_LOCAL="/home/valere/Downloads"
MUSIC_PATH_LOCAL="/home/valere/evilspins/web/mnt/media/files/music"

VIDEO_PATH="/home/root/media/video/autre"
IMAGE_PATH="/home/root/media/image/screenshit"
MUSIC_PATH="/home/root/media/files/music"

dlvideo() {
  cd "$VIDEO_PATH" || return
  pip install -U yt-dlp
  yt-dlp -f "bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4" --merge-output-format mp4 "$@"
}

dlimage() {
  wget -P "$IMAGE_PATH" "$1"
}

dlcover() {
  query_or_file="$1"
  manual_url="$2"
  

  if [ -f "$query_or_file" ]; then
    file="$query_or_file"
  elif echo "$query_or_file" | grep -q '%(ext)'; then
    file="$query_or_file"
  else
    file=$(find "$MUSIC_PATH" -type f \
      \( -iname "*${query_or_file}*.mp3" -o -iname "*${query_or_file}*.m4a" -o -iname "*${query_or_file}*.flac" -o -iname "*${query_or_file}*.wav" -o -iname "*${query_or_file}*.ogg" \) \
      | head -n 1)
  fi

  if [ -z "$file" ]; then
    echo "❌ Aucun fichier trouvé pour: $query_or_file"
    return 1
  fi

  base="${file%.*}"

  if [ -n "$manual_url" ]; then
    curl -s "$manual_url" -o "${base}.jpg"
    echo "✅ ${base}.jpg saved from manual URL (fichier: $file)"
    return
  fi

  artist_raw=$(echo "$base" | awk -F'__' '{print $2}')
  title_raw=$(echo "$base" | awk -F'__' '{for(i=3;i<=NF;i++){printf (i>3?" ":""); printf $i}}')
  artist=$(echo "$artist_raw" | tr '_' ' ')
  title=$(echo "$title_raw" | tr '_' ' ')
  query_enc=$(jq -rn --arg s "$artist $title" '$s|@uri')

  it_url="https://itunes.apple.com/search?term=${query_enc}&entity=song&limit=1"
  art=$(curl -s "$it_url" | jq -r '.results[0].artworkUrl100')
  if [ -n "$art" ] && [ "$art" != "null" ]; then
    art_hi=$(echo "$art" | sed 's/100x100bb/1000x1000bb/')
    curl -s "$art_hi" -o "${base}.jpg"
    echo "✅ ${base}.jpg saved from iTunes (fichier: $file)"
    return
  fi

  api_key="5ae5d1212599f96ffa799e21da1b2a7a38274c94bb5ae8ad26ce8d5b08528aaa"
  url=$(curl -s "https://serpapi.com/search.json?engine=google&q=${query_enc}&tbm=isch&api_key=${api_key}" \
        | jq -r '.images_results[0].original')

  if [ -n "$url" ] && [ "$url" != "null" ]; then
    curl -s "$url" -o "${base}.jpg"
    echo "✅ ${base}.jpg saved from Google Images (fichier: $file)"
  else
    echo "❌ No cover found for $artist - $title (fichier: $file)"
  fi
}

dlmusic() {
  mkdir -p "$MUSIC_PATH"

  NOW=$(date +"%Y%m%d%H")
  URL="$1"

  # Récupère le titre
  TITLE=$(yt-dlp --get-title "$URL")

  # Transforme le titre pour le nom de fichier
  SAFE_TITLE="${TITLE// - /__}"
  SAFE_TITLE="${SAFE_TITLE// /_}"

  TRACK_NAME="$MUSIC_PATH/${NOW}__${SAFE_TITLE}.%(ext)s"

  # Téléchargement
  yt-dlp -x --audio-format mp3 -o $TRACK_NAME "$URL"
  dlcover $TRACK_NAME
}

search() {
  find ./ -iname "*$1*"
}

searchinside() {
  grep -rwl "$1" ./
}

alias evilsync='rsync -avz --delete -e ssh $MUSIC_PATH_LOCAL root@erudi.fr:$MUSIC_PATH'
alias evildiff='rsync -avzn --delete -e ssh $MUSIC_PATH_LOCAL root@erudi.fr:$MUSIC_PATH'

# end .linux-env
