#!/bin/bash
# Work-in-progress omni-launcher based on @Gotbletu's urlportal
#
# https://github.com/gotbletu/dotfiles_v2/raw/master/normal_user/scripts/.scripts/urlportal
#
BROWSER="firefox-bin"
FALLBACK="$BROWSER"
NITTER_INSTANCE="https://nitter.snopyta.org"
VIDEO_PLAYER_CLIP="mpv --loop --really-quiet"
### long videos like youtube
#VIDEO_QUEUE="tsp mpv --ontop --no-border --force-window --autofit=500x280 --geometry=-15-53"
### short videos/animated gif clips
#IMAGEGUI="feh -. -x -B black -g 900x600-15+60"
#TORRENTCLI="transmission-remote --add"
# enable case-insensitive matching
shopt -s nocasematch
URL="$1"
case "$URL" in
*.jpg|*.png|*.gif|*bibliogram.*/imageproxy?url=*)
TEMP=`mktemp`
curl -s "$URL" > "$TEMP"
WIN=`xdotool getwindowfocus`
WIDTH=`xdotool getwindowgeometry --shell $WIN | sed -n 's/^WIDTH=\([[:digit:]]\+\)/\1/p'`
HEIGHT=`xdotool getwindowgeometry --shell $WIN | sed -n 's/^HEIGHT=\([[:digit:]]\+\)/\1/p'`
sxiv -p -b -a -e $WIN -g "$WIDTH"x"$HEIGHT" "$TEMP"
rm -f "$TEMP"
;;
*reddit.com/r/*/comments/*)
$SCRIPTS/reddit_comments.sh "$URL"
;;
#*imgur.com/a/*)
# WIN=`xdotool getwindowfocus`
# URLS=`w3m -dump_source "$URL" | sed -n 's|.*
/dev/null 2>&1 &
# ;;
# *v.redd.it/*|*video.twimg.com/*|*dailymotion.com*)
# nohup $VIDEO_CLIP "$url" > /dev/null 2>&1 &
# ;;
# *youtube.com/watch*|*youtu.be/*|*clips.twitch.tv/*)
# $VIDEO_QUEUE "$url"
# ;;
# *twitch.tv/*)
# $LIVEFEED "$url"
# ;;
# *reddit.com/r/*)
# tmux new-window -n rtv && tmux send-keys "rtv -l $url && tmux kill-pane" 'Enter'
# ;;
# *i.imgur.com/*.gifv|*i.imgur.com/*.mp4|*i.imgur.com/*.webm|*i.imgur.com/*.gif)
# nohup $VIDEO_CLIP "$url" > /dev/null 2>&1 &
# ;;
# *i.imgur.com/*| *imgur.com/*.*)
# # nohup $IMAGEGUI "$url" > /dev/null 2>&1 &
# tmux new-window -n pixcli && tmux send-keys "$IMAGECLI '$url' && tmux kill-pane" 'Enter'
# ;;
# mailto:*)
# tmux split-window -fv && tmux send-keys "mutt -- '$url' && tmux kill-pane" 'Enter'
# ;;
# *.pls|*.m3u)
# tmux split-window -fv -p 20 && tmux send-keys "mpv '$url' && exit" 'Enter'
# ;;
# magnet:*|*.torrent)
# $TORRENTCLI "$url"
# ;;
# *.jpg|*.jpeg|*.png|*:large)
# tmux new-window -n pixcli && tmux send-keys "$IMAGECLI '$url' && tmux kill-pane" 'Enter'
# # nohup $IMAGEGUI "$url" > /dev/null 2>&1 &
# ;;
# *.gif)
# nohup $VIDEO_CLIP "${url/.gifv/.webm}" > /dev/null 2>&1 &
# ;;
# *.mp4|*.mkv|*.avi|*.wmv|*.m4v|*.mpg|*.mpeg|*.flv|*.ogm|*.ogv|*.gifv)
# $VIDEO_QUEUE "$url"
# ;;
# *.mp3|*.m4a|*.wav|*.ogg|*.oga|*.flac)
# # create queue fifo files if it does not exist
# if [[ ! -p /tmp/mpvinput ]]; then
# mkfifo /tmp/mpvinput
# fi
#
# # check if process mpv exist (e.g mpv --input-file=/tmp/mpvinput filename.mp3)
# if pgrep -f mpvinput > /dev/null
# then
# # if mpv is already running then append new url/files to queue
# # echo loadfile \"${url/'/\\'}\" append-play > /tmp/mpvinput
# echo loadfile \"$url\" append-play > /tmp/mpvinput
# # nohup $VIDEO_CLIP "${url/.gifv/.webm}" > /dev/null 2>&1 &
# else
# # if mpv is not running then start it (initial startup)
# # mpv --no-video --input-file=/tmp/mpvinput "$1"
# tmux split-window -fv -p 20 && tmux send-keys "mpv --no-video --input-file=/tmp/mpvinput \"$url\" && exit" 'Enter'
# fi
# # Note: use "<" or ">" hotkeys to skip between songs/audio queue list on mpv
# ;;
*)
$FALLBACK "$URL"
;;
esac