#!/bin/bash

ICON='/usr/share/icons/hicolor/48x48/apps/click-radio.png'
CONF="$HOME/.config/clickradio"
ECON='/usr/share/click-radio/icons/error.svg'
TMP="$HOME/.config/clickradio/.tmp"

PID=`ps -eaf | grep yad | grep -v grep | awk '{print $2}'`
if [[ "" !=  "$PID" ]]; then
kill -9 $PID > /dev/null 2>&1
fi &
PID=`ps -eaf | grep yt-dl | grep -v grep | awk '{print $2}'`
if [[ "" !=  "$PID" ]]; then
kill -9 $PID > /dev/null 2>&1
fi &
PID=`ps -eaf | grep mpv | grep -v grep | awk '{print $2}'`
if [[ "" !=  "$PID" ]]; then
kill -9 $PID > /dev/null 2>&1
fi &
PID=`ps -eaf | grep click-radio | grep -v grep | awk '{print $2}'`
if [[ "" !=  "$PID" ]]; then
kill -9 $PID > /dev/null 2>&1
python3 /usr/share/click-radio/menu/notify/"Youtube Downloader" &
sleep 0.50
URL=`yad --title="Youtube Downloader" \
--width=450 \
--window-icon=$ICON \
--button="Cancel"!/usr/share/click-radio/icons/cancel.svg:1 \
--button="Ok"!/usr/share/click-radio/icons/ok.svg:0 \
--center \
--text="<b>Search</b> https://www.youtube.com/" \
--columns='1' --form --field="Search" "" \
--field="Youtube Music Download"'!/usr/share/click-radio/icons/download.svg!Download Music From Youtube':'FBTN' '/usr/share/click-radio/menu/app/yt-dl & exit'`
if [ $? = "1" ]; then
pkill -f "Youtube Downloader" 2>/dev/null
kill -9 $(ps -ef | grep searchweb | awk '{print $2}') 2>/dev/null &
python3 /usr/share/click-radio/menu/notify/"Youtube Downloader"
else
`echo $URL | awk '{ print $0 }' | sed 's|[|,]||g'  > "$CONF/.tmp"`
TMP=`cat "$CONF/.tmp" | awk '{ print $0 }'`
`echo $TMP | awk '{ print $0 }' | \
xdg-open https://www.youtube.com/results?search_query="$TMP"`
rm -f "$CONF/.tmp" 2>/dev/null
fi

fi &
