#!/bin/ksh
browser="links -g"
tui_links="xterm -T links -e links"
invidious_instance="https://vid.priv.au"
dl_dir=~/media/music

#instances:
breeze_wiki="antifandom.com"
invidious="yewtu.be"
nitter="nitter.cz"
anonymous_overflow="code.whatever.social"

touch ~/.ltmp

front() {
	linkschck
	grab_url
	uri=$(xclip -o)

        case $uri in
                *stackoverflow*) uri=$(print "$uri" | sed "s/stackoverflow.com/$anonymous_overflow/") ;;
                *fandom*) uri=$(print "$uri" | sed "s/fandom.com/$breeze_wiki/");;
                *youtube*) uri=$(print "$uri" | sed "s/youtube.com/$invidious/");;
                *youtu*) uri=$(print "$uri" | sed "s/youtu.be/$invidious/");;
                *twitter*) uri=$(print "$uri" | sed "s/twitter.com/$nitter/");;
        esac

	print $uri | xclip
	rp_echo "Parsing $uri"
	sleep .3
	xdotool key g Shift+Insert Enter

}

rp_echo() {
#	ratpoison -c "echo serpico: $*"
# YOU CAN ADD SOME OTHER NOTIFICATION HERE
# ie: notify-send "$*"
:
}

help() {
cat <<eof_
SERPICO
lx: is the serpico extension for graphical links2.
Options that open a new terminal window:
 - vid    open an invidious instance, optionally enter search query
 - go     open a new links window and go to <url>
 - re     open the current page in TUI links
 
Options that manipulate highlighted url:
 - parse  replace youtube, twitter, stackoverflow, fandom urls with frontends
 - play   attempt to play url with mpv/yt-dlp
 - rip    rip audio from a highlighted url with yt-dlp and download it
 - ytdl   download highlighted video

Experimental option that doesn't start a new links2 instance:
 - search performs a search query; if a links2 window is focused,
          the current page is replaced with the searched term.
          If links2 is not focused, start a new instance and perform
          a search
           
For more information consult run linccs -h.
eof_
}

funcs() {
cat <<eof_ | sort -V
vid
search
go
wiki
bookmark
load
help
rip
parse
re
play
youtube
ytdl
eof_
}

re() {
	linkschck
	xdotool key Shift+g Ctrl+u Escape
	xclip -o > ~/.ltmp
	xdotool key Shift+q
	$tui_links $(cat ~/.ltmp)
	
}

bookmark() {
	linkschck
	xdotool key Shift+g Ctrl+u Escape
	print "$(dmenu -p bookmark </dev/null) | $(xclip -o)" >> ~/bookmarks.txt
}

vid() {
	query="$(dmenu </dev/null)" 
  if [[ $query = "" ]]; then
    rp_echo "opening up invidious"
    $browser "$invidious_instance"
  else
    rp_echo "looking up videos: $query"
    $browser "$invidious_instance/search?q=$query"
  fi
}

search() {
	if [[ $(xdotool getwindowfocus getwindowname) = *[Ll]inks* ]]; then
	query="$(dmenu </dev/null)" 
    print "duckduckgo.com/lite/?q=$query" | xclip
	xdotool key g Shift+Insert Enter
	else
	query="$(dmenu </dev/null)" 
    if [[ $query = "" ]]; then
        $browser "duckduckgo.com"
    else
        rp_echo "looking up $query on duckduckgo"
        $browser "duckduckgo.com/lite/?q=$query"
    fi
	fi
}

load() {
	query=$(cat ~/bookmarks.txt | sort | dmenu -l 500)
	print $query | cut -d "|" -f2 | xclip
	if [[ $(xdotool getwindowfocus getwindowname) = *[Ll]inks* ]]; then
	if [[ $query = "" ]]; then exit; fi
	xdotool key g Shift+Insert Enter
	else
	$browser "$(xclip -o)"
	fi
}

wiki() {
	query="$(dmenu </dev/null)" 
    if [[ $query = "" ]]; then
		rp_echo "Firing up wikipedia."
		$browser "https://en.wikipedia.org"
	else
        rp_echo "looking up $query on wikipedia";
        $browser "https://en.wikipedia.org/wiki/Special:Search?go=Go&search=$query&ns0=1#bodyContent"
    fi
}

go() {
	query="$(dmenu </dev/null)"
	$browser "https://$query"
}

linkschck() {
    if [[ $(xdotool getwindowfocus getwindowname) != *[Ll]inks* ]]; then
        rp_echo "Careful, links is not focused."
        exit
    fi
}

grab_url() {
    xdotool key --clearmodifiers Escape l c
}

rip() {
    cd $dl_dir
    rp_echo "$dl_dir"
    linkschck
    grab_url
    yt-dlp -x $(xclip -o) && rp_echo "file dl'd" || rp_echo "fail"
    mpc update
}

play() {
    linkschck
    grab_url
    mpv $(xclip -o) || rp_echo "playback fail"
}

ytdl() {
	linkschck
	grab_url
	yt-dlp $(xclip -o) && rp_echo "Got it." || rp_echo "dl failed."
}

if [[ $* = "" ]]; then
case $(funcs | dmenu) in 
	vid) vid;;
	go) go;;
	search) search;;
	bookmark) bookmark;;
	load) load;;
	wiki) wiki;;
	rip) rip;;
	play) play;;
	ytdl) ytdl;;
	youtube) ytdm;;
	parse) front;;
	re) re;;
	help) $(help | dmenu -l 240);;
esac
fi
